2021 Niu Guest Winter Holiday Algorithm Basic Training Camp 5 B. Martial Arts Competition (Part 1)

B. Martial Arts Competition (Part 1)

Title link: https://ac.nowcoder.com/acm/contest/9985/B

Title description:

As we all know, Sister Tian only likes the smartest people in the world. In order to find such a person, she decided to compete with her!

I saw that Sister Tian left such a question on the list, whoever did it could capture her heart!

Sister Ai Mutian’s long-awaited Ze Gege inquiry came, and I saw that the list read:

Given n, m, define a sequence, the construction method is as follows:

1. Choose m times in [1,n] arbitrarily, and get m integers (obviously the numbers may be the same);

2. After sorting the selected m numbers, a sequence {a1, a2,..., am} is obtained.

Define the contribution of a sequence as max{a1,a2,...,am}−min{a1,a2,...,am}, and find the sum of the contributions of all essentially different sequences.

In order to prevent the result from being too large, the answer is 998244353 and output after taking the modulo.

(For two sequences A and B with sequence length m, if ∃i∈[1,m], Ai≠Bi, the sequences A and B are essentially different)

Ze Ge Ge has plenty of heart but not enough strength, and you, as his best friend, decided to help Ze Ge Ge capture the heart of beauty!

Now, this important task is left to you!

Enter a description:

Enter two positive integers n, m in one line

[Data scale and agreement]

1 <= n, m <= 5*10^5

Output description:

One integer per line is the result of the answer modulo 998244353.

Example 1:

Input
3 2
Output
4
Description

There are several essentially different sequences: 1 1, 2 2, 3 3, 1 2, 1, 3, 2 3, and the contribution is 0+0+0+1+2+1=4.

Problem-solving ideas:

Diaphragm method
Contribution is the maximum-minimum value (after the maximum and minimum values ​​of a sequence are determined, other numbers are arbitrarily selected within this range)

For example:
max=10,min=1
max=11,min=2
max=12,min=3
……The number of methods is the same

Enumerate the difference x, when max-min=x, min takes values ​​from 1 to nx, there are a total of nx types, and the range from min to max is a monotonous and undecreasing sequence (sorted), with m-2 numbers in between. , Each time the number plus one is regarded as a partition (the actual meaning of the partition), m-3 vacancies are placed d partitions, and each partition is forced to have a vacancy (it is worth noting: the first The partition can only be placed in the second position, otherwise the self-contained vacancy will be Wuhu lia, so the first place cannot be inserted into the partition.) Therefore m+d vacancies put d partitions, which is C(d,m+d-) 2).

code show as below:

#include<algorithm>
#include<iostream>
#include<cstdio>
using namespace std;
#define ll long long
const ll mod=998244353;
ll qsm(ll a,ll b){
    
    
	ll res=1;
	while(b){
    
    
		if(b&1)
			res=(res*a)%mod;
		a=(a*a)%mod;
		b>>=1;	
	}
	return res;
}
int main(){
    
    
	ll n,m;
	scanf("%lld%lld",&n,&m);
	ll ans=0;
	ll x=m-1;
	for(ll i=1;i<n;i++){
    
    
		ans=(ans+i*x%mod*(n-i))%mod;
		x=x*(m+i-1)%mod*qsm(i+1,mod-2)%mod;
	}
	printf("%lld\n",ans);
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_45894701/article/details/113949789