zoukankan      html  css  js  c++  java
  • 【CSU 1556】Pseudoprime numbers

    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

    1^m + 2^m + 3^m + …… + n^m

    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 + 3^m + …… + n^m  (mod 1e9+7)

    注意:每个加起来的时候和加起来后也要mod

    #include<stdio.h>
    #define M 1000000007
    long long qpow(long long a,long long m)//快速幂
    {
        long long ans=1,k=a;
        while(m)
        {
            if(m&1)
                ans=(ans*k)%M;
            k=(k*k)%M;
            m>>=1;
        }
        return ans;
    }
    int main()
    {
        long long n,m,ans;
        while(scanf("%lld%lld",&n,&m)!=EOF)
        {
            ans=0;
            for(int i=1; i<=n; i++)
                ans+=qpow(i,m)%M;//这里mod一下
            printf("%lld
    ",ans%M);//这里mod一下
        }
        return 0;
    }

      

  • 相关阅读:
    java1.8--OptionalInt,OptionalDouble,OptionalLong类
    java1.8--Optional类
    java1.8--Null Object模式
    JDK自带的缓存--包装类的缓存
    函数式编程--lambda表达式对比匿名内部类
    java1.8--改进的接口
    Mybatis Generator(定制化)代码生成器
    Mybatis
    git fork
    git原理图解
  • 原文地址:https://www.cnblogs.com/flipped/p/5182968.html
Copyright © 2011-2022 走看看