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
1 2 3
3 10
1 2 3
模乘法:对一个数可以拆分为N=(P*(N/P)+(N%P))
View Code
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 11 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
相关文章推荐
- poj 2524[并查集]
- OpenvSwitch完全使用手册
- 数据结构基本概念
- 构建之法读后感
- 二叉树是否对称
- MVC 列表分页 Partial内容页
- 第三百五十七天 how can I 坚持
- Could not find or load main class HelloWorld
- 超声波模块HC-SR04调试经验分享
- 使用mac终端生成RSA私钥和公钥文件
- Spring中的Resource
- Linux内核分析第五周学习总结——分析system_call中断处理过程
- java中编码问题
- android开发中遇到的安全问题
- 轻松使用Nginx搭建web服务器
- 不能承受的生命之轻
- 《写在伤心的时候》
- 设计模式之工厂三姐妹解读(一)
- NSURLSession的GET和POST请求基本使用
- 很腻害的视频,回味百遍