题意:
1、给定y、z、p,计算y^z mod p 的值;
2、给定y、z、p,计算满足xy ≡z(mod p)的最小非负整数x;
3、给定y、z、p,计算满足y^x ≡z(mod p)的最小非负整数x。
思路:
第一问是裸的快速幂
第二问,因为P是质数,所以求一下乘法逆元再乘z就行了,特判y是p的倍数时无解
第三问,bsgs模板
#include<iostream>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstring>
#include<vector>
#include<cstdio>
#include<stack>
#include<map>
#define ll long long
using namespace std;
ll power(ll a,ll b,ll p)
{
ll ans=1;
while(b)
{
if(b&1)
ans=(ans*a)%p;
a=(a*a)%p;
b>>=1;
}
return ans;
}
int main()
{
int t,k;
cin>>t>>k;
while(t--)
{
map<ll,int>mp;
ll y,z,p;
cin>>y>>z>>p;
if(k==1)
{
cout<<power(y,z,p)<<endl;
}
if(k==2)
{
if(y%p==0)
{
cout<<"Orz, I cannot find x!"<<endl;
continue;
}
cout<<(z*power(y,p-2,p))%p<<endl;
}
if(k==3)
{
if(y%p==0)
{
cout<<"Orz, I cannot find x!"<<endl;
continue;
}
ll ans;
ll m=ceil(sqrt(p));
for(int i=0;i<=m;i++)
{
if(i==0)
{
ans=z%p;
mp[ans]=i;
continue;
}
ans=(ans*y)%p;
mp[ans]=i;
}
int flag=0;
ans=1;
ll t=power(y,m,p);
for(int i=1;i<=m;i++)
{
ans=(ans*t)%p;
if(mp[ans])
{
ll x=i*m-mp[ans];
x=(x%p+p)%p;
cout<<x<<endl;
flag=1;
break;
}
}
if(!flag)
cout<<"Orz, I cannot find x!"<<endl;
}
}
return 0;
}