楼主: newkid

[每日一题] 2022 PUZZLEUP

[复制链接]
论坛徽章:
520
奥运会纪念徽章:垒球
日期:2008-09-15 01:28:12生肖徽章2007版:鸡
日期:2008-11-17 23:40:58生肖徽章2007版:马
日期:2008-11-18 05:09:48数据库板块每日发贴之星
日期:2008-11-29 01:01:02数据库板块每日发贴之星
日期:2008-12-05 01:01:03生肖徽章2007版:虎
日期:2008-12-10 07:47:462009新春纪念徽章
日期:2009-01-04 14:52:28数据库板块每日发贴之星
日期:2009-02-08 01:01:03生肖徽章2007版:蛇
日期:2009-03-09 22:18:532009日食纪念
日期:2009-07-22 09:30:00
81#
 楼主| 发表于 2022-11-28 23:11 | 只看该作者
〇〇 发表于 2022-11-28 21:09
把要猜的长方形改为任意4个格,也能用二分法吗

总共有C(25,4)=12650种,即使想分,也十分困难,验证也很困难,就没有趣味性了。

使用道具 举报

回复
论坛徽章:
520
奥运会纪念徽章:垒球
日期:2008-09-15 01:28:12生肖徽章2007版:鸡
日期:2008-11-17 23:40:58生肖徽章2007版:马
日期:2008-11-18 05:09:48数据库板块每日发贴之星
日期:2008-11-29 01:01:02数据库板块每日发贴之星
日期:2008-12-05 01:01:03生肖徽章2007版:虎
日期:2008-12-10 07:47:462009新春纪念徽章
日期:2009-01-04 14:52:28数据库板块每日发贴之星
日期:2009-02-08 01:01:03生肖徽章2007版:蛇
日期:2009-03-09 22:18:532009日食纪念
日期:2009-07-22 09:30:00
82#
 楼主| 发表于 2022-11-30 22:57 | 只看该作者
#7 PAWNS

Pawns will be placed on an 9xA square chess board. For any two columns if the number of rows with exactly one pawn should be at least 5, what can be the maximum number of columns (A) on this board?

Example for three columns:
(Columns a-b, a-c and b-c satisfy the conditions.)

棋子被放置在一个 9xA 方格的棋盘上。如果对于任意两列,至少有五行恰好有一个棋子,则该棋盘的最大列数 (A) 是多少?

三列的示例:(a-b、a-c 和 b-c 列满足条件。)

(图中a列的棋子所在行数为12469,b列1358, C列245789)

使用道具 举报

回复
论坛徽章:
520
奥运会纪念徽章:垒球
日期:2008-09-15 01:28:12生肖徽章2007版:鸡
日期:2008-11-17 23:40:58生肖徽章2007版:马
日期:2008-11-18 05:09:48数据库板块每日发贴之星
日期:2008-11-29 01:01:02数据库板块每日发贴之星
日期:2008-12-05 01:01:03生肖徽章2007版:虎
日期:2008-12-10 07:47:462009新春纪念徽章
日期:2009-01-04 14:52:28数据库板块每日发贴之星
日期:2009-02-08 01:01:03生肖徽章2007版:蛇
日期:2009-03-09 22:18:532009日食纪念
日期:2009-07-22 09:30:00
83#
 楼主| 发表于 2022-11-30 23:59 | 只看该作者
聪明的办法应该是反证法、抽屉原理之类的逻辑推理方法。
笨办法是用图论。棋盘总共9行,每列看作一个二进制数,这个数是 2^9=512 种的其中之一。
要求任何两个列的异或结果中,1的个数>=5。

问题转化为512个顶点的无向图中最大的连通子图有多少个顶点。

构造这个无向图所有的边:
with n as (select level-1 n from dual connect by level<=512)
    ,b as (select power(2,level-1) b from dual connect by level<=9)
select n1.n n1,n2.n n2
      ,count(*) cnt
  from n n1, n n2, b
