1. 程式人生 > >HDU4704(SummerTrainingDay04-A 歐拉降冪公式)

HDU4704(SummerTrainingDay04-A 歐拉降冪公式)

sample ++ sum using log ret cep content cstring

Sum

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 3245 Accepted Submission(s): 1332


Problem Description

技術分享

Sample Input

2

Sample Output

2

Hint

1. For N = 2, S(1) = S(2) = 1. 2. The input file consists of multiple test cases.

Source

2013 Multi-University Training Contest 10 模型最終轉換為求2^(b-1) mod (1e9+7),根據費馬小定理可得1e9+7的歐拉函數為1e9+6。根據歐拉降冪公式a^b = a^(b%phi(MOD)+phi(MOD)) mod MOD,用快速冪算出a^(b%phi(MOD)+phi(MOD))即為答案。
 1 //2017-08-04
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <iostream>
 5 #include <algorithm>
 6
#define ll long long 7 8 using namespace std; 9 10 const int N = 100010; 11 const int MOD = 1000000007; 12 char str[N]; 13 14 ll quick_pow(ll a, ll n){//快速冪 15 ll ans = 1; 16 while(n){ 17 if(n&1)ans = ans*a%MOD; 18 a = a*a%MOD; 19 n>>=1; 20 } 21 return ans; 22
} 23 24 int main() 25 { 26 while(scanf("%s", str)!=EOF){ 27 ll num = 0; 28 for(int i = 0; i < strlen(str); i++){//歐拉降冪 29 num *= 10; 30 num += str[i]-0; 31 num %= (MOD-1); 32 } 33 num -= 1; 34 printf("%lld\n", quick_pow(2, num)); 35 } 36 37 return 0; 38 }

HDU4704(SummerTrainingDay04-A 歐拉降冪公式)