hdu 2837 坑题。
Calculation
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1414 Accepted Submission(s): 291
Problem Description
Assume that f(0) = 1 and 0^0=1. f(n) = (n%10)^f(n/10) for all n bigger than zero. Please calculate f(n)%m. (2 ≤ n , m ≤ 10^9, x^y means the y th power of x).
Input
The first line contains a single positive integer T. which is the number of test cases. T lines follows.Each case consists of one line containing two positive integers n and m.
Output
One integer indicating the value of f(n)%m.
Sample Input
2
24 20
25 20
Sample Output
16
5
Source
1 /** 2 a ^ b % c= a ^ ( b % phi ( c ) + phi ( c ) ) % c 条件:(b>=c) phi(n)为n的欧拉函数值 3 我想说(⊙o⊙)…,完全搞不懂标程 4 **/ 5 #include<iostream> 6 #include<stdio.h> 7 #include<cstring> 8 #include<cstdlib> 9 using namespace std; 10 typedef __int64 LL; 11 12 LL Euler(LL n) 13 { 14 LL temp =n,i; 15 for(i=2;i*i<=n;i++) 16 { 17 if(n%i==0) 18 { 19 while(n%i==0) 20 n=n/i; 21 temp=temp/i*(i-1); 22 } 23 } 24 if(n!=1) temp=temp/n*(n-1); 25 return temp; 26 } 27 LL pow_mod(LL a,LL b,LL p){ 28 LL ans=1; 29 while(b) 30 { 31 if(b&1) ans=(ans*a)%p; 32 b=b>>1; 33 a=(a*a)%p; 34 } 35 return ans; 36 } 37 LL fuck(LL a,LL n,LL m) 38 { 39 LL i,ans=1; 40 for(i=1;i<=n;i++) 41 { 42 ans=ans*a; 43 if(ans>=m) return ans; 44 } 45 return ans; 46 } 47 LL dfs(LL n,LL m){ 48 LL ans,p,nima; 49 if(n==0) return 1; 50 if(n<10) return n; 51 p = Euler(m); 52 ans=dfs(n/10,p); 53 54 nima = fuck(n%10,ans,m); 55 if(nima>=m){ 56 ans=pow_mod(n%10,ans%p+p,m); 57 if(ans==0) return m; 58 } 59 else{ 60 ans=pow_mod(n%10,ans,m); 61 } 62 return ans; 63 } 64 int main() 65 { 66 int T; 67 LL n,m; 68 scanf("%d",&T); 69 while(T--){ 70 scanf("%I64d%I64d",&n,&m); 71 printf("%I64d\n",dfs(n,m)%m); 72 } 73 return 0; 74 }