二分求幂 2

/*
题目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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值