题意:
询问有多少数\(n\)满足\(n^{n!}\equiv b\mod p \land\ n\in[1,M]\),数据范围:\(M\leq2^{64}-1,p\leq1e5\)
思路:
这题显然要用欧拉降幂,\(n!\)小于\(\varphi(p)\)的直接暴力算,\(n!\neq 0\mod \varphi(p)\)也直接暴力。
\(n!\equiv 0\mod \varphi(p)\)显然这时质数恒为\(\varphi(p)\),由鸽笼定理得:
当\(x\)是常数时,\(1^x,2^x,\dots,n^x,\dots\mod p\)有循环节为\(\varphi(p)\)
那么直接按循环节搞一下即可。
注意一下,当\(b=0,M=2^{64}-1,p=1\)时,答案爆\(long long\)。
代码:
#include<map>
#include<set>
#include<queue>
#include<cmath>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<cstring>
#include<sstream>
#include<iostream>
#include<algorithm>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const int maxn = 1e5 + 5;
const int MAXM = 3e6;
const ll MOD = 998244353;
const ull seed = 131;
const int INF = 0x3f3f3f3f;
ull euler(ull n){
ull res = n, a = n;
for(int i = 2; i * i <= a; i++){
if(a % i == 0){
res = res / i * (i - 1);
while(a % i == 0) a/= i;
}
}
if(a > 1) res = res / a * (a - 1);
return res;
}
ull ppow(ull a, ull b, ull mod){
ull ret = 1;
while(b){
if(b & 1) ret = ret * a % mod;
a = a * a % mod;
b >>= 1;
}
return ret;
}
ull rec[maxn];
int main(){
int T, ca = 1;
scanf("%d", &T);
while(T--){
ull b, p, m;
scanf("%I64u%I64u%I64u", &b, &p, &m);
if(b == 0 && p == 1){
if(m == 18446744073709551615ULL)
printf("Case #%d: 18446744073709551616\n", ca++);
else
printf("Case #%d: %I64u\n", ca++, m + 1);
continue;
}
ull phi = euler(p);
ull ans = 0, fac = 1;
ull i = 1;
if(b == 0) ans++;
for(i = 1; i <= m; i++){
if(fac * i >= phi) break;
fac = fac * i;
if(ppow(i, fac, p) == b) ans++;
}
for(; i <= m; i++){
if(fac * i % phi == 0) break;
fac = fac * i % phi;
if(ppow(i, fac + phi, p) == b) ans++;
}
if(i <= m){
ull cnt = 0;
for(int j = 1; j <= p; j++){
rec[j] = ppow(j, phi, p);
if(rec[j] == b) cnt++;
}
for(; i <= p && i <= m; i++){
if(rec[i] == b) ans++;
}
if(i <= m){
ull rest = m - p;
ans += rest / p * cnt;
rest -= rest / p * p;
for(i = 1; i <= rest; i++){
if(rec[i] == b) ans++;
}
}
}
printf("Case #%d: %I64u\n", ca++, ans);
}
return 0;
}