Summer Training day4 欧拉降幂

来源:互联网 发布:php走势图源码 编辑:程序博客网 时间:2024/06/06 03:39

Input
2
Output
2         
Hint
1. For N = 2, S(1) = S(2) = 1.2. The input file consists of multiple test cases.
Sample Input
2
Sample Output
2



这道题的公式非常简单,就是求2^(N-1) %1e9+7

由于N实在是太大了,不能直接求快速幂,考虑到2^x % MOD是有循环节的,因此使用欧拉降幂公式a^x ≡a^(x modϕ(p)+ϕ(p)) (mod p)

 

#include <iostream>#include <cstdio>#include <cstring>using namespace std;typedef long long int64; int64 MOD = 1e9 + 6;char str[200007];int64 mod_pow(int64 x,int64 n){int64 ans = 1;while(n){if(n & 1){ans  = (ans * x) % (MOD + 1);}x = (x * x) % (MOD + 1);n >>= 1;}return ans % (MOD + 1);}int main(){while(cin>>str) {int len = strlen(str);int64 mod = 0;for(int i = 0; i < len;i++){mod *= 10;mod += str[i] - '0';mod %= MOD;}mod = (mod - 1)%MOD;printf("%lld\n",mod_pow(2,mod + MOD) );}return 0;}


原创粉丝点击