A sequence of numbers

http://acm.hdu.edu.cn/showproblem.php?pid=2817

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1594    Accepted Submission(s): 494

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
 
Recommend
gaojie
 
 
 
给出数列的前三项,数列要么是等差数列、要么是等比数列,求第K项。
很简单,注意细节处理。
注意数据类型的溢出,要用long long,中间过程防止溢出
 
 
#include<stdio.h>

#define MOD 200907

long long mod_exp(int a,int b,int n){
    long long ans=1;
    for(;b;b>>=1,a=(long long)(((long long)a)*a)%n)
        if(b&1)
            ans=(long long)(((long long)ans)*a)%n;
    return ans;
}

int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        long long a,b,c,k;
        scanf("%I64d%I64d%I64d%I64d",&a,&b,&c,&k);
        if(a+c==2*b){
            long long d=(long long)(b-a);
            int ans=(a%MOD+((k-1)%MOD)*d%MOD)%MOD;
            printf("%d\n",ans);
        }else{
            long long q=(long long)(b/a);
            long long tmp=mod_exp(q,k-1,MOD);
            int ans=(a*tmp)%MOD;
            printf("%d\n",ans);
        }
    }
    return 0;
}