D. Dandelion, 吉比特杯,WHU校赛2019

本文探讨了一个关于蒲公英种子飞行路径的问题,通过数学方法计算种子从起点到终点的不同路径数量,并介绍了一种利用费马小定理求逆元来解决大数组合问题的有效算法。

D. Dandelion

time limit per test1.0 s
memory limit per test256 MB
inputstandard input
outputstandard 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), the next second it will only appear in either point (x,y+1) or point (x+1,y). All points (x,y) on the path must satisfy the constraint that x is less than y(i.e x<y.

Now, there is a dandelion seed at point (0,0), please find the number of ways(mod 109+7) to reach the point (m,n). 0<m<n≤100000.

Input

The first line has a integer T (T<10), it means the number of the case.

The following lines of input will be two integers: m and n.

Output

For each case, print an integer in a single line to represent the answer.

Example
inputCopy

3
3 4
4 5
999 1000

outputCopy

5
14
894965608

思路:
在一个格点阵列中,从 (0,0) 点走到( m, n) 点且不经过对角线 x = y 的方法数 (x < y): C(n + m−1,m)−C(n + m−1,m−1)。
在一个格点阵列中,从 (0,0) 点走到 (m, n) 点且不穿过对角线 x = y 的方法数 (x <= y): C(n + m,m)−C(n + m,m−1)。

这里用公式C(n + m−1,m)−C(n + m−1,m−1)。

此题n, m较大, 一般求组合数的方法不行,得用费马小定理求逆元
关于逆元与组合数看这篇博客


#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#include<string>
#include<vector>
#include<stack>
using namespace std;
typedef long long ll;
const int maxn = 100000, mod = 1e9 + 7;
int fac[maxn * 2];

int pow_mod(int a, int b)
{
	int base = a, res = 1;
	while(b)
	{
		if(b & 1) res = (ll) res * base % mod;
		
			base = (ll) base * base % mod;
		b >>= 1;
	}

	return res;
}

int solve(int a, int b)    				//solve()函数求得从a个中取b个的组合数。
{
	int temp = (ll) fac[b] * fac[a - b] % mod;
	int re = (ll)fac[a] * pow_mod(temp, mod - 2) % mod;
	return re;
}

int main()
{
	ios::sync_with_stdio(false);
	int t;
	cin >> t;
	fac[0] = 1;
	for(int i = 1; i <= 200000; i++) fac[i] = (ll)i * fac[i - 1] % mod;
	while(t--)
	{
		int m, n;
		cin >> m >> n;
		cout << (solve(m + n - 1, m) - solve(m + n -1, m - 1) + mod) % mod << endl;
	}
}
`dandelion.out` 和 `catalina.out` 通常在不同的应用场景中使用,以下是它们的区别: ### 所属应用 - `dandelion.out`:并非通用标准的日志文件,一般是特定应用程序自定义生成的输出日志文件。该特定应用可能是某个公司或开发者自主开发的软件,使用 `dandelion.out` 来记录其运行过程中的各类信息。 - `catalina.out`:是 Apache Tomcat 服务器的标准日志文件。Tomcat 作为广泛使用的开源 Servlet 容器,`catalina.out` 用于记录 Tomcat 服务器启动、运行和关闭过程中的关键信息。 ### 记录内容 - `dandelion.out`:记录的内容取决于特定应用的设计,可能包含应用程序的业务逻辑执行信息、错误信息、调试信息等。例如,某个自定义的数据分析应用可能会在 `dandelion.out` 中记录数据处理的进度、分析结果的概要等。 - `catalina.out`:主要记录 Tomcat 服务器本身的启动和关闭信息,以及未被其他日志文件捕获的全局错误信息。比如,当 Tomcat 启动时,会在 `catalina.out` 中记录 Java 虚拟机的配置信息、加载的类库等;若出现严重错误导致服务器无法正常启动,错误信息也会输出到该文件中。 ### 日志配置 - `dandelion.out`:其日志配置由应用开发者根据需求进行设置。开发者可以决定日志的输出级别(如 DEBUG、INFO、WARN、ERROR)、日志的存储路径、日志文件的滚动策略(如按时间、按文件大小)等。 - `catalina.out`:Tomcat 的日志配置可以通过 `logging.properties` 文件进行调整。可以修改日志的输出格式、输出级别等参数,但它有自己默认的配置规则和方式。 ### 示例代码 以下是一个简单的 Python 示例,用于模拟查看这两个日志文件的最后 10 行内容: ```python import subprocess # 查看 dandelion.out 最后 10 行 try: result_dandelion = subprocess.run(['tail', '-n', '10', 'dandelion.out'], capture_output=True, text=True) print("dandelion.out 最后 10 行内容:") print(result_dandelion.stdout) except FileNotFoundError: print("dandelion.out 文件未找到") # 查看 catalina.out 最后 10 行 try: result_catalina = subprocess.run(['tail', '-n', '10', 'catalina.out'], capture_output=True, text=True) print("catalina.out 最后 10 行内容:") print(result_catalina.stdout) except FileNotFoundError: print("catalina.out 文件未找到") ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值