CSU - 1556 Jerry's trouble(高速幂取模)
【题目链接】:click here
【题目大意】:计算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;
}
CSU - 1556 Jerry's trouble(高速幂取模)的更多相关文章
- hdu 3221 Brute-force Algorithm(高速幂取模,矩阵高速幂求fib)
http://acm.hdu.edu.cn/showproblem.php?pid=3221 一晚上搞出来这么一道题..Mark. 给出这么一个程序.问funny函数调用了多少次. 我们定义数组为所求 ...
- UVa 11582 Colossal Fibonacci Numbers! 【大数幂取模】
题目链接:Uva 11582 [vjudge] watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fil ...
- HDU1061_Rightmost Digit【高速幂取余】
Rightmost Digit Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- 组合数取模Lucas定理及快速幂取模
组合数取模就是求的值,根据,和的取值范围不同,采取的方法也不一样. 下面,我们来看常见的两种取值情况(m.n在64位整数型范围内) (1) , 此时较简单,在O(n2)可承受的情况下组合数的计算可以 ...
- 【转】C语言快速幂取模算法小结
(转自:http://www.jb51.net/article/54947.htm) 本文实例汇总了C语言实现的快速幂取模算法,是比较常见的算法.分享给大家供大家参考之用.具体如下: 首先,所谓的快速 ...
- HDU 1061 Rightmost Digit --- 快速幂取模
HDU 1061 题目大意:给定数字n(1<=n<=1,000,000,000),求n^n%10的结果 解题思路:首先n可以很大,直接累积n^n再求模肯定是不可取的, 因为会超出数据范围, ...
- 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) ...
- POJ3641-Pseudoprime numbers(快速幂取模)
题目大意 判断一个数是否是伪素数 题解 赤果果的快速幂取模.... 代码: #include<iostream> #include<cmath> using namespace ...
- 九度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). ...
随机推荐
- Playing with String(codeforces 305E)
题意:刚开始你只有一个字符串每次能选择一个有的字符串 s,找到 i,满足s[i - 1] = s[i + 1],将其分裂成 3 个字符串s[1 · · · i - 1]; s[i]; s[i + 1 ...
- 字符串(bzoj 1856)
Description lxhgww最近接到了一个生成字符串的任务,任务需要他把n个1和m个0组成字符串,但是任务还要求在组成的字符串中,在任意的前k个字符中,1的个数不能少于0的个数.现在lxhgw ...
- 命名参数和可选参数在.NET中的使用
原文发布时间为:2011-04-25 -- 来源于本人的百度文章 [由搬家工具导入] Named and Optional Arguments class NamedExample{staticvoi ...
- Ubuntu FireFox的“后退”按钮与Backspace键
我们都知道,大部分的网页浏览器和窗口管理器的“后退”按钮是和Backspace键关联的,这样会极大的方便并加快我们浏览时的翻页速度(我一直是左手 放在键盘上按快捷键,右手鼠标的姿势找文件和看网页的,省 ...
- sublime 中设置pylint
http://www.360doc.com/content/14/1110/11/15077656_424004081.shtml 安装 pylinter 插件 详见 sublime 插件安装 配 ...
- 八、Ubuntu安装Tomcat和jdk
1.解压Tomcat 和 jdk tar -zxvf apache-tomcat-8.0.28.tar.gz tar -zxvf jdk-8u60-linux-x64.gz 2.将解压后的tomcat ...
- 解决mysql 远程链接问题
grant all privileges on *.* to 'root'@'192.168.2.204' identified by '123456' with grant option;flush ...
- 利用Jdk 6260652 Bug解析Arrays.asList
在java.util.ArrayList源码中: c.toArray might (incorrectly) not return Object[] (see 6260652) 产生疑惑: 附上Jav ...
- 查看tomcat启动文件都干点啥---Catalina.java
在前一章查看tomcat启动文件都干点啥---Bootstrap.java中我们得出结论,在Bootstrap中通过反射调用Catalina类中的getServer,start,stop,stopSe ...
- workflow engine Ruote初体验之二(通用属性)
罗列一下表达式所支持的属性: :timeout :if/ unless :forget :lose :flank :on_error :on_cancel :on_timeout :tag :filt ...