2019牛客暑期多校训练营(第七场)I——Chessboard(思维+组合数)

链接:https://ac.nowcoder.com/acm/contest/887/I
来源:牛客网
 

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld

题目描述

CSL likes playing tricks very much. The unlucky TL always becomes the target of CSL's tricks.

Today CSL came to trick TL again. He saw that TL has a very very large checkerboard and many glass balls. So he have an idea. He said to TL:

"What a big chessboard you have... How about play a game with me ? I will give you and ,and you can choose an arbitrarily large square area on the chessboard (assuming you choose a square area) and place a number of glass balls on each of the squares (each square should place not less than glass balls). Your placement needs to be satisfied:

If we choose squares of different rows and columns, then no matter how we choose, the total number of glass balls in this squares should always be the same, and should not greater then .

You just need to tell me how many ways you have to satisfy this. If you can't tell me, the chessboard and glass balls will all belong to me !

TL doesn't want to give the chessboard and glass ball to CSL, but he knows that the clever CSL has already worked out the answer in his mind. Can you help him solve this problem?

输入描述:

The first line of the input is a single integer T(T≤5)T (T \leq 5)T(T≤5) indicating the number of test cases.

Each of the following lines contains 2 integers and (meaning as description)

T≤5T \leq 5T≤5 , 1≤n,m≤20001 \leq n,m \leq 20001≤n,m≤2000

输出描述:

For each test case, output the answer in a single line. because the answer may be very big, so just print the result after mod 

示例1

输入

复制

5
1 1
2 1
3 1
4 1
5 1

输出

复制

1
3
9
26
73

备注:

 
 

题意:读了半天,意思就是任意不同行不同列的正方形里面数字的和总是相等的,每个数字不小于m,总和不大于n

官方题解:

看懂了就是实现最后那个公式了~~ 

上代码:

#include <iostream>
#include <cstdio>
using namespace std;
const int MAX = 1e6+100;
const int mod=998244353;
int f[MAX],finv[MAX],inv[MAX];//f是阶乘,finv是逆元的阶乘
void init(){
	inv[1]=1;
	for (int i = 2; i < MAX;i++){
		inv[i]=(mod-mod/i)*1ll*inv[mod%i]%mod;
	}
	f[0]=finv[0]=1;
	for (int i = 1; i < MAX;i++){
		f[i]=f[i-1]*1ll*i%mod;
		finv[i]=finv[i-1]*1ll*inv[i]%mod;
	}
}
int comb(int n,int m){//comb(n, m)就是C(n, m)
	if(m<0||m>n) return 0;
	return f[n]*1ll*finv[n-m]%mod*finv[m]%mod; 
}
int main(){
	init();
	int t;
	scanf("%d",&t);
	while(t--){
		int n,m;
		scanf("%d%d",&n,&m);
		int ans=0;
		for (int k = 1; k <= n;k++){
			for (int T = 0; T <= n-k*m;T++){
				ans=(ans+comb(T+2*k-1,2*k-1))%mod;
				if(T>=k) ans=(ans-comb(T+k-1,2*k-1)+mod)%mod;
			}
		}
		printf("%d\n",ans%mod);
	}
	return 0;
}
发布了195 篇原创文章 · 获赞 27 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/lgz0921/article/details/98957227