您的位置:首页 > 其它

(2017多校训练第三场)HDU - 6063 RXD and math 找规律 + 快速幂

2017-08-02 16:47 651 查看
刚开始一直执着于推公式,最后放弃了,打表发现规律是n^k,写个快速幂即可。不过要注意快速幂中的a是long long int类型的,所以要先取模一下。

代码如下:

#include <bits/stdc++.h>

using namespace std;

typedef long long int LL;
const int MOD = 1e9 + 7;
const int MAX_N = 5e5 + 5;
const int INF = 0x3f3f3f3f;

LL mod_pow(LL a, LL n, LL mod)
{
LL res = 1;
a %= mod; // a要先取模
while (n)
{
if (n & 1)
res = res * a % mod;
a = a * a % mod; // 如果a不先取模,这里会爆long long int
n >>= 1;
}
return res;
}

int main()
{
//freopen("test.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
cin.sync_with_stdio(false);
LL n, k;
int Case = 1;
while (cin >> n >> k)
cout << "Case #" << Case++ << ": " << mod_pow(n, k, MOD) << endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息