http://ac.jobdu.com/problem.php?pid=1442
-
题目描述:
-
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
只有等比数列和等差数列。。。数据太大了。。。等差数列直接用公式求。。。等比数列用二分求幂法。。。
#include<iostream>
#include<fstream>
#include<math.h>
using namespace std;
#define M 200907
long long DengBi(long long q, int k, long long n1){ // 等比数列
long long result = n1;
k--;
while (k != 0){
if (k % 2 == 1){
result *= q;
result %= M;
}
k /= 2;
q *= q;
q %= M;
}
return result;
}
int main()
{
int N, k;
long long n1, n2, n3, result, d, q;
//ifstream cin("data.txt");
while (cin >> N)
{
for (; N > 0; N--)
{
cin >> n1 >> n2 >> n3 >> k;
if (n2 - n1 == n3 - n2) //等差数列
{
d = n2 - n1;
result = ((((k - 1) % M)*(d % M)) % M + n1 % M) % M;
}
else if (n2 / n1 == n3 / n2)//等比数列
{
q = n2 / n1;
result = DengBi( q, k, n1);
}
cout << result << endl;
}
}
system("pause");
return 0;
}