hdu2817 A sequence of numbers

这题就是判断是等差数列还是等比数列,然后计算结果mod200907  

因为数字比较大10的九次方

所以等比用到了快速幂求模

不懂可以看看算法导论,在大数那里有讲


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int modPow(__int64 x,__int64 y) //计算x的y次方求余        快速幂不懂可以百度
{
    __int64  res=1,a=x;
    while (y>0)
	{
		if(y&1) 
		{
			res=(res*a)%200907;
		}
		a=(a*a)%200907;
		y>>=1;
    }
	return res;
}


int main()
{
	__int64 k,ans,ca,n,a,b,c,bi;
	scanf("%I64d",&n);
	while(n--)
	{
		scanf("%I64d%I64d%I64d%I64d",&a,&b,&c,&k);
		if((c-b)==(b-a))
		{
			ca=b-a;
		ans=(((a%200907+((k-1)%200907)*(ca%200907)))%200907)%200907;
		//	ans=(a+(k-1)*ca)%200907;
		}
		else if(a*c==b*b)
		{
			bi=(c/b);
			ans=(a*modPow(bi,k-1))%200907;
		}
		printf("%I64d
",ans);
	}
	return 0;
}






原文地址:https://www.cnblogs.com/fuhaots2009/p/3455415.html