Problem Description
There are an equation.
∑0≤k1,k2,⋯km≤n∏1⩽j<m(kj+1kj)%1000000007=?
We define that (kj+1kj)=kj+1!kj!(kj+1−kj)! . And (kj+1kj)=0 while kj+1<kj .
You have to get the answer for each n and m that given to you.
For example,if n=1 , m=3 ,
When k1=0,k2=0,k3=0,(k2k1)(k3k2)=1 ;
When k1=0,k2=1,k3=0,(k2k1)(k3k2)=0 ;
When k1=1,k2=0,k3=0,(k2k1)(k3k2)=0 ;
When k1=1,k2=1,k3=0,(k2k1)(k3k2)=0 ;
When k1=0,k2=0,k3=1,(k2k1)(k3k2)=1 ;
When k1=0,k2=1,k3=1,(k2k1)(k3k2)=1 ;
When k1=1,k2=0,k3=1,(k2k1)(k3k2)=0 ;
When k1=1,k2=1,k3=1,(k2k1)(k3k2)=1 .
So the answer is 4.
∑0≤k1,k2,⋯km≤n∏1⩽j<m(kj+1kj)%1000000007=?
We define that (kj+1kj)=kj+1!kj!(kj+1−kj)! . And (kj+1kj)=0 while kj+1<kj .
You have to get the answer for each n and m that given to you.
For example,if n=1 , m=3 ,
When k1=0,k2=0,k3=0,(k2k1)(k3k2)=1 ;
When k1=0,k2=1,k3=0,(k2k1)(k3k2)=0 ;
When k1=1,k2=0,k3=0,(k2k1)(k3k2)=0 ;
When k1=1,k2=1,k3=0,(k2k1)(k3k2)=0 ;
When k1=0,k2=0,k3=1,(k2k1)(k3k2)=1 ;
When k1=0,k2=1,k3=1,(k2k1)(k3k2)=1 ;
When k1=1,k2=0,k3=1,(k2k1)(k3k2)=0 ;
When k1=1,k2=1,k3=1,(k2k1)(k3k2)=1 .
So the answer is 4.
Input
The first line of the input contains the only integer
T
,
(1≤T≤10000)
Then T lines follow,the i-th line contains two integers n , m , (0≤n≤109,2≤m≤109)
Then T lines follow,the i-th line contains two integers n , m , (0≤n≤109,2≤m≤109)
Output
For each
n
and
m
,output the answer in a single line.
Sample Input
2 1 2 2 3
Sample Output
3 13
直接搬运原题的题解了。。
以上。。似乎第三步和第四步是一样的
倒数第二步用二项式定理就可以推出来了
#include<map>
#include<cmath>
#include<queue>
#include<vector>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
long long mod=1000000007;
inline long long power(long long x,int y)
{
long long xt=1;
while(y!=0)
{
if(y%2==1)
xt=xt*x%mod;
x=x*x%mod;
y/=2;
}
return xt;
}
int main()
{
int T;
scanf("%d",&T);
while(T>0)
{
T--;
int n,m;
scanf("%d%d",&n,&m);
printf("%I64d\n",(power(m,n+1)+mod-(long long)1)*power(m-1,mod-2)%mod);
}
return 0;
}