CF1110C Meaningless Operations

思路:

令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 }

猜你喜欢

转载自www.cnblogs.com/wangyiming/p/10358530.html