zoukankan      html  css  js  c++  java
  • HDU 6027

    题意

    给出n, k, 已知: f(i)=i^k , 求 sum = f(1)+f(2)+…+f(n) , 并将结果模10e9+7

    思路

    其实这个题直接暴力就能暴过去
    用快速幂的时候忘了中间的a也要用LL结果WA了两发


    快速幂取模算法详解
    快速幂
    快速幂板子

    ll quickmi(ll a, ll b){
        ll ans = 1;
        a %= mod;
        while(b){
            if( b & 1 )  ans = (ans*a) % mod;
            b >>= 1;
            a = (a*a) % mod;
        }
        return ans;
    }

    AC代码

    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    
    using namespace std;
    typedef long long ll;
    const ll mod = 1e9+7;
    const int maxn = 1e4+5;
    
    ll quickmi(ll a, ll b){
        ll ans = 1;
        a %= mod;
        while(b){
            if( b & 1 )  ans = (ans*a) % mod;
            b >>= 1;
            a = (a*a) % mod;
        }
        return ans;
    }
    
    int main()
    {
        int T;
        ll n, k;
        scanf("%d",&T);
        while(T--){
            scanf("%lld%lld", &n, &k);
            ll sum = 0;
            for( ll i = 1; i <= n; i++ )
                sum = (sum + quickmi(i, k)) % mod;
            printf("%lld
    ", sum % mod);
        }
        return 0;
    }
  • 相关阅读:
    iOS数据持久化的方式
    Runtime
    <02>
    <01>
    <02>
    UIActivityIndicatorView
    <01>数据存储
    UI<10>
    UI<09>
    UI<08>
  • 原文地址:https://www.cnblogs.com/JinxiSui/p/9740568.html
Copyright © 2011-2022 走看看