HDU 5793 A Boring Question (数学)

来源:互联网 发布:psd头像源码怎么使用 编辑:程序博客网 时间:2024/05/23 01:45

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=5793

题解:

2016多校训练赛6的第一题。。。开始没发现打表能够找出规律,汗,打表后找出规律发现是个等比数列,直接求和公式搞一发就好了,记得用乘法逆元和快速幂取模。

AC代码:

#include <iostream>#include <cstring>#include <algorithm>#include <cstdio>#define ll long long intusing namespace std;const ll N = 1000000007;ll multi(ll a, ll b){    ll re = 1;    while(b > 0)    {        if(b & 1)        {            re = (re * a)%N;        }        a = (a * a)%N ;        b >>= 1;    }    return re%N;}ll extgcd(ll a, ll b, ll &x, ll &y){    ll d = a;    if(b == 0LL)    {        x = 1; y = 0;    }else    {        d = extgcd(b, a % b, y, x);        y -= (a / b) * x;    }    return d;}ll mod_inverse(ll a, ll MOD){    ll x, y;    extgcd(a, MOD, x ,y);    return (x % MOD + MOD) % MOD;}//乘法逆元int main(){    ll t,n,m,ans,x,y;    scanf("%lld",&t);    while(t--)    {        scanf("%lld %lld", &n, &m);        ans = 0;        if(n == 0)            ans = 1;        else            ans = ((multi(m,n+1) - 1) * mod_inverse(m-1, N) )% N;        printf("%lld\n", ans);    }    return 0;}
0 0