首页 > 其他 > 详细

CF1110C Meaningless Operations

时间:2019-02-10 00:05:12      阅读:209      评论:0      收藏:0      [点我收藏+]

思路:

令x为满足2<= a的最大的x。如果a的二进制表示中包含0,则将b构造为(2x+1 - 1) ^ a即可;否则gcd(a ^ b, a & b) = gcd(2x+1 - 1 - b, b) = gcd(2x+1 - 1, b),要令此式最大,b应为(2x+1 - 1)的最大非平凡因子。

实现:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 inline int max_fac(int x)
 5 {
 6     for (int i = 2; i * i <= x; i++)
 7     {
 8         if (x % i == 0) return x / i;
 9     }
10     return 1;
11 }
12 
13 int main()
14 {
15     int q, x;
16     set<int> st;
17     for (int i = 1; i <= 25; i++) st.insert((1 << i) - 1);
18     while (cin >> q)
19     {
20         for (int i = 0; i < q; i++)
21         {
22             cin >> x;
23             if (st.count(x))
24             {
25                 cout << max_fac(x) << endl;   
26             }
27             else
28             {
29                 int tmp = 0, y = x;
30                 while (y) { y >>= 1; tmp++; }
31                 cout << (1 << tmp) - 1 << endl; 
32             }
33         }
34     }
35     return 0;
36 }

 

CF1110C Meaningless Operations

原文:https://www.cnblogs.com/wangyiming/p/10358530.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!