题目是要求 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;
}