多校6 1001 HDU5793 A Boring Question (推公式 等比数列求和)
题解:http://bestcoder.hdu.edu.cn/blog/
1 多校6 1001 HDU5793 A Boring Question 2 3 4 // #pragma comment(linker, "/STACK:102c000000,102c000000") 5 // #include <conio.h> 6 #include <bits/stdc++.h> 7 using namespace std; 8 #define clc(a,b) memset(a,b,sizeof(a)) 9 const double inf = 0x3f3f3f3f; 10 #define lson l,mid,rt<<1 11 // #define rson mid+1,r,rt<<1|1 12 const int N = 2010; 13 const int MOD = 1e9+7; 14 #define LL long long 15 #define LB long double 16 // #define mi() (l+r)>>1 17 double const pi = acos(-1); 18 const double eps = 1e-8; 19 void fre(){freopen("in.txt","r",stdin);} 20 void freout(){freopen("out.txt","w",stdout);} 21 inline int read(){int x=0,f=1;char ch=getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1;ch=getchar();}while(ch>='0'&&ch<='9') { x=x*10+ch-'0';ch=getchar();}return x*f;} 22 23 LL q_MOD(LL a,LL n) 24 { 25 LL ans=1; 26 while(n){ 27 if(n&1)ans=ans*a%MOD; 28 n>>=1; 29 a=a*a%MOD; 30 } 31 return ans; 32 } 33 int main() 34 { 35 int t; 36 scanf("%d",&t); 37 while(t--){ 38 LL n,m; 39 scanf("%LLd%LLd",&n,&m); 40 printf("%LLd\n",(q_MOD(m,n+1)-1)*q_MOD(m-1,MOD-2)%MOD); 41 } 42 return 0; 43 }