Codeforces Global Round 1 C. Meaningless Operations 数学

C. Meaningless Operations

在这里插入图片描述

code

/*Siberian Squirrel*/
/*Cute JinFish*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const double PI = acos(-1), eps = 1e-8;
/*const int MOD = 998244353, r = 119, k = 23, g = 3;
const int MOD = 1004535809, r = 479, k = 21, g = 3;*/
const int INF = 0x3f3f3f3f, MOD = 1e9 + 7;
const int M = 1e7 + 10, N = 2e6 + 10;
int sgn(double x) {
    
    
    if(fabs(x) < eps) return 0;
    return x < 0? -1: 1;
}
//inline int rnd(){static int seed=2333;return seed=(((seed*666666ll+20050818)%998244353)^1000000007)%1004535809;}
//double Rand() {return (double)rand() / RAND_MAX;}

int n;
int prime[N], cnt;
bool vis[N];

void init() {
    
    
    for(int i = 2; i < N; ++ i) {
    
    
        if(!vis[i]) prime[++ cnt] = i;
        for(int j = 1; j <= cnt && prime[j] * i < N; ++ j) {
    
    
            vis[i * prime[j]] = true;
            if(i % prime[j] == 0) break;
        }
    }
}

void solve(ll res = 0, ll temp = 1) {
    
    
    while(temp <= n) {
    
    res ++; temp <<= 1;}
    if(temp - 1 != n) cout << temp - 1 << endl;
    else {
    
    
        for(int i = 1; i <= res; ++ i) {
    
    
            if(n % prime[i] == 0) {
    
    
                cout << n / prime[i] << endl;
                return;
            }
        }
        cout << 1 << endl;
    }
    return;
}


int main() {
    
    
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(nullptr);
// srand(time(0));
#ifdef ACM_LOCAL
    freopen("input", "r", stdin);
    freopen("output", "w", stdout);
#endif
    init();
    int o = 1;
	cin >> o;
    while(o --) {
    
    
        cin >> n;
        solve();
    }
    return 0;
}

Can the greatest common divisor and bitwise operations have anything in common? It is time to answer this question.

Suppose you are given a positive integer a
. You want to choose some integer b from 1 to a−1 inclusive in such a way that the greatest common divisor (GCD) of integers a⊕b and a&b

is as large as possible. In other words, you’d like to compute the following function:

f(a)=max0<b<agcd(a⊕b,a&b).

Here ⊕
denotes the bitwise XOR operation, and &

denotes the bitwise AND operation.

The greatest common divisor of two integers x
and y is the largest integer g such that both x and y are divided by g

without remainder.

You are given q
integers a1,a2,…,aq. For each of these integers compute the largest possible value of the greatest common divisor (when b

is chosen optimally).
Input

The first line contains an integer q
(1≤q≤103

) — the number of integers you need to compute the answer for.

After that q
integers are given, one per line: a1,a2,…,aq (2≤ai≤225−1

) — the integers you need to compute the answer for.
Output

For each integer, print the answer in the same order as the integers are given in input.

猜你喜欢

转载自blog.csdn.net/qq_46173805/article/details/115244025