Comet OJ - Contest # 10 C- fish big splash (extended GCD + violence enumeration)

Topic links:

https://www.cometoj.com/contest/65/problem/C

Meaning of the questions:

Find the minimum of the first n items and such items and X of the front mod n is equal to 0 give

Ideas:

This equation will then see occur (n + 1) * n / 2% X = 0, i.e., (n + 1) * n% 2X = 0; associated with the remainder of such congruence equation or the like. Of course, also think exgcd () demand-related issues

equation ax + ny = b the smallest positive integer solution 
ax≡b (mod n) inversing principle (congruence equation) 
AX + by = GCD (A, B) (A, B solution prime only)

Provided n + 1 = ap, n = bq, simultaneous equations to give: qp - bq = 1, the minimum required bq solutions of classical Euclidean expansion problems.
Equation has a solution conditions must be met gcd (a, b) == 1 , it is decomposed by the number of unique decomposition theorem, and in order to satisfy all prime i.e. taking power as a product of all prime factors.
code:

#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#define IOS ios::sync_with_stdio(0); cin.tie(0);
#define mp make_pair
#define Accept 0
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
const double Pi = acos(-1.0);
 Const  Double ESP = 1E9 ;
 const  int INF = 0x3f3f3f3f ;
 const  int MAXN = 1E5 + . 7 ;
 const  int MAXM = 1e6 + . 7 ;
 const  int MOD = 1E9 + . 7 ;
 const  int maxL = 1e6 + . 7 ; 

int Prim [maxL];
 int CNT; 
LL FAC [maxL]; 
int VIS [maxL];
 int CUR; 
LL MX; // maximum number of the prime number power factor 

int getPrime () {
     int i ,j;
    int cnt = 0;
    memset(vis,0,sizeof(0));
    vis[0] = vis[1] = 1;
    for(i=2;i<=MAXL;++i)
    {
        if(!vis[i]) vis[i]= prim[cnt++]= i;
        for(j=0;j<cnt&&i*prim[j]<=MAXL;++j){
            vis[i*prim[j]] = 1;
            if(i%prim[j]==0) break;
        }
    }
    return cnt;
}
ll exgcd(ll a, ll b, ll &x, ll &y) {
    if(b == 0)
    {
        x = 1;
        y = 0;
        return a;
    }
    ll d = exgcd(b,a%b,x,y),temp = x;
    x = y;
    y = temp-a/b*y;
    return d;
}
int getFactor(ll n,int cnt){
    int cur = 0;
    for(int i=0;i<cnt;i++){
        if(n==1) break;
        if(n%prim[i]==0){
            fac[cur] = 1;
            while(n%prim[i]==0){
                n /= prim[i];
                fac[cur] *= prim[i];
            }
            cur++;
            mx = max(mx,fac[cur]);
        }
    }
    if(n>1) fac[cur++] =n;returnCUR; 
} 

LL Solve (n-LL) { 
        LL ANS = n-<< . 1 ;
     // there are so mutually prime prime factors of a plurality of possible composition 
    for ( int I = . 1 ; I <( . 1 << (CUR)); I ++ ) { 
        LL A = . 1 , B;
         for ( int J = 0 ; J <(CUR); J ++ ) {
             IF (I & ( . 1 << J)) A * = FAC [J]; 
        } 
        B = 2 * n-/ A; 
        LL X, Y; 
        exgcd (A, B, X, Y); 
        ANS=min(ans,min(-((x%b+b)%b-b)*a,-((y%a+a)%a-a)*b));
//        exgcd(a,b,x,y);
//        ll xx=x,yy=y;
//        if(x>0){
//            x = xx%b;
//            y += (xx-x)/b*a;
//        }else{
//            y = yy%a;
//            x += (yy-y)/a*b;
//        }
//        ans = min(ans,min(abs(a*x),abs(b*y)));    
    }
    return ans;
}
int main(){
    int T;
    cnt = getPrime();
    cin>>T;
     // set x + 1 = ap, x = bq; bq = available. 1-AP;
     // minimal solution bq evaluates, exgcd (); 
    the while (T-- ) { 
        LL n-; Scanf ( " % LLD " , & n-); 
        n- << = . 1 ; 
        CUR = getFactor (n-, CNT); 
        LL ANS = Solve (n-/ 2 ); 
        the printf ( " % LLD \ n- " , ANS); 
    } 
}

 

Guess you like

Origin www.cnblogs.com/Tianwell/p/11482380.html