1. 程式人生 > >CF - 1110 C Meaningless Operations

CF - 1110 C Meaningless Operations

alt pen sign -- push_back ons txt can bsp

題目傳送門

題解:

首先根據觀察,很容易發的是:

  x != (1<<k) - 1 時候 答案就是, 將x二進制下再最高位後的0都變成1。

然後就是考慮 x == (1<<k) - 1的時候

  同樣根據觀察可以得到 b ^ x = x - b, b&x = b

  所以就是將x拆成2個數, 然後這2個數的gcd最大。

  我們就最後找x的因子。 如 x = b * c

  那麽們就可以把2個數分成 c , (b-1) * c,gcd 為 c。

  或者 b , b * (c-1) gcd為b

代碼:

技術分享圖片
/*
code by: zstu wxk
time: 2019/02/08
*/ #include<bits/stdc++.h> using namespace std; #define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout); #define LL long long #define ULL unsigned LL #define fi first #define se second #define pb push_back #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define lch(x) tr[x].son[0] #define
rch(x) tr[x].son[1] #define max3(a,b,c) max(a,max(b,c)) #define min3(a,b,c) min(a,min(b,c)) typedef pair<int,int> pll; const int inf = 0x3f3f3f3f; const int _inf = 0xc0c0c0c0; const LL INF = 0x3f3f3f3f3f3f3f3f; const LL _INF = 0xc0c0c0c0c0c0c0c0; const LL mod = (int)1e9+7; const int N = 1e5 + 100; int q, n; int
ans[(1<<25) + 100]; int _pow[N]; int cal(int x){ int ret = 1; for(int i = 2; i * i <= x; ++i){ if(x%i == 0){ ret = max({ret, i, x/i}); } } return ret; } void init(){ ans[2] = 3; ans[3] = 1; int t = 8; for(int i = 3; i <= 25; ++i){ int m = t - 1, z = t - 2; while(!ans[z]){ ans[z] = m; --z; } ans[m] = cal(m); t *= 2; } } void Ac(){ for(int i = 1; i <= q; ++i){ scanf("%d", &n); printf("%d\n", ans[n]); } } int main(){ init(); while(~scanf("%d", &q)){ Ac(); } return 0; }
View Code

  

CF - 1110 C Meaningless Operations