题目链接:http://acm.tju.edu.cn/toj/showp3293.html
Time Limit: 1.0 Seconds Memory Limit: 65536K
Total Runs: 1195 Accepted Runs: 343
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
Author:SUN, Chao
Source: Multi-School Training Contest - TOJ Site #1
快速幂取余,这道题主要学会这个就好了,快速幂基础题,相关知识:http://blog.youkuaiyun.com/lsldd/article/details/5506933。代码如下:
#include<stdio.h>
#include<algorithm>
long long modexp(long long a, long long b, long long mod)
{
long long res=1;
while(b>0)
{
a=a%mod;
if(b&1)//&位运算:判断二进制最后一位是0还是1,&的运算规则为前后都是1的时候才是1;
res=res*a%mod;
b=b>>1;
a=a*a%mod;
}
return res;
}
int main(){
int n;
long long a,b,c,k;
scanf("%d",&n);
while(n--){
scanf("%lld%lld%lld%lld",&a,&b,&c,&k);
if(c+a==2*b){
long long q=b-a;
a=(a%200907+((k-1)%200907)*(q%200907))%200907;
printf("%lld\n",a);
}
else{
long long q=b/a;
printf("%lld\n",(a*modexp(q,k-1,200907))%200907);
}
}
}