where n1.n<n2.n
       and bitand(n1.n+n2.n-bitand(n1.n,n2.n*2),b.b)>0
group by n1.n ,n2.n
having count(*)>=5;

无向图的极大团、最大团(Bron-Kerbosch算法):
https://blog.csdn.net/yo_bc/article/details/77453478

使用道具 举报

回复
论坛徽章:
407
紫蛋头
日期:2012-05-21 10:19:41迷宫蛋
日期:2012-06-06 16:02:49奥运会纪念徽章:足球
日期:2012-06-29 15:30:06奥运会纪念徽章:排球
日期:2012-07-10 21:24:24鲜花蛋
日期:2012-07-16 15:24:59奥运会纪念徽章:拳击
日期:2012-08-07 10:54:50奥运会纪念徽章:羽毛球
日期:2012-08-21 15:55:33奥运会纪念徽章:蹦床
日期:2012-08-21 21:09:51奥运会纪念徽章:篮球
日期:2012-08-24 10:29:11奥运会纪念徽章:体操
日期:2012-09-07 16:40:00
84#
发表于 2022-12-3 11:04 | 只看该作者
bitand(n1.n,n2.n*2)为什么要*2

使用道具 举报

回复
论坛徽章:
520
奥运会纪念徽章:垒球
日期:2008-09-15 01:28:12生肖徽章2007版:鸡
日期:2008-11-17 23:40:58生肖徽章2007版:马
日期:2008-11-18 05:09:48数据库板块每日发贴之星
日期:2008-11-29 01:01:02数据库板块每日发贴之星
日期:2008-12-05 01:01:03生肖徽章2007版:虎
日期:2008-12-10 07:47:462009新春纪念徽章
日期:2009-01-04 14:52:28数据库板块每日发贴之星
日期:2009-02-08 01:01:03生肖徽章2007版:蛇
日期:2009-03-09 22:18:532009日食纪念
日期:2009-07-22 09:30:00
85#
 楼主| 发表于 2022-12-3 22:37 | 只看该作者
笔误,括号放错位置了,应该是:
and bitand(n1.n+n2.n-bitand(n1.n,n2.n)*2,b.b)>0
异或公式:
BITXOR(x,y) = BITOR(x,y) - BITAND(x,y) = (x + y) - BITAND(x, y) * 2;

使用道具 举报

回复
论坛徽章:
407
紫蛋头
日期:2012-05-21 10:19:41迷宫蛋
日期:2012-06-06 16:02:49奥运会纪念徽章:足球
日期:2012-06-29 15:30:06奥运会纪念徽章:排球
日期:2012-07-10 21:24:24鲜花蛋
日期:2012-07-16 15:24:59奥运会纪念徽章:拳击
日期:2012-08-07 10:54:50奥运会纪念徽章:羽毛球
日期:2012-08-21 15:55:33奥运会纪念徽章:蹦床
日期:2012-08-21 21:09:51奥运会纪念徽章:篮球
日期:2012-08-24 10:29:11奥运会纪念徽章:体操
日期:2012-09-07 16:40:00
86#
发表于 2022-12-4 08:00 来自手机 | 只看该作者
如果是每列三行,任意两列和为1的行大于等于二,那么肯定不能有8列,因为全1那列只能与全零或两个零的列组合才能符合要求,所以,有它就没有两个一的列

使用道具 举报

回复
论坛徽章:
407
紫蛋头
日期:2012-05-21 10:19:41迷宫蛋
日期:2012-06-06 16:02:49奥运会纪念徽章:足球
日期:2012-06-29 15:30:06奥运会纪念徽章:排球
日期:2012-07-10 21:24:24鲜花蛋
日期:2012-07-16 15:24:59奥运会纪念徽章:拳击
日期:2012-08-07 10:54:50奥运会纪念徽章:羽毛球
日期:2012-08-21 15:55:33奥运会纪念徽章:蹦床
日期:2012-08-21 21:09:51奥运会纪念徽章:篮球
日期:2012-08-24 10:29:11奥运会纪念徽章:体操
日期:2012-09-07 16:40:00
87#
发表于 2022-12-4 10:41 | 只看该作者
with recursive n as (select i-1 n from generate_series(1,8)t(i))
    ,b as (select 1<<(i-1) b from generate_series(1,3)t(i))
