#include <utility>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdio>
#include <iostream>
#include <iomanip>
#include <set>
#include <vector>
#include <cmath>
#include <queue>
#include <bitset>
#include <map>
#include <iterator>
using namespace std;
#define clr(a,v) memset(a,v,sizeof(a))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int INF = 0x7f7f7f7f;
const int maxn = 100009;
const double pi = acos(-1.0);
const double eps = 1e-10;
const int mod = 1000000007;
typedef long long LL;
typedef pair<int, int> pii;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<VVI> VVVI;
bitset<maxn + 100> vis;
int p[maxn], size;
int MOD[30] = { 100000000, 40000000, 16000000, 6400000, 2560000, 1024000,
409600, 163840, 65536, 32768, 16384, 8192, 4096, 2048, 1024, 512, 256,
128, 64, 32, 16, 8, 4, 2, 1, };
LL POW(LL a, LL b, int mod) {
LL res = 1;
a%=mod;
while (b) {
if (b & 1)
res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return res;
}
bool cmp(LL a, int k, int m) {
LL res = 1;
for (int i = 0; i < k && res < m; ++i)
res *= a;
return res >= m;
}
LL a;
LL dfs(int n, int m, int dep) {
if (n == 0) {
return 1 % m;
}
int mod = dep > 24 ? 1 : MOD[dep];
LL pv = dfs(n - 1, mod, dep + 1);
if(!pv)
pv+=mod;
LL res = POW(a, pv, m);
//cout << "res " << res << " pv " << mod << endl;
res = res + cmp(a, pv, m) * m;
return res;
}
int main() {
ios::sync_with_stdio(false);
//init();
int k;
while (cin >> a >> k) {
if (!a) {
if (k & 1)
cout << 0 << endl;
else
cout << 1 << endl;
continue;
}
cout << dfs(k, MOD[0], 1) % MOD[0] << endl;
}
return 0;
}
HDU 3609 (指数循环)
最新推荐文章于 2019-03-10 12:35:31 发布