@description@

给定一个长度为 n 的正整数序列 a1, a2, ..., an。

考虑建一张 n 个点的图。假如 ai AND aj ≠ 0,则在 i, j 之间连无向边。

求在这张图上的最小环。

Input

第一行一个整数 n 表示序列长度 (1≤n≤10^5)

第二行包含 n 个整数 a1,a2,…,an (0≤ai≤10^18)。

Output

如果图中不含任何环,输出 -1。

否则输出最小环长度。

Examples

Input

4

3 6 28 9

Output

4

Input

5

5 12 9 16 48

Output

3

Input

4

1 2 4 8

Output

-1

Note

第一个样例答案为 (9,3,6,28)。

第二个样例答案为 (5,12,9)。

第三个样例没有环。

@solution@

其实是一道很 sb 的题。。。

考虑假如某个二进制位上存在至少三个数该位为 1,则存在一个长度为 3 的环,显然该环最小。

因为最多有 60 个二进制位,每个位上存在最多 2 个数该位为 1 才有考虑的价值。

而在这种情况,因为非 0 的元素总会占一个二进制位的 1,所以最多会有 120 个非 0 元素;而为 0 的元素就是个孤立点,不需要管它。

所以直接当非 0 的点数 < 120(代码中写的是 300比较方便copy模板)时才用 Floyd 跑最小环,否则直接输出 3。

@accepted code@

  1. #include<cstdio>
  2. #include<algorithm>
  3. using namespace std;
  4. typedef long long ll;
  5. const int MAXN = 300;
  6. int G[MAXN + 5][MAXN + 5], A[MAXN + 5][MAXN + 5];
  7. ll a[MAXN + 5];
  8. int n, cnt;
  9. int main() {
  10. scanf("%d", &n);
  11. for(int i=1;i<=n;i++) {
  12. ll x; scanf("%lld", &x);
  13. if( x ) a[++cnt] = x;
  14. if( cnt > MAXN ) {
  15. puts("3");
  16. return 0;
  17. }
  18. }
  19. for(int i=1;i<=cnt;i++)
  20. for(int j=1;j<=cnt;j++)
  21. if( a[i] & a[j] ) A[i][j] = G[i][j] = 1;
  22. else A[i][j] = G[i][j] = MAXN + 5;
  23. int ans = MAXN + 5;
  24. for(int k=1;k<=cnt;k++) {
  25. for(int i=1;i<k;i++)
  26. for(int j=i+1;j<k;j++)
  27. ans = min(ans, A[i][k] + A[k][j] + G[i][j]);
  28. for(int i=1;i<=cnt;i++)
  29. for(int j=1;j<=cnt;j++)
  30. G[i][j] = min(G[i][k] + G[k][j], G[i][j]);
  31. }
  32. if( ans == MAXN + 5 ) puts("-1");
  33. else printf("%d\n", ans);
  34. }

@details@

所以,我至今不知道为什么我当时会卡在这种 sb 题上面。。。

@codeforces - 1205B@ Shortest Cycle的更多相关文章

  1. [Codeforces 1205B]Shortest Cycle(最小环)

    [Codeforces 1205B]Shortest Cycle(最小环) 题面 给出n个正整数\(a_i\),若\(a_i \& a_j \neq 0\),则连边\((i,j)\)(注意i- ...

  2. Codeforces 1206 D - Shortest Cycle

    D - Shortest Cycle 思路:n大于某个值肯定有个三元环,否则floyd找最小环. 代码: #pragma GCC optimize(2) #pragma GCC optimize(3) ...

  3. Codeforces Round #580 (Div. 2)-D. Shortest Cycle(思维建图+dfs找最小环)

    You are given nn integer numbers a1,a2,…,ana1,a2,…,an. Consider graph on nn nodes, in which nodes ii ...

  4. CF 1206D - Shortest Cycle Floyd求最小环

    Shortest Cycle 题意 有n(n <= 100000)个数字,两个数字间取&运算结果大于0的话连一条边.问图中的最小环. 思路 可以发现当非0数的个数很大,比如大于200时, ...

  5. D. Shortest Cycle(floyd最小环)

    D. Shortest Cycle time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  6. D. Shortest Cycle

    D. Shortest Cycle A[i]&A[j]!=0连边, 求图中最小环 N>128 时必有3环 其他暴力跑 folyd最小环 #include<bits/stdc++.h ...

  7. codeforces 962F.simple cycle(tarjan/点双连通分量)

    题目连接:http://codeforces.com/contest/962/problem/F 题目大意是定义一个simple cycle为从一个节点开始绕环走一遍能经过simple cycle内任 ...

  8. Codeforces 938G Shortest Path Queries [分治,线性基,并查集]

    洛谷 Codeforces 分治的题目,或者说分治的思想,是非常灵活多变的. 所以对我这种智商低的选手特别不友好 脑子不好使怎么办?多做题吧-- 前置知识 线性基是你必须会的,不然这题不可做. 推荐再 ...

  9. Codeforces 845G Shortest Path Problem?

    http://codeforces.com/problemset/problem/845/G 从顶点1dfs全图,遇到环则增加一种备选方案,环上的环不需要走到前一个环上作为条件,因为走完第二个环可以从 ...

随机推荐

  1. ACM-ICPC 2019 西安邀请赛 D.Miku and Generals(二分图+可行性背包)

    “Miku is matchless in the world!” As everyone knows, Nakano Miku is interested in Japanese generals, ...

  2. eclipse中部署web项目

    因为eclipse是免费的,所以很多企业都会选用eclipse作为开发工作,那么我们就需要熟练使用eclipse部署web项目. 第一步:选择window -> preferences ,选择s ...

  3. OSGi教程:Class Space Consistency

    此教程基于OSGi Core Release 7 OSGi类空间的一致性 详细内容上面英文教程有详细解答 下面主要是一些个人见解,若有不当之处,欢迎指出: "Class space cons ...

  4. Least Common Multiple (最小公倍数,先除再乘)

      思路: 求第一个和第二个元素的最小公倍数,然后拿求得的最小公倍数和第三个元素求最小公倍数,继续下去,直到没有元素 注意:通过最大公约数求最小公倍数的时候,先除再乘,避免溢出   #include ...

  5. SQL SERVER 2008 R2 插入数据非常慢

    表是5字段int类型,第一个字段是主健,自增字段 表结构: id int  Uncheckedbillno bigint  Uncheckedopid int  Checkedbillopid int ...

  6. 实现手机网页调起原生微信朋友圈分享的工具nativeShare.js

    http://www.liaoxiansheng.cn/?p=294 我们知道现在我们无法直接通过js直接跳转到微信和QQ等软件进行分享,但是现在像UC浏览器和QQ浏览器这样的主流浏览器自带一个分享工 ...

  7. op应用:官方,wifidog,portal,uci,luci,脚本,框架,usb

    http://wiki.openwrt.org/doc/starthttp://downloads.openwrt.org/docs/buildroot-documentation.htmlhttp: ...

  8. ES6 中字符串的扩展

    1. 字符的Unicode表示法 JavaScript允许采用 \uxxxx 形式表示一个字符,其中 xxxx 表示字符的 Unicode 码点. "\u0061" // 表示小写 ...

  9. LintCode刷题笔记--Flip Bits

    Flip Bits: 标签:位运算 题目:Determine the number of bits required to flip if you want to convert integer n  ...

  10. JavaEE架构简介与JavaWeb新特性

    Fragment 将一个web应用做成几个部分,然后整合 创建Fragment项目   然后打包放入Servlet项目中的WEB-INF下的lib中 注解 @WebServlet @WebServle ...