式子推对了样例也过了,结果全 WA 爆 0,调代码 1h+ 本人已崩溃 QAQ
算法:BSGS
#include<iostream>
#include<unordered_map>
#include<cmath>
using namespace std;
#define int long long
int gcd(int a, int b) { return (b == 0? a : gcd(b, a%b)); }
int exgcd(int a, int b, int &x, int &y)
{
if(b == 0)
{
x = 1, y = 0;
return a;
}
int d, x1, y1;
d = exgcd(b, a%b, x1, y1);
x = y1, y = x1 - a/b*y1;
return d;
}
int BSGS(int a, int b, int p)
{
unordered_map <int, int> hash;
int am = 1, ami = 1, baj = b;
int M = ceil(sqrt(p));
for(int j = 0; j < M; j ++)
hash[baj] = j, baj = (baj * a) % p;
for(int i = 1; i <= M; i ++)
am = (am * a) % p;
for(int i = 1; i <= M; i ++)
{
ami = (ami * am) % p;
if(hash.count(ami))
return i * M - hash[ami];
}
return -1;
}
signed main()
{
int T;
cin >> T;
while(T --)
{
int p, a, b, x1, t;
int d, x, y, ans;
int T, xx;
cin >> p >> a >> b >> x1 >> t;
if(x1 == t)
{
cout << "1\n";
continue;
}
if(a == 1)
{
d = exgcd(b, p, x, y);
ans = (t-x1) * x + 1;
cout << (ans == p? ans : ans%p) << '\n';
continue;
}
if(a == 0 || x1 == 0)
{
if(b == t)
cout << "2\n";
else
cout << "-1\n";
continue;
}
if(b == 0)
{
if( (a*x1) % p == t )
cout << "2\n";
else
cout << "-1\n";
continue;
}
exgcd(a-1, p, x, y);
T = ( (x1 + b * x) % p + p ) % p;
exgcd(T, p, xx, y);
ans = BSGS(a, (((t + b * x) * xx) % p + p ) % p , p);
if(ans == -1)
cout << -1 << '\n';
else
cout << ans + 1 << '\n';
}
return 0;
}