CSU - 1556 Jerry's trouble(快速幂取模)
2015-06-28 23:16
337 查看
【题目链接】:click here
【题目大意】:计算x1^m+x2^m+..xn^m(1<=x1<=n)( 1 <= n < 1 000 000, 1 <= m < 1000)
【解题思路】:快速幂取模
代码:
solution one:
solution two:
【题目大意】:计算x1^m+x2^m+..xn^m(1<=x1<=n)( 1 <= n < 1 000 000, 1 <= m < 1000)
【解题思路】:快速幂取模
代码:
solution one:
#include<bits/stdc++.h> #define LL long long using namespace std; const LL mod=(LL)1e9+7; LL pow_mod(LL a,LL p,LL n) { if(p==0) return 1; LL ans=pow_mod(a,p/2,n); ans=ans*ans%n; if(p&1) ans=ans*a%n; return ans; } int n,m; int main() { while(scanf("%d%d",&n,&m)!=EOF) { LL s=0; for(int i=1; i<=n; i++) s=(s+pow_mod(i%mod,m,mod))%mod; printf("%lld\n",s); } return 0; }
solution two:
#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> using namespace std; typedef long long LL; const LL mod=1e9+7; LL pow_mod(LL a,LL b) { LL res=a,ans=1; while(b) { if(b&1) ans=(res*ans)%mod; res=res*res%mod; b>>=1; } return ans; } int main() { LL n,m; while(~scanf("%lld %lld",&n,&m)) { LL s=0; for(int i=1; i<=n; ++i) s+=pow_mod(i%mod,m)%mod; printf("%lld\n",s%mod); } return 0; }
相关文章推荐
- javascript图形动画设计--以简单正弦波轨迹移动
- Web前端从入门到精通-2 关于html和开发工具
- 【学习笔记】【C语言】结构体和函数
- myeclipse 显示行数
- spss modeler 许可证到期的解决办法
- 严重: Error starting static Resources
- Tomcat --> Cannot create a server using the selected type
- win2008r2的iis7.5手动建站方法,iis7.5中用独立用户建立网站的方法,提高网站安全性
- BRI OS 高级ping
- md 散列算法
- 黑马程序员-OC语言-类和对象
- big O notation
- @suppressWarnings("unchecked") java 中是什么意思
- Eclipse Java注释模板设置详解
- oracle学习
- 数据库模型设计——关系的实现,主键的设计
- 金点涨停 阻击主力 公式
- ListView的实例讲解
- 小生入门请多多指教
- Criteria——Hibernate的面向对象查询