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

Hdu 2917 A sequence of numbers

2016-01-28 16:21 435 查看


A sequence of numbers

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 4457 Accepted Submission(s): 1401



Problem Description

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.



Input

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

Output one line for each test case, that is, the K-th number module (%) 200907.



Sample Input

2
1 2 3 5
1 2 4 5




Sample Output

5
16




Source

2009 Multi-University Training Contest 1 - Host
by TJU


思路:简单题目,先判断是等比数列还是等差数列,等比数列用快速幂求出q^(n-1),等差数列直接用公式
AC代码如下:
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
#define LL long long
#define MOD 200907

LL powerMod(LL q,LL n){

    LL ans=1;
    q%=MOD;
    while(n){
        if(n%2)
            ans=(ans*q)%MOD;
        n/=2;
        q=(q*q)%MOD;
    }
    return ans;
}

int main(){

    int t;
    scanf("%d",&t);
    while(t--){
        LL a,b,c,k;
        scanf("%I64d%I64d%I64d%I64d",&a,&b,&c,&k);
        if(a+c==2*b){
            printf("%I64d\n",(a+(k-1)*(b-a))%MOD);
        }
        else {
            LL q=b/a;
            LL ans=powerMod(q,k-1);
            printf("%I64d\n",(a%MOD*ans)%MOD);
        }
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: