您的位置:首页 > 产品设计 > UI/UE

A sequence of numbers

2015-11-16 17:51 543 查看
题目1442:A sequence of numbers

时间限制:1 秒

内存限制:128 兆

特殊判题:否

提交:2298

解决:541

题目描述:

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


arithmetic sequences:等差数列

geometric sequences:等比数列

代码如下:

#include <stdio.h>

#define M 200907
long long geoSeq(long long a, long long q, int k)
{
long long ans = a;
k -= 1;
while (k)
{
if (k & 1){
ans *= q;
ans %= M;
}
q *= q;
q %= M;
k >>= 1;
}
return ans;
}
long long arithSeq(long long a, long long d, int k)
{
int ans = a % M + ((k - 1)*d) % M;
return ans %= M;
}
int main()
{
int n;
scanf("%d", &n);
while (n--)
{
long long a, b, c;
int k;
scanf("%lld%lld%lld%d", &a, &b, &c, &k);

long long ans;
if (a*c == b*b)
ans = geoSeq(a, c / b, k);
else
ans = arithSeq(a, c - b, k);
printf("%lld\n", ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: