题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2817
题目描述:
Problem Description
Xinlv wrote some sequences on the paper a long time ago, they might be arithmetic or geometric sequences. The numbers are not very clear now, and only the first three numbers of each sequence are recognizable. Xinlv wants to know some numbers in these sequences, and he needs your help.
Input
The first line contains an integer N, indicting that there are N sequences. Each of the following N lines contain four integers. The first three indicating the first three numbers of the sequence, and the last one is K, indicating that we want to know the K-th numbers of the sequence.
You can assume 0 < K <= 10^9, and the other three numbers are in the range [0, 2^63). All the numbers of the sequences are integers. And the sequences are non-decreasing.
Output
Output one line for each test case, that is, the K-th number module (%) 200907.
Sample Input
2 1 2 3 5 1 2 4 5
Sample Output
5 16
题目分析:
快速幂取模大法,节省时间
AC代码:
/*
快速幂取模
by_Superxd
*/
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#define MOD 200907
#define LL long long
using namespace std;
LL a,b,c,k,n;
LL quckmod(LL x,LL y)//快速幂取模x^y
{
LL cnt=1;
while(y)
{
if(y%2==1)
{
cnt*=x;
cnt%=MOD;
}
x*=x;
x%=MOD;
y/=2;
}
return cnt;
}
int main()
{
scanf("%lld",&n);
while(n--)
{
scanf("%lld%lld%lld%lld",&a,&b,&c,&k);
if(a+c==2*b)//等差数列
{
//公差是b-a,有an=a1+(n-1)*k
printf("%lld\n",(a%MOD+(((k-1)%MOD)*((b-a)%MOD))%MOD)%MOD);
}
else//等比数列
{
//公比是b/a,有an=a1*(b/a)^(n-1);
printf("%lld\n",((a%MOD)*(quckmod(b/a,k-1)%MOD))%MOD);
}
}
return 0;
}