codeforces 1284C

Topic Link

The meaning of problems

A defined arrangement a a one interval [ l , r ] [l,r] is good if and only if a l al to a r With maximum value - minimum value = rl.
seeking all permutations of length n in the total number of intervals a good answer to a given modulo prime number

solution

First, look at the subject sample, l = r can be found to meet the requirements of a certain interval, this interval being n ! × n n! \times n a, n and the length of section must also meet the requirements, such interval being n ! n! months, which inspired me is the number of program can you calculate the range of different lengths?

Then wrote a violent, discovered that in fact very regular. Specifically: the number and total length of the total length of the good arrangement of n sections of length l is (n-1) are arranged in longitudinal section as well the number of l-1 is closely related. Then push a push equation, the problem can be solved

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxn=3e5+5;
inline int read(){
	char c=getchar();int t=0,f=1;
	while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
	while(isdigit(c)){t=(t<<3)+(t<<1)+(c^48);c=getchar();}
	return t*f;
}
int n,m,ans;
int rec[maxn];
signed main(){
	//freopen("3.in","r",stdin);
	//freopen("3.out","w",stdout);
	n=read(),m=read();
	rec[1]=1;
	for(int i=2;i<=n;i++)rec[i]=rec[i-1]*i%m;
	for(int i=n;i>=1;i--){
		ans=(ans+rec[i]*i%m*rec[n-i+1]%m)%m;
	//printf("%lld\n",rec[i]*i*rec[n-i+1]);
	}
	printf("%lld\n",ans);
	return 0;
}

Probably prove: For the total length after length n-1 is a good range l-1, the total length increased to n, the number of new entrants can be placed in this position l range, it will be extra l kinds of programs.

Published 62 original articles · won praise 1 · views 995

Guess you like

Origin blog.csdn.net/wmhtxdy/article/details/103846858