楼主: 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
91#
 楼主| 发表于 2022-12-5 00:01 | 只看该作者
如果没有很好的推理结论做基础,一个SQL是不可能跑出来的。

使用道具 举报

回复
论坛徽章:
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
92#
发表于 2022-12-5 14:42 | 只看该作者
本帖最后由 〇〇 于 2022-12-5 14:50 编辑
newkid 发表于 2022-12-5 00:01
如果没有很好的推理结论做基础,一个SQL是不可能跑出来的。

发现一个规律,如果要求两两异或4个及以上1,那么选出来的4个数的异或结果为0

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 a from t where lv=3;


{13,16,38,59}
{13,16,42,55}
(240 行记录)


postgres=# select 13#16#42#55;
?column?
----------
        0
(1 行记录)


postgres=# select 13#16#42;
?column?
----------
       55
(1 行记录)


postgres=# select 13#16#38;
?column?
----------
       59
(1 行记录)
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 distinct a[1]#a[2]#a[3]#a[4] from t where lv=3;

?column?
----------
        0
(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
93#
发表于 2022-12-5 20:38 | 只看该作者
如果知道结果最大是4,可以这么验证
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 n1,n2,n1#n2 n3,cnt from p)
,t2 as(select t.*,t1.* from t,t t1 where t1.n1>t.n2 and t1.n3=t.n3
and (t.n1,t1.n1) in (select n1,n2 from p)
and (t.n1,t1.n2) in (select n1,n2 from p)
and (t.n2,t1.n1) in (select n1,n2 from p)
and (t.n2,t1.n2) in (select n1,n2 from p)
)
select * from t2 limit 4;

使用道具 举报

回复
论坛徽章:
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
94#
发表于 2022-12-5 20:49 | 只看该作者
本帖最后由 〇〇 于 2022-12-5 20:52 编辑

如果x在结果中,那么全1-x必定不能在结果中,因为xor(x,全1-x)=0,所以8个数最多取4个,在拼接时直接丢弃,比如二进制位为3时:有1 没 6 ,有3 没 4

使用道具 举报

回复
论坛徽章:
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
95#
 楼主| 发表于 2022-12-5 22:42 | 只看该作者
这是怎么回事:
xor(x,全1-x)=0

这个异或难道不应该全是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
96#
发表于 2022-12-5 23:52 来自手机 | 只看该作者
是全1,我错了,但是从结果看,确实是94楼那样互斥

使用道具 举报

回复
论坛徽章:
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
97#
发表于 2022-12-6 07:34 来自手机 | 只看该作者
同一个数,和全零异或得a,和全一异或得b,ab中必有一个数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
98#
发表于 2022-12-6 07:48 来自手机 | 只看该作者
因为数都是对称的,任取一个数开始,第二个数有2^(n-1)种选择,到第n-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
99#
发表于 2022-12-6 08:52 | 只看该作者
把数对压缩为正好有所需的1的,并且把98楼的思路加入,6行取4个,比90楼快了点
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 n>a[lv+1] and array_position(a,63-n)is null  and lv+1= ( select count(*) from p q, unnest(a) u(c) where n1=c and

n2=n)
)
select max(lv)+1 from t;
?column?
----------
        4
(1 行记录)


Time: 2871.206 ms (00:02.871)

使用道具 举报

回复
论坛徽章:
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
100#
发表于 2022-12-6 09:09 | 只看该作者
再把99楼第一步限制为0(根据对称性不影响结果总列数),就很快了
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, n1#n2 n3 from p where n1=0
union all
select lv+1,a||array[n],n3#n from t,n where n>a[lv+1] and n<>63-n3 and array_position(a,63-n)is null  and lv+1= ( select count(*) from p q, unnest(a) u

(c) where n1=c and n2=n)
)
select max(lv)+1 from t;

使用道具 举报

回复

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

本版积分规则 发表回复

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