(2017多校训练第三场)HDU

来源:互联网 发布:华夏免费版数据库 编辑:程序博客网 时间:2024/05/18 01:32

刚开始一直执着于推公式,最后放弃了,打表发现规律是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;}


原创粉丝点击