2242: [SDOI2011]計算器
Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 4058 Solved: 1579
[Submit][Status][Discuss]
Description
Input
輸入包含多組數據。
Output
Sample Input
3 1
2 1 3
2 2 3
2 3 3
【樣例輸入2】
3 2
2 1 3
2 2 3
2 3 3
【數據規模和約定】
對於100%的數據,1<=y,z,p<=10^9,爲質數,1<=T<=10。
Sample Output
2
1
2
【樣例輸出2】
2
1
0
HINT
Source
#include<iostream>
#include<stdio.h>
#include<cmath>
#include<string.h>
#include<map>
using namespace std;
int T,k;
int flag=0;
long long aa,bb,p;
map<int,int> mp;
long long cheng(long long a,long long b,long long p)
{
long long ans=1;
while(b>0)
{
if(b&1) ans=ans*a%p;
a=a*a%p;
b>>=1;
}
return ans;
}
void solve1()
{
while(T--)
{
scanf("%lld%lld%lld",&aa,&bb,&p);
long long ans=cheng(aa,bb,p);
printf("%lld\n",ans);
}
return;
}
void ex_gcd(long long a,long long b,long long &d,long long &x,long long &y)
{
if(b==0)
{
x=1,y=0;
d=a;
return ;
}
ex_gcd(b,a%b,d,x,y);
long long x0=x;
x=y;
y=x0-(a/b)*y;
}
void solve2()
{
while(T--)
{
long long x,y,d;
scanf("%lld%lld%lld",&aa,&bb,&p);
ex_gcd(aa,p,d,x,y);
if(bb%d!=0)
{
printf("Orz, I cannot find x!\n");
continue;
}
x=x*(bb/d)%p;
x=(x%(p/d)+p/d)%(p/d);
printf("%lld\n",x);
}
}
void solve3()
{
while(T--)
{
flag=0;
long long x,y,d;
scanf("%lld%lld%lld",&aa,&bb,&p);
aa%=p;
if(!aa&&!bb)
{
printf("1\n");
continue;
}
if(!aa)
{
printf("Orz, I cannot find x!\n");
continue;
}
mp.clear();
long long m = ceil(sqrt(p)),t = 1;
mp[1]=m+1;
for(long long i=1;i<m;i++)
{
t=t*aa%p;
if(!mp[t]) mp[t]=i;
}
long long tmp=cheng(aa,p-m-1,p),ine=1;
for(long long k=0;k<m;k++)
{
int i=mp[bb*ine%p];
if(i)
{
flag=1;
if(i==m+1) i=0;
printf("%lld\n",k*m+i);
break;
}
ine=ine*tmp%p;
}
if(!flag)
printf("Orz, I cannot find x!\n");
}
}
int main()
{
scanf("%d%d",&T,&k);
if(k==1) solve1();
if(k==2) solve2();
if(k==3) solve3();
return 0;
}