题目链接】: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&#39;s trouble(高速幂取模)的更多相关文章

  1. hdu 3221 Brute-force Algorithm(高速幂取模,矩阵高速幂求fib)

    http://acm.hdu.edu.cn/showproblem.php?pid=3221 一晚上搞出来这么一道题..Mark. 给出这么一个程序.问funny函数调用了多少次. 我们定义数组为所求 ...

  2. UVa 11582 Colossal Fibonacci Numbers! 【大数幂取模】

    题目链接:Uva 11582 [vjudge] watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fil ...

  3. HDU1061_Rightmost Digit【高速幂取余】

    Rightmost Digit Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  4. 组合数取模Lucas定理及快速幂取模

    组合数取模就是求的值,根据,和的取值范围不同,采取的方法也不一样. 下面,我们来看常见的两种取值情况(m.n在64位整数型范围内) (1)  , 此时较简单,在O(n2)可承受的情况下组合数的计算可以 ...

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

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

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

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

  7. 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) ...

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

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

  9. 九度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). ...

随机推荐

  1. sql中的like和正则的区别

    like,模糊查询,更多的是用于匹配已知的字符,比如查询该字段含有1的记录,like ‘%1%’:但是如果要匹配不确定的,但是一个系列的字符,比如数字,最好用regexpselect * from t ...

  2. How Many to Be Happy?

    How Many to Be Happy? 时间限制: 1 Sec  内存限制: 128 MB 题目描述 Let G be a connected simple undirected graph wh ...

  3. js 字符串长度截取

    <script> function cutstr(str, len) { var temp, icount = 0, patrn = /[^\x00-\xff]/, strre = &qu ...

  4. java常见设计模式简要总结

    设计模式六大原则 1.开放封闭原则:对扩展开放,对修改封闭,意即程序拓展时不要动原有的代码 2.LSP原则:任何基类可以出现的地方,子类一定可以出现 3.依赖倒置原则:使用接口,依赖于抽象而不是具体 ...

  5. struct sockaddr与struct sockaddr_in ,struct sockaddr_un的区别和联系

    在linux环境下,结构体struct sockaddr在/usr/include/linux/socket.h中定义,具体如下:typedef unsigned short sa_family_t; ...

  6. 《Linux命令行与shell脚本编程大全 第3版》Linux命令行---40

    以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下:

  7. 一个关于集合的问题,为什么添加进List集合中的元素被莫名其妙的改变了

    以前自己理解的不够深刻,特此记录一下提醒自己,如果正好也帮到了你,我会很开心.相信只有自己正好遇到这个问题,才觉得哦,原来这样.自己小白,大神莫喷 为什么添加进List集合中的元素被莫名其妙的改变了? ...

  8. Codeforces Round #466 (Div. 2) A. Points on the line[数轴上有n个点,问最少去掉多少个点才能使剩下的点的最大距离为不超过k。]

    A. Points on the line time limit per test 1 second memory limit per test 256 megabytes input standar ...

  9. sprintf 心得

    [string print format]sprintf指的是字符串格式化命令. [主要功能]是把格式化的数据写入某个字符串中. sprintf是个变参函数. 使用sprintf对于写入buffer的 ...

  10. 2016集训测试赛(二十四)Problem C: 棋盘控制

    Solution 场上的想法(显然是错的)是这样的: 我们假设棋子是一个一个地放置的, 考虑在放置棋子的过程中可能出现哪些状态. 我们令有序整数对\((i, j)\)表示总共控制了\(i\)行\(j\ ...