dreamstart的催促

dreamstart的催促

链接:https://ac.nowcoder.com/acm/contest/322/A
来源:牛客网
 

题目描述

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

聪明的你可以帮助他们吗?

输入描述:

第一行有一个整数n,n <= 1e5

接下来一行有n个数,每个数的大小不超过1e16

输出描述:

输出取模之后的和

示例1

输入

4
1 6 9 12

输出

21502

解题思路:快速幂解法,普通的for循环会超时,另外,每次乘后要进行取余,
否则有可能会数据相乘的时候过大 

代码如下

#include<stdio.h>

#include<math.h>

#include<string.h>

#include<algorithm>

using namespace std;

int main()
{
	long long int a, b, c = 1, sum = 0;
	
	scanf("%lld", &a);
	
	for(int i = 1; i <= a; i++)
	{
		scanf("%lld", &b);
		
		int j = i;
		
		while(j)
		{
			if(j % 2)
			c = c * b % 10000019;
			
			b = b * b % 10000019;
			
			j = j / 2;
		}
		sum = sum + c;
		
		c = 1;
		
		sum = sum % 10000019;
	}
	
	printf("%lld\n",sum);
}

猜你喜欢

转载自blog.csdn.net/w__000000wbt/article/details/85621887