大数求解:等差数列第n项值 等比数列第n项值,结果mod100007
#include<iostream>
#include<cstdio>
#include<string.h>
using namespace std;
long long d,q,a1,a2,a3,a4,ans;
long long exp_mod(long long a,long long n,long long b)
//快速幂取模:
{
long long t;
if(n==0)
return 1%b;
if(n==1)
return a%b;
t=exp_mod(a,n/2,b);
t=t*t%b;
if((n&1)==1)
t=t*a%b;
return t;
}
int main()
{
while(scanf("%lld%lld%lld%lld",&a1,&a2,&a3,&a4)!=EOF)
{
if(a1+a3==a2*2)//等差数列
{
d=a2-a1;
// printf("%d\n",d);
ans=((a1%100007)+((a4-1)%100007)*(d%100007))%100007;
printf("%lld\n",ans);
continue;
}
else
{
//快速幂取模就是在O(logn)内求出a^n mod b的值。算法的原理是ab mod c=(a mod c)(b mod c)mod c
//An=A1×q^(n-1)
printf("%lld\n",(a1%100007*exp_mod(a2,a4-1,100007))%100007);
}
}
return 0;
}