(多校第六场1001)HDU5793 A Boring Question(逆元,等比数列和取模)

来源:互联网 发布:淘宝的名星同款的图片 编辑:程序博客网 时间:2024/05/17 02:30

推出公式就好了,就是计算m^0+m^1+.....+m^n;




#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<algorithm>#include<queue>#include<stack>#include<set>#include<map>#include<climits>#define LL long long#define  MOD  int(1e9+7)using namespace std;LL qpow(LL a, LL b){    LL res = 1;    while (b > 0)    {        if (b & 1)            res = (res * a) % MOD;        a = (a * a) % MOD;        b >>= 1;    }    return res;}LL _getInverse(LL a, LL p){    return qpow(a, p - 2);}LL getAns(LL m, LL n) //m^0+m^1+....+m^n{    if (m == 1)        return n + 1;    return (qpow(m, n + 1) - 1) * _getInverse(m - 1, MOD) % MOD;}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
原创粉丝点击