【九度OJ】题目1442:A sequence of numbers 解题报告
【九度OJ】题目1442:A sequence of numbers 解题报告
标签(空格分隔): 九度OJ
原题地址: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
解题方法
这个题的意思就是这个数列可能是等差数列也是等比数列,自己判断,然后找出第k个数。
等差比较好计算,等比数列求解要用到二分求幂。
#include<stdio.h>
#define M 200907
long long fun(long long a, long long d, long long k) {
k--;//公式
long long ans = a;//等比初项
while (k != 0) {
if (k % 2 == 1) {
ans *= d;
ans %= M;//不超过M
}
k /= 2;
d *= d;
d %= M;//
}
return ans;
}
int main() {
int n;
while (scanf("%d", &n) != EOF) {
while (n-- != 0) {
long long answer = 0;
long long a, b, c;
int k;
scanf("%lld%lld%lld%d", &a, &b, &c, &k);
if (2 * b == a + c) {
long long dis = b - a;
answer = (a % M) + (((k - 1) % M) * (dis % M) % M) % M;//等差
} else {
long long dis = b / a;
answer = fun(a, dis, k);//等比,二分求幂
}
printf("%lld\n", answer);
}
}
return 0;
}
Date
2017 年 3 月 8 日