,p as(select n1.n n1,n2.n n2
      ,count(*) cnt
  from n n1, n n2, b
where n1.n<n2.n
       and xor(n1.n,n2.n)&b.b>0
group by n1.n ,n2.n
having count(*)>=2)
,t as(select 1 lv,[n1,n2]a from p
union all
select lv+1,a+[n] from t,n where (a[-1],n) in( select (n1,n2) from p q)and(a[-2],n) in( select (n1,n2) from p q)
)
select lv,a from t order by lv;

┌───────┬──────────────┐
│  lv   │      a       │
│ int32 │   int64[]    │
├───────┼──────────────┤
│     1 │ [1, 2]       │
│     1 │ [0, 3]       │
│     1 │ [1, 4]       │
│     1 │ [3, 4]       │
│     1 │ [0, 5]       │
│     1 │ [2, 5]       │
│     1 │ [1, 6]       │
│     1 │ [3, 6]       │
│     1 │ [5, 6]       │
│     1 │ [0, 7]       │
│     1 │ [2, 7]    │
│     1 │ [4, 7]    │
│     1 │ [2, 4]    │
│     1 │ [3, 5]    │
│     1 │ [0, 6]    │
│     1 │ [1, 7]    │
│     2 │ [1, 2, 4]    │
│     2 │ [0, 3, 5]    │
│     2 │ [0, 3, 6]    │
│     2 │ [0, 5, 6]    │
│     2 │ [3, 5, 6]    │
│     2 │ [1, 2, 7]    │
│     2 │ [1, 4, 7]    │
│     2 │ [2, 4, 7]    │
│     3 │ [0, 3, 5, 6] │
│     3 │ [1, 2, 4, 7] │
├───────┴──────────────┤
│  26 rows (20 shown)  │
└──────────────────────┘

使用道具 举报

回复
论坛徽章:
407
紫蛋头
日期:2012-05-21 10:19:41迷宫蛋
日期:2012-06-06 16:02:49奥运会纪念徽章:足球
日期:2012-06-29 15:30:06奥运会纪念徽章:排球
日期:2012-07-10 21:24:24鲜花蛋
日期:2012-07-16 15:24:59奥运会纪念徽章:拳击
日期:2012-08-07 10:54:50奥运会纪念徽章:羽毛球
日期:2012-08-21 15:55:33奥运会纪念徽章:蹦床
日期:2012-08-21 21:09:51奥运会纪念徽章:篮球
日期:2012-08-24 10:29:11奥运会纪念徽章:体操
日期:2012-09-07 16:40:00
88#
发表于 2022-12-4 10:44 | 只看该作者
其实漏写了lv=2时还要判断a[-3]条件,结果刚好满足,但这样写512是不可行的

使用道具 举报

回复
论坛徽章:
407
紫蛋头
日期:2012-05-21 10:19:41迷宫蛋
日期:2012-06-06 16:02:49奥运会纪念徽章:足球
日期:2012-06-29 15:30:06奥运会纪念徽章:排球
日期:2012-07-10 21:24:24鲜花蛋
日期:2012-07-16 15:24:59奥运会纪念徽章:拳击
日期:2012-08-07 10:54:50奥运会纪念徽章:羽毛球
日期:2012-08-21 15:55:33奥运会纪念徽章:蹦床
日期:2012-08-21 21:09:51奥运会纪念徽章:篮球
日期:2012-08-24 10:29:11奥运会纪念徽章:体操
日期:2012-09-07 16:40:00
89#
发表于 2022-12-4 11:45 | 只看该作者
〇〇 发表于 2022-12-4 10:44
其实漏写了lv=2时还要判断a[-3]条件,结果刚好满足,但这样写512是不可行的

