zoukankan      html  css  js  c++  java
  • csu 1556(快速幂)

    1556: Jerry's trouble

    Time Limit: 10 Sec  Memory Limit: 256 MB
    Submit: 787  Solved: 317
    [Submit][Status][Web Board]

    Description

     Jerry is caught by Tom. He was penned up in one room with a door, which only can be opened by its code. The code is the answer of the sum of the sequence of number written on the door. The type of the sequence of number is

    But Jerry’s mathematics is poor, help him to escape from the room.

    Input

     There are some cases (about 500). For each case, there are two integer numbers n, m describe as above ( 1 <= n < 1 000 000, 1 <= m < 1000).

    Output

     For each case, you program will output the answer of the sum of the sequence of number (mod 1e9+7).

    Sample Input

    4 1
    5 1
    4 2
    5 2
    4 3

    Sample Output

    10
    15
    30
    55
    100

    题意:求解 (1^m+2^m+...+n^m)%mod
    题解:快速幂
    #include <stdio.h>
    #include <string.h>
    #include <iostream>
    #include <math.h>
    #include <algorithm>
    using namespace std;
    typedef long long LL;
    const LL mod = 1000000007;
    LL pow_mod(LL a,LL n){
        LL ans = 1;
        while(n){
            if(n&1) ans = ans*a%mod;
            a = a*a%mod;
            n>>=1;
        }
        return ans;
    }
    int main()
    {
        int n,m;
        while(scanf("%d%d",&n,&m)!=EOF){
            LL res = 0;
            for(int i=1;i<=n;i++){
                res = (res+pow_mod(i,m))%mod;
            }
            printf("%lld
    ",res);
        }
    }
  • 相关阅读:
    一份简单的自我评述
    从诞总那儿得到的一些感悟
    2021秋软件工项目选题
    LeNet 网络进行猫狗大战
    不平行的直线
    切长条
    纪念品分组
    奇♂妙拆分
    Qt 一些日期格式转换不精确
    windbg 查看崩溃日志
  • 原文地址:https://www.cnblogs.com/liyinggang/p/5792917.html
Copyright © 2011-2022 走看看