HDU 5793 A Boring Question(多校6)

题目链接


题意:

给定n和m,求的结果是多少


题解:

化简过程。

最后化为等比数列求和公式,m^0 + m^1 + m^2 + ... + m^n = ? = (m^(n-1) - 1) / (m - 1)


代码:

#include <algorithm>
#include <iostream>
#include <numeric>
#include <cstring>
#include <iomanip>
#include <string>
#include <vector>
#include <cstdio>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <set>
#define LL long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const double esp = 1e-6;
const double PI = 3.1415926535898;
const int INF = 0x3f3f3f3f;
using namespace std;

const long long MOD = 1e9+7;

long long pw[100010];

long long fast_pow(long long a,long long b){
    long long res = 1;
    while(b){
        if(b & 1)
            res = (res * a) % MOD;
        a = (a * a) % MOD;
        b >>= 1;
    }
    return res;
}

LL getans(LL m,LL n){
    if(m==1)
        return n+1;
    LL a = fast_pow(m,n+1) - 1;
    LL b = m - 1;
    LL ans = (a * fast_pow(b,MOD - 2) % MOD) % MOD;
    return ans;
}

int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        LL n,m;
        scanf("%lld%lld",&n,&m);
        printf("%lld\n",getans(m,n));
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值