HDU2837 Calculation (index loop section) solution to a problem

Meaning of the questions:

Known \ (F (0) =. 1, F (n-) = (n-\% 10) ^ {F (n-/ 10)} \) , seeking \ (f (n) \ mod m \)

Ideas:

Euler's theorem, the extended: when (b> = m \) \ time, \ (A ^ B \ equiv A ^ B {\% \ varphi (m) + \ varphi (m)} \ MOD m \) , then we can go directly through the recursive solution to this equation.
In recursion to the next each time the module is \ (Phi (MOD) \) , then after we find out, how do you know whether or not combined with \ (Phi (m) \) ?
A criterion is given, if the \ (A ^ B> = m \) , then the need to \ (A + B ^ {Phi (m)} \) .
In fact, I did not understand.

Code:


#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;
}

Guess you like

Origin www.cnblogs.com/KirinSB/p/11517956.html
Recommended