[关闭]
@Chilling 2016-08-11T15:28:54.000000Z 字数 1299 阅读 918

HDU-2817: A sequence of numbers

数论


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项(快速幂)。注意要取模。


  1. #include<stdio.h>
  2. #define LL long long
  3. #define MOD 200907
  4. LL quick(LL x,LL m,LL n)
  5. {
  6. LL ans=1;
  7. while(m>0)
  8. {
  9. if(m%2==1)
  10. ans=ans*x%n;
  11. x=x*x%n;
  12. m=m/2;
  13. }
  14. return ans;
  15. }
  16. int main()
  17. {
  18. LL n,a,b,c,k;
  19. while(scanf("%lld",&n)!=EOF)
  20. {
  21. while(n--)
  22. {
  23. scanf("%lld%lld%lld%lld",&a,&b,&c,&k);
  24. if(c-b==b-a)
  25. printf("%lld\n",(a%MOD+((k-1)%MOD*(b-a)%MOD)%MOD)%MOD);
  26. else if(c/b==b/a)
  27. {
  28. LL ans=quick(b/a,k-1,MOD);
  29. ans=((a%MOD)*ans)%MOD;
  30. printf("%lld\n",ans);
  31. }
  32. }
  33. }
  34. return 0;
  35. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注