TAKE # 10c

Began to solve some problems left over by history, it seems like eighty-nine question, is estimated to make up a lot of time.

First, we find that it is easy to find x such that minimum x (x + 1)% (2 n-) == 0
then x and x + 1 are relatively prime,

May wish to make x = ax, x + 1 = by,
is to find by-ax = 1

In addition also to ensure that this number can be divisible by 2n, so each quality factor we enumerate n violence is to give back to the front or

Note that when a time equal to y, y-1 must be equal to the back

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
void exgcd(ll a,ll b,ll& d,ll& x,ll& y) {
    if (!b) {
        d = a;
        x = 1;
        y = 0;
    } else {
        exgcd(b, a % b, d, y, x);
        y -= x * (a / b);
    }
}
const int N = 1e6+5;
ll vis[N],b[N],ind=0;
void init(){
    for(int i=2;i<N;i++){
        if(vis[i])continue;
        b[ind++]=i;
        for(int j=i+i;j<N;j+=i){
            vis[j]=1;
        }

    }
}
int t;ll n;
ll p[20],c[20],cnt=0;
int main(){
    ios::sync_with_stdio(false);
    init();
    cin>>t;
    while (t--){
        cin>>n;cnt=0;
        n*=2;
        for(int i=0;i<ind&&b[i]*b[i]<=n;i++){
            if(n%b[i]==0){
                p[cnt]=1;
                while (n%b[i]==0){
                    n/=b[i];
                    p[cnt]*=b[i];
                }
                cnt++;
            }
        }
        ll a,b,d,x,y;
        ll ans = 1e18;
        if(n)p[cnt]=n,c[cnt]=1,cnt++;
        for(int i=0;i<(1<<cnt);i++){
            a=1,b=1;
            for(int j=0;j<cnt;j++){
                if(i>>j&1){
                    a*=p[j];
                }else{
                    b*=p[j];
                }
            }
            if(a==1){ans = min(ans,b-1);}
            else {
                exgcd(a,-b,d,x,y);
                if(d==1) {
                    y = (y % a + a) % a;
                    ans = min(ans, y * b);
                }
            }
        }
        cout<<ans<<endl;
    }
}

Guess you like

Origin www.cnblogs.com/MXang/p/11479374.html