您的位置:首页 > 其它

ACdream 1007

2016-03-25 23:39 204 查看
input

T <=10

n k n<=1000 k<=10^18

a1,a2,...an |ai|<=10^18

output

(a1^k+a2^k+...+an^k)%10^10+7

Sample Input

2

3 1
1 2 3
3 10
1 2 3

Sample Output

6 60074

做法:快速幂+__int128,需注意ai可能是负数,模的是10^10+9,超int了

模乘法:对一个数可以拆分为N=(P*(N/P)+(N%P))

#include <bits/stdc++.h>
#define MAX 100000
#define LL long long
//#define __int128 long long
#define mod 10000000007LL
using namespace std;
int cas=1,T;
template <class T>
bool scanff(T &ret)
{ //Faster Input
char c; int sgn; T bit=0.1;
if(c=getchar(),c==EOF) return 0;
while(c!='-'&&c!='.'&&(c<'0'||c>'9')) c=getchar();
sgn=(c=='-')?-1:1;
ret=(c=='-')?0:(c-'0');
while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');
if(c==' '||c=='\n'){ ret*=sgn; return 1; }
while(c=getchar(),c>='0'&&c<='9') ret+=(c-'0')*bit,bit/=10;
ret*=sgn;
return 1;
}
template <class T>
void printff(T ret)
{ //Faster Output
char s[22];
if(ret<0) { printf("-");ret=-ret; }
int len=0;
while(ret)
{
s[++len]=ret%10+'0';
ret/=10;
}
if(!len) s[++len]='0';
while(len)
{
printf("%c",s[len]);
len--;
}
}
LL qmod( __int128 a, LL n)
{
__int128 res=1;
LL flag=1;
if(a<0) { flag=(n&1?-1:1);a=-a; }
a%=mod;
while(n)
{
if(n&1) res=res*a%mod;
a=a*a%mod;
n>>=1;
}
return (LL)res*flag;
}
int main()
{
//freopen("1.in","w",stdout);
//freopen("1.in","r",stdin);
//freopen("1.out","w",stdout);
scanf("%d",&T);
while(T--)
{
int n;
LL k,a,res=0;
scanf("%d%lld",&n,&k);
for(int i=0;i<n;i++)
{
scanf("%lld",&a);
res=(res+qmod(a,k))%mod;
}
printff((res+mod)%mod);
printf("\n");
}
//printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: