@Chilling
2016-08-11T15:28:54.000000Z
字数 1299
阅读 930
数论
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 , and the other three numbers are in the range . 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
题意:输入t,代表测试组数。接下来每组测试数据,一行,四个数字,前三个代表这个序列的前三个数,第四个数字是要求的第几位数。求等比数列或者等差数列的第k项。
分析:首先判断是等差数列还是等比数列。然后等差数列求第k项或者等比数列求第k项(快速幂)。注意要取模。
#include<stdio.h>
#define LL long long
#define MOD 200907
LL quick(LL x,LL m,LL n)
{
LL ans=1;
while(m>0)
{
if(m%2==1)
ans=ans*x%n;
x=x*x%n;
m=m/2;
}
return ans;
}
int main()
{
LL n,a,b,c,k;
while(scanf("%lld",&n)!=EOF)
{
while(n--)
{
scanf("%lld%lld%lld%lld",&a,&b,&c,&k);
if(c-b==b-a)
printf("%lld\n",(a%MOD+((k-1)%MOD*(b-a)%MOD)%MOD)%MOD);
else if(c/b==b/a)
{
LL ans=quick(b/a,k-1,MOD);
ans=((a%MOD)*ans)%MOD;
printf("%lld\n",ans);
}
}
}
return 0;
}