A sequence of numbers(九度教程第 58 题)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_37053885/article/details/88046204

A sequence of numbers(九度教程第 58 题)

时间限制:1 秒 内存限制:32 兆 特殊判题:否

1.题目描述:

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

2.基本思路

输入的序列可能为等差序列有可能为等比序列,根据等差等比的通项公式求第k项即可。由于该题没有找到评测平台,在小规模的数据上面没问题,但最后不知道会不会溢出,尤其是pow操作很有可能会溢出。

3.代码实现

#include <iostream>
#include <math.h>

#define N 3

using namespace std;

long long buf[N];

int main()
{
    int n,k;
    scanf("%d",&n);
    for(int t=0;t<n;t++){
        for(int i=0;i<N;i++)
            scanf("%lld",&buf[i]);
        scanf("%d",&k);
        if(buf[2]-buf[1]==buf[1]-buf[0]){
            int d = buf[1]-buf[0];
            printf("%lld\n",buf[0]+(long)d*(k-1));
        }
        else{
            int q = buf[1]/buf[0];
            printf("%lld\n",buf[0]*(long)pow(q,(k-1)));
        }

    }
    return 0;
}
/*
2
1 2 3 5
1 2 4 5
*/

猜你喜欢

转载自blog.csdn.net/qq_37053885/article/details/88046204