题目1442:A sequence of numbers
题目描述:
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
*/
#include <stdio.h>
#define M 200907
long long buf[5];
int main(){
int n,i;
while(scanf("%d",&n)!=EOF){
for(int j=0;j<n;j++){
for(i=0;i<4;i++)
scanf("%lld",&buf[i]);
long long ans;
if((buf[1]-buf[0])==(buf[2]-buf[1])){
ans=(((buf[1]-buf[0])%M)*((buf[3]-
1)%M))%M+buf[0]%M;
ans%=M;
printf("%lld\n",ans);
}
else if((buf[1]/buf[0])==(buf[2]/buf[1])){
long long q=buf[1]/buf[0];
ans=buf[0];
buf[3]--;
while(buf[3]){
if(buf[3]%2==1){
ans*=q;
ans%=M;
}
buf[3]/=2;
q*=q;
q%=M;
}
printf("%lld\n",ans);
}
}
}
return 0;
}
#include <stdio.h>
#define M 200907
long long buf[5];
long long ans; //全局定义
long long dengbi(long long d,long long k){ // 等比数列
ans=buf[0];
--k;
while(k!=0){
if(k%2==1){
ans*=d;
ans%=M;
}
k/=2;
d*=d;
d%=M;
}
return ans;
}
long long dengcha(long long d,long long k){//等差
ans=(buf[0]%M)+(((k-1)%M)*(d%M))%M;
ans %= M;
return ans;
}
int main(){
int n,i,j;
long long d;
while(scanf("%d",&n) != EOF){
for(i=0;i<n;i++){
for(j=0;j<4;j++){
scanf("%lld",&buf[j]);} //j写错
if((buf[1]-buf[0])==(buf[2]-buf[1])){ // 等差数列
d=buf[1]-buf[0];
ans = dengcha(d,buf[3]);
}
else{
if((buf[1]/buf[0])==(buf[2]/buf[1])){//等比数列
d=buf[1]/buf[0];
ans = dengbi(d,buf[3]);
}
}
printf("%lld\n",ans);
}
}
return 0;
}