A Boring Question
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 635 Accepted Submission(s): 375
Problem Description
There are an equation.
∑
0≤k
1
,k
2
,⋯k
m
≤n
∏
1⩽j<m
(k
j+1![]()
k
j![]()
)%1000000007=?
We define that (k
j+1![]()
k
j![]()
)=k
j+1
!
k
j
!(k
j+1
−k
j
)!![]()
![]()
. And
(k
j+1![]()
k
j![]()
)=0
while
k
j+1
<k
j![]()
.
You have to get the answer for each n
and
m
that given to you.
For example,if n=1
,
m=3
,
When k
1
=0,k
2
=0,k
3
=0,(k
2![]()
k
1![]()
)(k
3![]()
k
2![]()
)=1
;
When k
1
=0,k
2
=1,k
3
=0,(k
2![]()
k
1![]()
)(k
3![]()
k
2![]()
)=0
;
When k
1
=1,k
2
=0,k
3
=0,(k
2![]()
k
1![]()
)(k
3![]()
k
2![]()
)=0
;
When k
1
=1,k
2
=1,k
3
=0,(k
2![]()
k
1![]()
)(k
3![]()
k
2![]()
)=0
;
When k
1
=0,k
2
=0,k
3
=1,(k
2![]()
k
1![]()
)(k
3![]()
k
2![]()
)=1
;
When k
1
=0,k
2
=1,k
3
=1,(k
2![]()
k
1![]()
)(k
3![]()
k
2![]()
)=1
;
When k
1
=1,k
2
=0,k
3
=1,(k
2![]()
k
1![]()
)(k
3![]()
k
2![]()
)=0
;
When k
1
=1,k
2
=1,k
3
=1,(k
2![]()
k
1![]()
)(k
3![]()
k
2![]()
)=1
.
So the answer is 4.
∑
We define that (k
You have to get the answer for each n
For example,if n=1
When k
When k
When k
When k
When k
When k
When k
When k
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≤10
9
,2≤m≤10
9
)
Then T
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 <algorithm>
#include <cstdio>
#include <vector>
#include <iostream>
using namespace std;
const int mod = 1000000007;
int n,m;
long long quickmod(long long a,long long b)
{
long long ans = 1;
while(b)//用一个循环从右到左便利b的所有二进制位
{
if(b&1)//判断此时b[i]的二进制位是否为1
{
ans = (ans*a)%mod;//乘到结果上,这里a是a^(2^i)%m
b--;//把该为变0
}
b/=2;
a = a*a%mod;
}
return ans;
}
int main()
{
int t;
cin>>t;
while(t--)
{
cin>>n>>m;
cout<<(long long)((quickmod(m,n+1)-1)%mod*(quickmod(m-1,mod-2)%mod))%mod<<endl;
}
return 0;
}