标签:cpp cti clu img com name ati map using
已知\(f(0)=1,f(n)=(n\%10)^{f(n/10)}\),求\(f(n)\mod m\)
由扩展欧拉定理可知:当\(b>=m\)时,\(a^b\equiv a^{b\%\varphi(m)+\varphi(m)}\mod m\),那么我们可以通过这个式子直接去递归求解。
在递归的时候每次给下一个的模数都是\(phi(mod)\),那么我们求出来之后,怎么知道要不要再加\(phi(m)\)?
给出一个判定方法,若\(a^b>=m\),那么需要改为\(a^{b+phi(m)}\)。
其实也没看懂。
#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;
ll euler(ll n){
ll 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;
}
ll ppow(ll a, ll b, ll mod){
ll ret = 1;
while(b){
if(b & 1) ret = ret * a % mod;
a = a * a % mod;
b >>= 1;
}
return ret;
}
ll check(ll a, ll b, ll m){
ll ret = 1;
for(int i = 0; i < b; i++){
ret = ret * a;
if(ret >= m) return ret;
}
return ret;
}
ll f(ll a, ll mod){
if(a == 0) return 1 % mod;
ll phm = euler(mod);
ll b = f(a / 10, phm);
ll ans = check(a % 10, b, mod);
if(ans >= mod){
ans = ppow(a % 10, b + phm, mod);
return ans == 0? mod : ans;
}
else return ans;
}
int main(){
int T;
scanf("%d", &T);
while(T--){
ll n, m;
scanf("%lld%lld", &n, &m);
printf("%lld\n", f(n, m) % m); //这里要取模
}
return 0;
}
标签:cpp cti clu img com name ati map using
原文地址:https://www.cnblogs.com/KirinSB/p/11517956.html