快速幂等差数列

快速幂等差数列

\(a_i+a_{i+2}=a_{i+1}*2\)为等差数列,根据通项\(a_n=a_1+(n-1)*d\)就可以求出第n项。
而对于等比数列,\(a_n=a_1*q^{n-1}\),q的n-1次幂。

#include<cstdio>
#include<cstring>
#define ll long long
using namespace std;
const ll M = 200907;
ll pow(ll x, ll y)//快速幂 
{
	ll ans_ = 1;
	while(y)
	{
		if(y%2==1) ans_*=x;
		x = (x*x)%M;
		y = y/2;
	}
	return ans_;
} 
ll a, b, c, d, k, ans;
void s()
{
	scanf("%lld%lld%lld%lld", &a, &b, &c, &k);
	if(c-b == b-a)
	{
		d = (b-a)%M;
		ans = (a+(k-1)%M*d)%M;
	}//判断是否为等差数列 
	else
	{
		d = (b/a)%M;
		ans = a%M*pow(d, k-1)%M;
	}//否则为等比数列 
	printf("%lld\n", ans);
}
int t;
int main()
{
	scanf("%d", &t);
	while(t--) s();
	return 0;
}
posted @ 2019-12-24 20:20  orange_lyc  阅读(185)  评论(0编辑  收藏  举报