A sequence of numbers
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1129 Accepted Submission(s): 359
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
2009 Multi-University Training Contest 1 – Host by TJU
Recommend gaojie 给出数列的前三项,数列要么是等差数列、要么是等比数列,求第K项。 很简单,注意细节处理。 注意数据类型的溢出,要用long long,中间过程防止溢出
#include<stdio.h>
#define MOD 200907
/*int EE(int x,int n)
{
int m=n;
int power=1;
int z=x;
while(m>0)
{
if((m%2))
{
power=((power*z)%MOD);
}
m/=2;
z=((z*z)%MOD);
}
return power;
}*/
long long modular_exponent(long long a,long long b,int n){ //a^b mod n
long long ret=1;
for (;b;b>>=1,a=(long long)(((long long)a)*a%n))
if (b&1)
ret=(long long)(((long long)ret)*a%n);
//printf("%d\n",ret);
return ret;
}
int main()
{
double a,b,c;
int T;
int k;
scanf("%d",&T);
while(T--)
{
scanf("%lf%lf%lf%d",&a,&b,&c,&k);
if(a+c==2*b)
{
long long a1=(long long )a;
long long d=(long long )(b-a);
int ans=(a1%MOD+((k-1)%MOD)*(d%MOD))%MOD;
printf("%d\n",ans);
}
else
{
long long a1=(long long )a;
long long t1=(long long )(a1%MOD);
double q1=(b/a);
long long q2=(long long)q1;
long long q=(long long)(q2%MOD);
long long tmp=modular_exponent(q,k-1,MOD);
int ans=(t1*tmp)%MOD;
printf("%d\n",ans);
}
}
return 0;
}