CF1284C·New Year and Permutation

初見安~這裏是傳送門:Codeforces 1284C

Sol

這個題很有意思呀。就是給你一個n,求n的所有排列裏區間左右端點的差等於區間內極差的區間的和。

其實手枚一下就可以發現,其實滿足這個條件的區間內的數一定是連續的

所以我們從區間長度入手,對於每個長度的區間看對應了多少個排列滿足要求

這就很簡單了呀。我們枚舉區間的極差i,考慮選差值爲i的i+1個數有n-i種選法,區間內的順序隨意,整體看這i+1個數爲一組,另外的n-i-1個數爲n-i-1個組,這n-i個組的順序同樣隨意。

所以就有:

\small ans =\sum_{i=0}^{n-1}(n-i)*(i+1)!(n-i)!

然後這個題就沒了。

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
#define maxn 300005
using namespace std;
typedef long long ll;
int read() {
	int x = 0, f = 1, ch = getchar();
	while(!isdigit(ch)) {if(ch == '-') f = -1; ch = getchar();}
	while(isdigit(ch)) x = (x << 1) + (x << 3) + ch - '0', ch = getchar();
	return x * f;
}

int n, mod;
ll ans = 0, fac[maxn];
signed main() {
	n = read(), mod = read();
	fac[0] = 1;
	for(int i = 1; i <= 300000; i++) fac[i] = fac[i - 1] * i % mod;
	for(int i = 0; i < n; i++) ans = (ans + (n - i) * fac[i + 1] % mod * fac[n - i] % mod) % mod;
	printf("%lld\n", ans % mod);
	return 0;
}
 

迎評:)
——End——

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章