Root
Time Limit: 30000/15000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 34 Accepted Submission(s): 6
For each case, each case contains two integers sum,m(1≤sum≤100000000,1≤m≤100000) in the first line.
The next m lines will contains two intgeers xi,yi(0≤xi,yi≤1000000000)
Each line cotain a integer which is the smallest integer for (xi,yi) ,if we can't find such a integer just output "-1" without quote.
175 =5^2∗7
2^0 mod 5 = 1
2^3 mod 7 = 1
So the answer to (2,1) is 0
比较经典一道扩展欧几里得
现在,我们首先来解决下原根的问题:简单的解释可以参考:>>原根<<
资源下载:http://download.csdn.net/detail/u010579068/8993383
不急看懂的,可以先去切道 定义题 链接:1135 原根
解题 http://www.cnblogs.com/yuyixingkong/p/4722821.html
转载请注明出处:寻找&星空の孩子
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<map>
using namespace std;
typedef long long ll;
const int maxn=1e6+;
const int maxv=1e5+;
bool isnp[maxv];
int prime[maxv],pnum;//素数数组,素数个数
int cas;
void get_prime()//素数打表
{
pnum=;
int i,j;
memset(isnp,,sizeof(isnp));
isnp[]=isnp[]=true;
for(i=; i<maxv; i++)
{
if(!isnp[i])prime[pnum++]=i;
for(j=; j<pnum&&prime[j]*i<maxv; j++)
{
isnp[i*prime[j]]=true;
if(i%prime[j]==)break;
}
}
}
ll qukpow(ll k,ll base,ll p)
{
ll res=;
for(; k; k>>=)
{
if(k&)res=(res*base)%p;
base=(base*base)%p;
}
return res;
}
ll ppow(ll a,ll b,ll mod)
{
ll c=;
while(b)
{
if(b&) c=c*a%mod;
b>>=;
a=a*a%mod;
}
return c;
}
ll fpr[maxv]; ll find_primitive_root(ll p)//求p的原根 g^(p-1) = 1 (mod p); 求g
{
ll cnt=,num=p-,res;
int i;
if(p==)return ;
for(i=; i<pnum && prime[i]*prime[i]<=num && num> ; i++)
{
if(num%prime[i]==)//
{
fpr[cnt++]=prime[i];
while(num%prime[i]==)num/=prime[i];
}
}
if(num>)fpr[cnt++]=num;//fpr[]存的是p-1的因子
for(res=; res<=p-; res++)//暴力
{
for(i=; i<cnt; i++)
if(ppow(res,p/prime[i],p)==)break;
if(i>=cnt)return res;
}
return -;
}; const int mod=1e6+; struct solve
{
struct HashTable
{
int top,head[mod];
struct Node
{
int x,y,next;
} node[mod];
void init()
{
top=;
memset(head,,sizeof(head));
}
void insert(ll x,ll y)
{
node[top].x=x;
node[top].y=y;
node[top].next=head[x%mod];
head[x%mod]=top++;
}
ll query(ll x)
{
for(int tx=head[x%mod]; tx; tx=node[tx].next)
if(node[tx].x==x)return node[tx].y;
return -;
}
} mp; ll p;
ll discretelog(ll prt,ll a) //取对数
{
ll res,am=ppow(prt,maxn-,p),inv=ppow(a,p-,p),x=;
for(ll i=maxn-;; i+=(maxn-))
{
if((res=mp.query((x=x*am%p)*inv%p))!=-)
{ return i-res;
}
if(i>p)break;
}
return -;
}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y)//扩展欧几里得 x为最后需要的k
{
if(!b)
{
d=a;
x=;
y=;
}
else
{
ex_gcd(b,a%b,d,y,x);
y-=x*(a/b);
}
} ll proot;
void init()
{
mp.init();
ll tmp,x,y,d;
int i;
proot=find_primitive_root(p);//找到素数p的原根
for(i=,tmp=; i<maxn-; i++,tmp=tmp*proot%p)
mp.insert(tmp%p,i*1ll);
}
ll query(ll x,ll y)
{
ll d;
x%=p;
y%=p; if(y==)return ;
else if(x==)
{
if(y==)return ;
else return -;
}
else if(y==)return -;
else
{
ll s=discretelog(proot,x); ll t=discretelog(proot,y); ex_gcd(s,p-,d,x,y);
if(t%d)return -;
else
{
ll dx=(p-)/d;
x=(x%dx+dx)%dx;
x*=(t/d);
x=(x%dx+dx)%dx;
return x;
}
}
}
} sol[];
int main()
{
int i,j,q,con,T;
ll sum,x,y;
scanf("%d",&T);
get_prime();
cas=;
while(cas<=T)
{
con=;
scanf("%I64d %d",&sum,&q); for(i=; i<pnum&&prime[i]*prime[i]<=sum&&sum!=; i++)
{
if(sum%prime[i]==)//素数存起来
{
sol[con].p=prime[i];
sol[con].init();
con++;
while(sum%prime[i]==)sum/=prime[i];
}
}
if(sum>)
{
sol[con].p=sum;
sol[con].init();
con++;
} printf("Case #%d:\n",cas++); for(i=; i<q; i++)
{
scanf("%lld %lld",&x,&y); ll res=1e18,tmp;
for(j=; j<con; j++)
{ tmp=sol[j].query(x,y);
if(tmp!=-)res=min(res,tmp);
}
if(res==1e18)res=-;
printf("%I64d\n",res);
}
}
return ;
}