CF1110C Meaningless Operations
阿新 • • 發佈:2019-02-10
否則 out mean f11 滿足 bsp div 思路 names
思路:
令x為滿足2x <= 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