题目1442:A sequence of numbers 九度OJ

题目1442:A sequence of numbers

时间限制:1 秒

内存限制:128 兆

特殊判题:

提交:4160

解决:1059

题目描述:

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
//题目1442:题中所给的序列为算术几何序列,也就是等差或者等比序列
//所以,在读入序列之后,利用序列前三个数判断该序列是等比序列还是等差序列。
//据说直接判断是否为等差序列比较省时间。如果该序列不是等差序列 ,则必定是等比序列 
// 等差序列利用公式即可求得------an=a1+(n-1)*d
//等比序列,遵照二分求幂的原则可求得---------an=a1*q^(n-1) 
//You can assume 0 < K <= 10^9, and the other three numbers are in the range [0, 2^63)
//关于题中变量 大小的 设定 ,另外三个数都是2^63 -1,说明需要用 long long 来存储
// 输出 取模   Output one line for each test case, that is, the K-th number module (%) 200907.
 
#include <cstdio>
#define ret 200907
int main(){
	int n;
	while(scanf("%d",&n)!=EOF){
		while(n--){
			long long a,b,c,k;
			long long ans;
			scanf("%lld%lld%lld%lld",&a,&b,&c,&k); 
			//判断是等差还是等比
			if((b-a)==(c-b)){//是等差数列 
			 	ans=a%ret+(((k-1)%ret)*((b-a)%ret))%ret;//对此处数值的范围 不是很明了 ,在哪里取模才正确 
			}else{    //是等比数列 --------二分求幂  
				long long q=b/a;
				long long m=k-1;
				ans=1;
				while(m!=0){
					if(m%2==1){
						ans=(ans*q)%ret;
					}
					m=m/2;
					q=(q*q)%ret;
				}
				ans=((a%ret)*ans)%ret;
			}
			printf("%lld\n",ans);
			 
		}
	}
	return 0;
} 




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值