解题思路
首先判断当前序列是等差数列还是等比数列,若 b ∗ 2 = = ( a + c ) b*2==(a+c) b∗2==(a+c),就是等差,第 k k k项为 a + ( b − a ) ∗ ( k − 1 ) a+(b-a)*(k-1) a+(b−a)∗(k−1)。如果是等比数列,就要用到快速幂,第 k k k项为 a ∗ k s m ( b / a , k − 1 ) a*ksm(b/a,k-1) a∗ksm(b/a,k−1)
代码
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<algorithm>
#include<iomanip>
#include<cmath>
using namespace std;
const int Mod=200907;
int T;
long long a,b,c,k;
long long ksm(long long x,long long b){
long long base=x%Mod,ans=1;
while(b>0)
{
if(b&1)
ans=ans*base%Mod;
base=base*base%Mod;
b>>=1;
}
return ans;
}
int main(){
scanf("%d",&T);
for(int i=1;i<=T;i++)
{
scanf("%lld%lld%lld%lld",&a,&b,&c,&k);
if(b*2==(a+c))
printf("%lld\n",(a+(b-a)*(k-1)%Mod)%Mod);
else printf("%lld\n",a*ksm(b/a,k-1)%Mod);
}
}