HDU 5793 A Boring Question

打表找规律。会发现答案是以1为首项,m为公比的等比数列的前n+1项和。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-8;
void File()
{
    freopen("D:\\in.txt","r",stdin);
    freopen("D:\\out.txt","w",stdout);
}
inline int read()
{
    char c = getchar();  while(!isdigit(c)) c = getchar();
    int x = 0;
    while(isdigit(c)) { x = x * 10 + c - '0'; c = getchar(); }
    return x;
}


int T;
LL n,m,ans,MOD=1000000007;

LL pow(LL a,LL b,LL p)
{
    LL res=1; a%=p;
    while(b)
    {
        if(b&1) res=(res*a)%p;
        a=(a*a)%p, b/=2;
    }
    return res;
}

LL extend_gcd(LL a,LL b,LL &x,LL &y)
{
    if(a==0&&b==0) return -1;
    if(b==0){x=1;y=0;return a;}
    LL d=extend_gcd(b,a%b,y,x);
    y-=a/b*x;
    return d;
}

LL mod_reverse(LL a,LL n)
{
    LL x,y,d=extend_gcd(a,n,x,y);
    if(d==1) return (x%n+n)%n;
    else return -1;
}

int main()
{
    scanf("%d",&T);
    while(T--)
    {
        scanf("%lld%lld",&n,&m);
        printf("%lld\n",((pow(m,n+1,MOD)-1+MOD)%MOD)*mod_reverse(m-1,MOD)%MOD);
    }
    return 0;
}

 

posted @ 2016-08-05 15:29  Fighting_Heart  阅读(144)  评论(0编辑  收藏  举报