2019湖北省省赛 D题题解

解析了湖北省网络赛D题“Dandelion”的算法题解,探讨了种子从起点到终点的不同路径数量计算,利用费马小定理求逆元,通过组合数学中的C(n+m-1,m)和C(n+m-1,m-1)公式解决x<y约束条件下的路径计数问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

湖北省网络赛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;
}

据说 这题是原题啊~~~

稍后 放上原址……

http://www.51nod.com/Blog/Index.html#!#blogId=12

 

Alone的题解也来一发

https://blog.youkuaiyun.com/qq_41279172/article/details/89165744

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值