題目1442:A sequence of numbers 九度OJ
時間限制:1 秒
記憶體限制:128 兆
特殊判題:否
提交:4160
解決:1059
- 題目描述:
-
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.
- 輸入:
-
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 one line for each test case, that is, the K-th number module (%) 200907.
- 樣例輸入:
-
2 1 2 3 5 1 2 4 5
- 樣例輸出:
-
5 16
//題目1442:題中所給的序列為算術幾何序列,也就是等差或者等比序列 //所以,在讀入序列之後,利用序列前三個數判斷該序列是等比序列還是等差序列。 //據說直接判斷是否為等差序列比較省時間。如果該序列不是等差序列 ,則必定是等比序列 // 等差序列利用公式即可求得------an=a1+(n-1)*d //等比序列,遵照二分求冪的原則可求得---------an=a1*q^(n-1) //You can assume 0 < K <= 10^9, and the other three numbers are in the range [0, 2^63) //關於題中變數 大小的 設定 ,另外三個數都是2^63 -1,說明需要用 long long 來儲存 // 輸出 取模 Output one line for each test case, that is, the K-th number module (%) 200907. #include <cstdio> #define ret 200907 int main(){ int n; while(scanf("%d",&n)!=EOF){ while(n--){ long long a,b,c,k; long long ans; scanf("%lld%lld%lld%lld",&a,&b,&c,&k); //判斷是等差還是等比 if((b-a)==(c-b)){//是等差數列 ans=a%ret+(((k-1)%ret)*((b-a)%ret))%ret;//對此處數值的範圍 不是很明瞭 ,在哪裡取模才正確 }else{ //是等比數列 --------二分求冪 long long q=b/a; long long m=k-1; ans=1; while(m!=0){ if(m%2==1){ ans=(ans*q)%ret; } m=m/2; q=(q*q)%ret; } ans=((a%ret)*ans)%ret; } printf("%lld\n",ans); } } return 0; }