postgresql居然支持在递归里写count(*)
with recursive n as (select i-1 n from generate_series(1,8)t(i))
    ,b as (select 1<<(i-1) b from generate_series(1,3)t(i))
,p as(select n1.n n1,n2.n n2
      ,count(*) cnt
  from n n1, n n2, b
where n1.n<n2.n
       and (n1.n#n2.n)&b.b>0
group by n1.n ,n2.n
having count(*)>=2)
,t as(select 1 lv,array[n1,n2]a from p
union all
select lv+1,a||array[n] from t,n where a[lv+1]<n and lv+1= ( select count(*) from p q, unnest(a) u(c) where n1=c and n2=n)
)
select lv,a from t order by lv;
lv |     a
----+-----------
  1 | {0,5}
  1 | {1,4}
  1 | {0,3}
  1 | {0,6}
  1 | {2,4}
  1 | {3,4}
  1 | {5,6}
  1 | {2,7}
  1 | {2,5}
  1 | {1,6}
  1 | {0,7}
  1 | {3,5}
  1 | {3,6}
  1 | {1,7}
  1 | {1,2}
  1 | {4,7}
  2 | {0,5,6}
  2 | {1,4,7}
  2 | {0,3,5}
  2 | {0,3,6}
  2 | {2,4,7}
  2 | {3,5,6}
  2 | {1,2,4}
  2 | {1,2,7}
  3 | {0,3,5,6}
  3 | {1,2,4,7}
(26 行记录)

使用道具 举报

回复
论坛徽章:
407
紫蛋头
日期:2012-05-21 10:19:41迷宫蛋
日期:2012-06-06 16:02:49奥运会纪念徽章:足球
日期:2012-06-29 15:30:06奥运会纪念徽章:排球
日期:2012-07-10 21:24:24鲜花蛋
日期:2012-07-16 15:24:59奥运会纪念徽章:拳击
日期:2012-08-07 10:54:50奥运会纪念徽章:羽毛球
日期:2012-08-21 15:55:33奥运会纪念徽章:蹦床
日期:2012-08-21 21:09:51奥运会纪念徽章:篮球
日期:2012-08-24 10:29:11奥运会纪念徽章:体操
日期:2012-09-07 16:40:00
90#
发表于 2022-12-4 12:18 | 只看该作者
6行还行,7行就算不动了
with recursive n as (select i-1 n from generate_series(1,128)t(i))
    ,b as (select 1<<(i-1) b from generate_series(1,7)t(i))
,p as(select n1.n n1,n2.n n2
      ,count(*) cnt
  from n n1, n n2, b
where n1.n<n2.n
       and (n1.n#n2.n)&b.b>0
group by n1.n ,n2.n
having count(*)>=4)
,t as(select 1 lv,array[n1,n2]a from p
union all
select lv+1,a||array[n] from t,n where a[lv+1]<n and lv+1= ( select count(*) from p q, unnest(a) u(c) where n1=c and n2=n)
)
--select lv,a from t order by lv;
select max(lv)+1 from t;
~~~~~~~~~~~~~~~~~
Cancel request sent
ERROR:  canceling statement due to user request
Time: 235055.623 ms (03:55.056)

----
with recursive n as (select i-1 n from generate_series(1,64)t(i))
    ,b as (select 1<<(i-1) b from generate_series(1,6)t(i))
,p as(select n1.n n1,n2.n n2
      ,count(*) cnt
  from n n1, n n2, b
where n1.n<n2.n
       and (n1.n#n2.n)&b.b>0
group by n1.n ,n2.n
having count(*)>=4)
,t as(select 1 lv,array[n1,n2]a from p
union all
select lv+1,a||array[n] from t,n where a[lv+1]<n and lv+1= ( select count(*) from p q, unnest(a) u(c) where n1=c and n2=n)
)
--select lv,a from t order by lv;
select max(lv)+1 from t;
?column?
----------
        4
(1 行记录)


Time: 4547.475 ms (00:04.547)

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表