求2^n%mod的值, n<=10^100000。
费马小定理 如果a, p 互质, 那么a^(p-1) = 1(mod p) 然后可以推出来a^k % p = a^(k%(p-1))%p。
#include <iostream> #include <vector> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <map> #include <set> #include <string> #include <queue> #include <stack> #include <bitset> using namespace std; #define pb(x) push_back(x) #define ll long long #define mk(x, y) make_pair(x, y) #define lson l, m, rt<<1 #define mem(a) memset(a, 0, sizeof(a)) #define rson m+1, r, rt<<1|1 #define mem1(a) memset(a, -1, sizeof(a)) #define mem2(a) memset(a, 0x3f, sizeof(a)) #define rep(i, n, a) for(int i = a; i<n; i++) #define fi first #define se second typedef pair<int, int> pll; const double PI = acos(-1.0); const double eps = 1e-8; const int mod = 1e9+7; const int inf = 1061109567; const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} }; ll pow(ll a, ll b) { ll ret = 1; while(b) { if(b&1) ret = ret*a%mod; a = a*a%mod; b>>=1; } return ret; } int main() { string s; while(cin>>s) { int len = s.size(); ll num = 0; for(int i = 0; i<len; i++) { num = (num*10+s[i]-'0')%(mod-1); } num--; num = (num+mod-1)%(mod-1); ll ans = pow(2LL, num)%mod; cout<<ans<<endl; } return 0; }