comet#10 C
Posted 163467wyj
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了comet#10 C相关的知识,希望对你有一定的参考价值。
//枚举 2n 的质因子a ,2n/a = b, ap - bq = 1, 找到 bq(min) #include<bits/stdc++.h> const int MA = 1e6+100; typedef long long ll; int t, vis[MA],pri[MA],prinum,cnt; ll n,fac[50]; using namespace std; void exgcd(ll a, ll b, ll& d, ll& x, ll& y) if(!b) x = 1, y = 0; d = a; else exgcd(b, a%b, d, y, x); y -= x*(a/b); void init()// 素数表 for(int i = 2; i <= MA-100; i++) if(!vis[i]) pri[++prinum] = i; for(int j = 1; j <= prinum && pri[j]*i <= MA-100; j++) vis[pri[j]*i] = 1; if(i % pri[j] == 0) break; ll calc(ll a, ll b) if(b == 1) return a-1; ll d,x,y; exgcd(a, b, d, x, y); //x = (x%b+b)%b; if(a*x <= 1) x += b;// bq >= 0, so ap=bq+1 ap >=1 通解x=x0+kb, y = y0-ka return a*x-1; ll dfs(ll num, ll a, ll b) if(num > cnt) return calc(a, b); return min(dfs(num+1, a*fac[num], b), dfs(num+1, a, b*fac[num])); int main() init(); cin>>t; while(t--) cin>>n; n <<= 1; cnt = 0; for(int i = 1; i <= prinum; i++) if(n % pri[i]) continue; fac[++cnt] = 1; while(n%pri[i] == 0) n /= pri[i]; fac[cnt] *= pri[i]; if(n > 1) fac[++cnt] = n; ll ans = dfs(1,1,1); cout<< ans <<endl; return 0; /* 5 2 4 6 8 10 样例输出 1 3 7 3 15 4 */
以上是关于comet#10 C的主要内容,如果未能解决你的问题,请参考以下文章