HDU6027 Easy Summation 2017中国大学生程序设计竞赛
来源:互联网 发布:蓝牙串口软件ymodem 编辑:程序博客网 时间:2024/05/19 03:27
题目如下;
Easy Summation
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1296 Accepted Submission(s): 515
Problem Description
You are encountered with a traditional problem concerning the sums of powers.
Given two integersn and k . Let f(i)=ik , please evaluate the sum f(1)+f(2)+...+f(n) . The problem is simple as it looks, apart from the value of n in this question is quite large.
Can you figure the answer out? Since the answer may be too large, please output the answer modulo109+7 .
Given two integers
Can you figure the answer out? Since the answer may be too large, please output the answer modulo
Input
The first line of the input contains an integer T(1≤T≤20) , denoting the number of test cases.
Each of the followingT lines contains two integers n(1≤n≤10000) and k(0≤k≤5) .
Each of the following
Output
For each test case, print a single line containing an integer modulo109+7 .
Sample Input
32 54 24 1
Sample Output
333010
这道题是一道简单的计算题,就是给你 n,k, 求出 f(1)+f(2)+……+f(n) f(i)=i^k
当时做题的时候一直WA,比赛结束后才知道正解,思路是对的,但是小细节没把握好。
下面我给出这道题的三种解法。
第一种 暴力
考虑到 k的范围1-5 很小 n的范围也只有1-10000 所以这题完全可以用暴力的方法来做 细节地方要注意的是 ans 和 tmp 一定要设为long long 否则在计算过程中会溢出发生错误
/*2017年7月28日08:39:28暴力AC代码 */#include<stdio.h>typedef long long ll;const int mod=1e9+7;int main(){int t;scanf("%d",&t);while(t--){ll ans=0;int n,k;scanf("%d%d",&n,&k);for(int i=1;i<=n;i++){ll tmp=1;for(int j=1;j<=k;j++){tmp=i*tmp%mod;}ans=(ans+tmp)%mod;}printf("%I64d\n",ans);}return 0;}
第二种方法打表
利用一个二维数组 f[maxn][10] 存贮 f[i][k] 当输入n,k的时候,再把i 从1-n 都累加一遍,即可 注意同上 ans 和tmp要声明为Long Long
#include<stdio.h>typedef long long ll;const int mod=1e9+7;const int maxn=10000+10;ll f[maxn][10];int main(){for(int i=1;i<=10000;i++)f[i][0]=1; for(int i=1;i<=10000;i++){for(int j=1;j<=5;j++){f[i][j]=f[i][j-1]*i%mod;}}int t;scanf("%d",&t);while(t--){int n,k;scanf("%d%d",&n,&k);ll ans=0;for(int i=1;i<=n;i++){ans=(ans+f[i][k])%mod;} printf("%d\n",ans);}return 0;}
第三种 利用快速幂的方法
简单粗暴 同样注意 快速幂函数中,数据类型要写成long long 不然在计算过程中会溢出
#include<stdio.h>typedef long long ll;const ll mod=1e9+7;ll fast_mod(ll x,ll n){ll res=1;while(n){if(n&1) res=(res*x)%mod;x=(x*x)%mod;n>>=1;}return res;}int main(){int t;scanf("%d",&t);while(t--){int n,k;scanf("%d%d",&n,&k);int res=0;for(int i=1;i<=n;i++){res=(res+fast_mod(i,k)%mod)%mod;}printf("%I64d\n",res);}return 0;}
虽然这题很水,但是我当时做的时候一直出错 ,原因就是我ans 和 tmp 还有 快速幂中的数据 都开成了int ,这样导致在计算的中间过程中,数据已经发生溢出,结果不对,所以看了几个大佬的代码,学习了一下各种写法。
刚刚入门,如有错误请指正。
阅读全文
0 0
- HDU6027 Easy Summation 2017中国大学生程序设计竞赛
- HDU6027-Easy Summation
- HDU6027 Easy Summation
- HDU6027-Easy Summation-简单数学
- 2017中国大学生程序设计竞赛
- 2017中国大学生程序设计竞赛
- 2017中国大学生程序设计竞赛
- 2017中国大学生程序设计竞赛
- 2017中国大学生程序设计竞赛
- 2017中国大学生程序设计竞赛
- 2017中国大学生程序设计竞赛
- 2017中国大学生程序设计竞赛
- 2017中国大学生程序设计竞赛
- 2017中国大学生程序设计竞赛
- 2017中国大学生程序设计竞赛
- 2017中国大学生程序设计竞赛
- 2017中国大学生程序设计竞赛
- 2017中国大学生程序设计竞赛
- 频道管理
- Lengthening Sticks 组合数+容斥原理
- ViewPager
- caffe基础-01数据层的配置
- 字典的基础及字典的使用实例
- HDU6027 Easy Summation 2017中国大学生程序设计竞赛
- UVA
- OpenGL超级宝典visual studio 2013开发环境配置,GLTools
- 1.输入子系统
- opencv1-基础库
- [HiHoCoder]#1015 : 马拉车
- BZOJ 3790 Manacher 解题报告
- 排序中的数据结构
- 20个非常有用的Java程序片段