标签:ash div puts isp name 方法 lang while http
https://www.luogu.com.cn/problem/P3846
\(BSGS\)可以\(O( \sqrt{p} )\)处理:
处理方法十分常规
\(C++ Code:\)
#include<iostream>
#include<cstdio>
#include<cmath>
#include<map>
#define ll long long
using namespace std;
ll p,b,n;
map<ll,int>a;
ll ksm(ll x,ll y)
{
ll ans=1;
while (y)
{
if (y&1)
ans=ans*x%p;
x=x*x%p;
y >>=1;
}
return ans;
}
int main()
{
scanf("%lld%lld%lld",&p,&b,&n);
if (n==1)
{
printf("0\n");
return 0;
}
int g=(int)sqrt(p)+1;
ll c=n%p;
for (int i=0;i<g;i++)
{
a[c]=i+1;
c=c*b%p;
}
ll y=ksm(b,g);
ll e=1;
for (int i=1;i<=g;i++)
{
e=e*y%p;
if (a[e])
{
printf("%lld\n",(ll)i*g-(ll)(a[e]-1));
return 0;
}
}
puts("no solution");
return 0;
}
Luogu3846 [TJOI2007] 可爱的质数/【模板】BSGS
标签:ash div puts isp name 方法 lang while http
原文地址:https://www.cnblogs.com/GK0328/p/13362627.html