Welcome to __xiong 's blog: http://blog.csdn.net/acmore_xiong?viewmode=list |
calculation time limit:2000/1000 MS ( java/others) memory limit:32768/32768 K (java/others) Total submission (s): 1912 accepted Submission (s): 413 Link: Click me problem descriptionassume that F (0) = 1 and 0^0=1. F (n) = (n%10) ^f (N/10) for all n bigger than zero. Calculate f (N)%m. (2≤n, m≤10^9, x^y means the y th power of x). inputthe first line contains a single pos Itive integer T. Which is the number of test cases. T lines follows. Each case consists of one line containing the positive integers n and m. outputone integer indicating the value of f ( N)%m. sample Input 224 2025 sample output |
Test Instructions:
Known f (0) = 1,0^0 =1,"Note that0 of the other any other side is 0, although the problem is not directly given ~", Also known as f (n) = (n%10) ^f (N/10), let you ask f (n)%m. (2≤n, m≤10^9)
Analysis:
To solve a recursive,f (n) recursion will need to be power multiple times, so that we can use exponential link to the power of processing, the formula, such as Phi (i) the Euler function value of I, in fact, the exponential cycle is Euler functions + a combination of the fast power, in this problem need to pay special attention to the 0 the processing, seeking 0 the multiple parties need to be judged.
There is a way to power down in my other blog on the detailed explanation, please refer to the "Csust-super fast Power" Fermat theorem "+" fast power to take the model ", traced to the truth, the cost pony theorem is a expansion of Euler theorem ~ for more number-theoretic knowledge, please refer to the A supplementary explanation of the basis of number theory ~
implementation code:
#include <set> #include <cmath> #include <cstdio> #include <string> #include <cstring># Include <iostream> #include <algorithm>using namespace std; #define FIN freopen ("Input.txt", "R", Stdi N) #define FOUT freopen ("Output.txt", "w", stdout) #define CASE (T) for (scanf ("%d", &t); t--;) typedef __int64 LL;INT T; LL N, M, P; ll Phi (ll N) {ll ans = n; for (LL i = 2; I * I <= n; i++) {if (n% i = = 0) {ans-= ans/i; while (n% i = = 0) n/= i; }} if (n > 1) ans-= ans/n; return ans;} Iteration Form//ll Pow_mod (ll A, ll P, ll MoD)//{//ll ans = 1;//if (a = = 0) Return p = = 0;//while (p)//{//I F (P & 1)//{//ans = ans * A% mod;//if (ans = = 0) ans = mod;//}//a = A * A% mod;//p >>= 1;//}//return ans;//}ll Pow_mod (ll A, ll B, ll MoD) {if (b = = 0) return 1; if (a = = 0) return 0; LLx, ans; x = Pow_mod (A, b >> 1, MoD); Ans = x * x% MOD; if (b & 1) ans = (a * ans)% MOD; if (ans = = 0) ans = MOD; return ans;} LL f (int n,int m) {if (n <) return n; int x = f (N/10,phi (M)), ans; Ans = pow_mod (n% 10,x,m); return ans;} int main () {#ifndef Online_judge FIN, #endif//Online_judge case (T) {scanf ("%i64d%i64d", &n, &m); LL ans = f (n,m); printf ("%i64d\n", ans% M); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 2837 Calculation "Euler function, fast exponential circulation section"