牛客网 - dreamstart的催促(快速幂)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lzyws739307453/article/details/85469908

题目链接:https://ac.nowcoder.com/acm/contest/322/A
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld

题目描述 

有一天集训队的学弟们正在计算一堆数,但是dreamstart感觉他们算的太慢了,就让他们坐在一起想出一个快速计算的方法,但是由于他们一时想不出来,想让你帮助他们。他们说现在有一个数列,要算出第 i 个数的 i 次幂并且把每个数计算出来的值加到一起,最后答案模10000019。
聪明的你可以帮助他们吗?

输入描述:

第一行有一个整数n,n <= 1e5
接下来一行有n个数,每个数的大小不超过1e16

输出描述:

输出取模之后的和

输入

4
1 6 9 12

输出

21502

解题思路

快速幂跑一遍。。。

#include <iostream>
#define MOD 10000019
using namespace std;
typedef long long LL;
LL edge(LL a, LL b)
{
    LL ans = 1;
    while (b)
    {
        if (b & 1)
            ans = (ans * a) % MOD;
        a = (a * a) % MOD;
        b >>= 1;
    }
    return ans;
}
int main()
{
    int n;
    LL ans = 0, a;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
    {
        scanf("%lld", &a);
        a = a % MOD;
        ans = (ans + edge(a, i)) % MOD;
    }
    printf("%lld\n", ans);
}

猜你喜欢

转载自blog.csdn.net/lzyws739307453/article/details/85469908
今日推荐