湖北省网络赛D题题解
D. Dandelion
time limit per test
1.0 s
memory limit per test
256 MB
input
standard input
output
standard output
"O dandelion, yellow as gold,
What do you do all day?"
"I just wait here in the tall green grass
Till the children come to play."
"O dandelion, yellow as gold,
What do you do all night?"
"I wait and wait till the cool dews fall
And my hair grows long and white."
"And what do you do when your hair is white
And the children come to play?"
"They take me up in their dimpled hands
And blow my hair away!"
Spring is here and the dandelion is blooming. There is a small dandelion seed taking off. It will float upwards when there is no wind, and when the wind blows up it will be blown to the right.
In other words, if a dandelion seed is currently at point (x,y)(x,y), the next second it will only appear in either point (x,y+1)(x,y+1) or point (x+1,y)(x+1,y). All points (x,y)(x,y) on the path must satisfy the constraint that x is less than y(i.e x<yx<y.
Now, there is a dandelion seed at point (0,0)(0,0), please find the number of ways(mod 109+7109+7) to reach the point (m,n)(m,n). 0<m<n≤1000000<m<n≤100000.
Input
The first line has a integer TT (T<10T<10), it means the number of the case.
The following lines of input will be two integers: mm and nn.
Output
For each case, print an integer in a single line to represent the answer.
Example
input
Copy
3
3 4
4 5
999 1000
output
Copy
5
14
894965608
思路:
最开始我们选择打表出10*10 的数据结果
如下图:
假设不存在 x < y 不能通过的情况 即所有的路都能走
得到下图 (同时我也根据上图找到了规律……在已知题解的情况下找规律)
官方题解说的是 :
用费马小定理求逆元,然后直接套公式。
ans = C(n+m-1,m) - C(n+m-1,m-1)
C(n+m-1,m) 表示的是 不存在 x < y 不能走的情况
C(n+m-1,m-1) 表示的是 考虑不能走的情况
所以 两者相减
以下是 官方给的标程:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 500005, mod = 1e9 + 7;
int mi[maxn];
inline int quick(int a, int b) {
int res = a, ans = 1;
for (int i = 0; i < 31; i ++) {
if (1 << i & b) ans = (long long)ans * res % mod;
res = (long long)res * res % mod;
}
return ans;
}
int C(int n, int m) {
int tmp = (long long)mi[n - m] * mi[m] % mod;
int ans = (long long)mi[n] * quick(tmp, mod - 2) % mod;
return ans;
}
int main() {
int n, m;
mi[0] = 1;
for (int i = 1; i <= 200000; i ++) mi[i] = (long long)mi[i - 1] * i % mod;
int t;
scanf("%d",&t);
while (t--) {
scanf("%d%d", &m, &n);
printf("%d\n", (C(n + m - 1, m) - C(n + m - 1, m - 1) + mod) % mod);
}
return 0;
}
据说 这题是原题啊~~~
稍后 放上原址……