CSU 1556-Jerry's trouble(快速幂)
2015-12-07 21:57
239 查看
1556: Jerry's trouble
Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 741 Solved: 299[Submit][Status][WebBoard]Description
Jerry is caught by Tom. He was penned up in one room with a door, which only can be opened by its code. The code is the answer of the sum of the sequence of number written on the door. The type of the sequence of number isBut Jerry’s mathematics is poor, help him to escape from the room.Input
There are some cases (about 500). For each case, there are two integer numbers n, m describe as above ( 1 <= n < 1 000 000, 1 <= m < 1000).Output
For each case, you program will output the answer of the sum of the sequence of number (mod 1e9+7).Sample Input
4 1 5 1 4 2 5 2 4 3
Sample Output
10 15 30 55 100
AC代码:
#include<iostream>#include<algorithm>#include<cstring>#include<string>#include<cstdio>using namespace std;#define T 123#define inf 0x3f3f3f3f#define mod 1000000007typedef long long ll;int powMod(ll n,ll p){ll res = 1;while(p>0){if(p&1){res=(res*n)%mod;}p/=2;n=(n*n)%mod;}return res;}int main(){#ifdef zscfreopen("input.txt","r",stdin);#endifint n,m,i;while(~scanf("%d%d",&n,&m)){ll sum=1;for(i=2;i<=n;++i){sum=(sum+powMod(i,m))%mod;}printf("%lld\n",sum%mod);}return 0;}
相关文章推荐
- 动易2006序列号破解算法公布
- Ruby实现的矩阵连乘算法
- C#插入法排序算法实例分析
- 超大数据量存储常用数据库分表分库算法总结
- C#数据结构与算法揭秘二
- C#冒泡法排序算法实例分析
- 算法练习之从String.indexOf的模拟实现开始
- C#算法之关于大牛生小牛的问题
- C#实现的算24点游戏算法实例分析
- c语言实现的带通配符匹配算法
- 浅析STL中的常用算法
- 算法之排列算法与组合算法详解
- C++实现一维向量旋转算法
- Ruby实现的合并排序算法
- C#折半插入排序算法实现方法
- 基于C++实现的各种内部排序算法汇总
- C++线性时间的排序算法分析
- C++实现汉诺塔算法经典实例
- PHP实现克鲁斯卡尔算法实例解析
- C#获取关键字附近文字算法实例