Easy Summation

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 4112    Accepted Submission(s): 1676

Problem Description
You are encountered with a traditional problem concerning the sums of powers.
Given two integers n 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 modulo 109+7.
 
Input
The first line of the input contains an integer T(1≤T≤20), denoting the number of test cases.
Each of the following T lines contains two integers n(1≤n≤10000) and k(0≤k≤5).
 
Output
For each test case, print a single line containing an integer modulo 109+7.
 
Sample Input
3
2 5
4 2
4 1
 
Sample Output
33
30
10

题意:给出n,k,定义f(i)=i^k,求出[f(1)+f(2)+……+f(n)]mod 1e9+7

题解:直接套快速幂,注意用long long

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const long long mod=1e9+;
  4. long long PowerMod(long long a, long long b,long long c) {
  5. long long ans = ;
  6. a = a % c;
  7. while(b>) {
  8.  
  9. if(b % == )
  10. ans = (ans * a) % c;
  11. b = b/;
  12. a = (a * a) % c;
  13. }
  14. return ans;
  15. }
  16. int main()
  17. {
  18. int t;
  19. while(~scanf("%d",&t))
  20. {
  21. while(t--)
  22. {
  23. long long n,k,sum=;
  24. scanf("%lld %lld",&n,&k);
  25. for(int i=;i<=n;i++)
  26. {
  27. sum+=PowerMod(i,k,mod);
  28. sum=sum%mod;
  29. }
  30. printf("%lld\n",sum);
  31. }
  32. }
  33. return ;
  34. }

hdu6027Easy Summation(快速幂取模)的更多相关文章

  1. 【转】C语言快速幂取模算法小结

    (转自:http://www.jb51.net/article/54947.htm) 本文实例汇总了C语言实现的快速幂取模算法,是比较常见的算法.分享给大家供大家参考之用.具体如下: 首先,所谓的快速 ...

  2. HDU 1061 Rightmost Digit --- 快速幂取模

    HDU 1061 题目大意:给定数字n(1<=n<=1,000,000,000),求n^n%10的结果 解题思路:首先n可以很大,直接累积n^n再求模肯定是不可取的, 因为会超出数据范围, ...

  3. UVa 11582 (快速幂取模) Colossal Fibonacci Numbers!

    题意: 斐波那契数列f(0) = 0, f(1) = 1, f(n+2) = f(n+1) + f(n) (n ≥ 0) 输入a.b.n,求f(ab)%n 分析: 构造一个新数列F(i) = f(i) ...

  4. POJ3641-Pseudoprime numbers(快速幂取模)

    题目大意 判断一个数是否是伪素数 题解 赤果果的快速幂取模.... 代码: #include<iostream> #include<cmath> using namespace ...

  5. 九度OJ 1085 求root(N, k) -- 二分求幂及快速幂取模

    题目地址:http://ac.jobdu.com/problem.php?pid=1085 题目描述: N<k时,root(N,k) = N,否则,root(N,k) = root(N',k). ...

  6. HDU--杭电--4506--小明系列故事——师兄帮帮忙--快速幂取模

    小明系列故事——师兄帮帮忙 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) To ...

  7. CodeForces Round #191 (327C) - Magic Five 等比数列求和的快速幂取模

    很久以前做过此类问题..就因为太久了..这题想了很久想不出..卡在推出等比的求和公式,有除法运算,无法快速幂取模... 看到了 http://blog.csdn.net/yangshuolll/art ...

  8. HDU1013,1163 ,2035九余数定理 快速幂取模

    1.HDU1013求一个positive integer的digital root,即不停的求数位和,直到数位和为一位数即为数根. 一开始,以为integer嘛,指整型就行吧= =(too young ...

  9. Powmod快速幂取模

    快速幂取模算法详解 1.大数模幂运算的缺陷: 快速幂取模算法的引入是从大数的小数取模的朴素算法的局限性所提出的,在朴素的方法中我们计算一个数比如5^1003%31是非常消耗我们的计算资源的,在整个计算 ...

随机推荐

  1. thinkphp清除缓存

    前台 //清除缓存 $(function(){ $("#cache").click(function(){ layer.confirm('你确定要清除缓存吗?', {icon: 3 ...

  2. (第二场)A Run 【动态规划】

    链接:https://www.nowcoder.com/acm/contest/140/A 题目描述: White Cloud is exercising in the playground.Whit ...

  3. 启用 DHCP

    #开启DHCP协议 Get-NetIPInterface -Dhcp Disabled|Set-NetIPInterface -Dhcp Enabled #开启本机上所有博通网卡的DHCP Get-W ...

  4. 计算机名称和IP地址

    获取本地IP地址 得到远程机IP地址与描述 若仅仅是查看IP地址

  5. 启动memcache

    "D:\SOFT\memcached-1.4.5-amd64\memcached-amd64\memcached.exe"

  6. Redis通过IO进行序列化+反序列化

    必须引用序列化Serializable接口 创建类:Role package com.wbg.springRedis.entity; import java.io.Serializable; publ ...

  7. 随机数使用不当引发的生产bug

    前几天负责的理财产品线上出现问题:一客户赎回失败,查询交易记录时显示某条交易记录为其他人的卡号. 交易的链路如下: 出现该问题后,我们对日志进行了分析,发现主站收到的两笔流水号完全相同,然而主站却没有 ...

  8. IDEA(2018.3.2)

    N757JE0KCT-eyJsaWNlbnNlSWQiOiJONzU3SkUwS0NUIiwibGljZW5zZWVOYW1lIjoid3UgYW5qdW4iLCJhc3NpZ25lZU5hbWUiO ...

  9. 基于LSB的图像数字水印实验

    1. 实验类别 设计型实验:MATLAB设计并实现基于LSB的图像数字水印算法. 2. 实验目的 了解信息隐藏中最常用的LSB算法的特点,掌握LSB算法原理,设计并实现一种基于图像的LSB隐藏算法. ...

  10. mysql alter 增加修改表结构及约束

    1) 加索引,添加时若未指定索引名,默认为字段名   mysql> alter table 表名 add index 索引名 (字段名1[,字段名2 …]); 例子: mysql> alt ...