zoukankan      html  css  js  c++  java
  • hdu6027Easy Summation(快速幂取模)

    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(1T20), denoting the number of test cases.
    Each of the following T lines contains two integers n(1n10000) and k(0k5).
     
    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

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 const long long mod=1e9+7;
     4 long long PowerMod(long long a, long long b,long long c) {
     5     long long ans = 1;
     6     a = a % c;
     7     while(b>0) {
     8 
     9         if(b % 2 == 1)
    10             ans = (ans * a) % c;
    11         b = b/2;
    12         a = (a * a) % c;
    13     }
    14     return ans;
    15 }
    16 int main()
    17 {
    18     int t;
    19     while(~scanf("%d",&t))
    20     {
    21         while(t--)
    22         {
    23             long long n,k,sum=0;
    24             scanf("%lld %lld",&n,&k);
    25             for(int i=1;i<=n;i++)
    26             {
    27                 sum+=PowerMod(i,k,mod);
    28                 sum=sum%mod;
    29             }
    30             printf("%lld
    ",sum);
    31         }
    32     }
    33     return 0;
    34 }
  • 相关阅读:
    python中的赋值与深浅拷贝
    PAT甲级:1089 Insert or Merge (25分)
    PAT甲级:1064 Complete Binary Search Tree (30分)
    navicat 15 破解教程
    SQL
    以初学者的角度理解:SQL实现关系除法
    线性回归与梯度下降(ML作业)
    海明码
    CRC校验原理
    Jupyter notebook总是卡在int[*]怎么解决?
  • 原文地址:https://www.cnblogs.com/fqfzs/p/9858893.html
Copyright © 2011-2022 走看看