HDU4704(SummerTrainingDay04-A 欧拉降幂公式)
Posted Penn000
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU4704(SummerTrainingDay04-A 欧拉降幂公式)相关的知识,希望对你有一定的参考价值。
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
模型最终转换为求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 欧拉降幂公式)的主要内容,如果未能解决你的问题,请参考以下文章
HDU5037(SummerTrainingDay01-C)
HDU 4704 Sum(隔板原理+组合数求和公式+费马小定理+快速幂)
HDU4417(SummerTrainingDay08-N 主席树)