ACdream1007 a+b


題目是要求 a[i]^k 的累加和。有如下幾點注意、

1.  mod=1e10+7
2.  大整數乘法(直接乘會爆LL)
3.  小心負數
4.  取模運算一般取最下非負整數。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#define LL long long
#define mod 10000000007
using namespace std;

LL plus2(LL x,LL k){
	if (k<0) return plus2(-x,-k);
	if (k==0) return 0;
	if (k==1) return x;
	LL tmp=(plus2(x,k/2)%mod);
	if (k&1) return (tmp+tmp+x)%mod;
	else return (tmp+tmp)%mod;
}

LL pow2(LL x,LL k){
	if (k==0) return 1;
	if (k==1) return x;
	LL tmp=(pow2(x,k/2)%mod);
	if (k&1) return (plus2(plus2(tmp,tmp),x)%mod);
	else return (plus2(tmp,tmp)%mod);
}

LL a;
LL k;
int main(){
	int n,cases;
	scanf("%d",&cases);
	while (cases--){
		scanf("%d%lld",&n,&k);
		LL ans=0;
		if (k&1)
		for (int i=1;i<=n;i++){
			scanf("%lld",&a);
			ans=(ans+plus2(a%mod,pow2(a%mod,k-1))%mod)%mod;
		}
		else 
		for (int i=1;i<=n;i++){
			scanf("%lld",&a);
			ans=(ans+pow2(a%mod,k)%mod)%mod;
		}
		printf("%lld\n",(ans+mod)%mod);
	}
	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章