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

 #include<bits/stdc++.h>
using namespace std;
const long long mod=1e9+;
long long PowerMod(long long a, long long b,long long c) {
long long ans = ;
a = a % c;
while(b>) { if(b % == )
ans = (ans * a) % c;
b = b/;
a = (a * a) % c;
}
return ans;
}
int main()
{
int t;
while(~scanf("%d",&t))
{
while(t--)
{
long long n,k,sum=;
scanf("%lld %lld",&n,&k);
for(int i=;i<=n;i++)
{
sum+=PowerMod(i,k,mod);
sum=sum%mod;
}
printf("%lld\n",sum);
}
}
return ;
}

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. php 统计二维数组中某个相等值的总个数,并且组合成一个新的数组 转发

    $ar = array(   array( 'sv_id' => 1, 'mid' => 1, 'sv_question1' => 20 ),   array( 'sv_id' =& ...

  2. jquery 写的图片左右连续滚动

    <style type="text/css"> *{ margin:0; padding:0;} body { font:12px/1.8 Arial; color:# ...

  3. 16、SpringBoot-CRUD错误处理机制(3)

    3).将自己指定的数据携带出去 出现错误以后,会来到/error请求,会被BasicErrorController 进行处理 响应出去的数据是由 getErrorAttributes 得到的( Abs ...

  4. es6之函数扩展与对象扩展

    一.函数扩展 1.参数默认值 参数有默认值,后面不可以再加没有默认值的变量.如以下test函数中,不可以加写成 function test(x,y="word",z){ } fun ...

  5. 截屏快捷键(windows)自带截屏,不需要安装任何插件

    方案一:电脑自带截屏,不需要QQ Shift+win+s即可截图(推荐使用) 方案二:搜狗输入法下载自带:Ctrl+Shift+x

  6. Linux环境进程间通信: 共享内存

    Linux环境进程间通信: 共享内存 第一部分 共享内存可以说是最有用的进程间通信方式,也是最快的IPC形式.两个不同进程A.B共享内存的意思是,同一块物理内存被映射到进程A.B各自的进程地址空间.进 ...

  7. 【题解】洛谷P2704 [NOI2001] 炮兵阵地(状压DP)

    洛谷P2704:https://www.luogu.org/problemnew/show/P2704 思路 这道题一开始以为是什么基于状压的高端算法 没想到只是一道加了一行状态判断的状压DP而已 与 ...

  8. C#通过拼接协议的方式来发送邮件类库

    using System; using System.Collections.Generic; using System.Net; using System.Net.Mail; using Syste ...

  9. Unity3D Errors And Fix

    Author Error: Shader warning in 'Custom/ShowAnimation': Not enough temporary registers, needs 9 (com ...

  10. Nodejs中获取参数以及处理参数

    先看题干效果 在这里我们建了一个表单 填入表单需要提交的信息 对两个参数进行获取和一个加法计算 表单html代码 <form action='http://localhost:8080' met ...