当前位置:网站首页>Codeforces Round #100 E. New Year Garland & 2021 CCPC Subpermutation
Codeforces Round #100 E. New Year Garland & 2021 CCPC Subpermutation
2022-07-27 16:35:00 【dplovetree】
E. New Year Garland
The question :
use m m m Colored balls of different colors decorate n n n Christmas tree on the first floor . The first of Christmas tree i i i Layer just by l i l_i li A string of colored balls , And the adjacent colored balls in the same layer have different colors , At the same time, the color sets of the colored balls used in the adjacent two layers are different . Ask how many decoration schemes there are , The answer is right p p p modulus . ( n , m < = 1 e 6 , l i < = 5000 , 2 < = p < = 1 e 9 ) (n,m<=1e6,l_i<=5000,2<=p<=1e9) (n,m<=1e6,li<=5000,2<=p<=1e9), ∑ 1 n l i < = 1 e 7 \sum_{1}^{n} {l_i}<=1e7 ∑1nli<=1e7;
Ideas :
Don't consider the limitation of color set between two adjacent layers , Only consider how to calculate the different colors of adjacent colored balls on the same layer ; Consider using D P DP DP To calculate , Definition f [ i ] [ j ] f[i][j] f[i][j], The representative length is i i i And just used 1 − j 1-j 1−j The number of color schemes ( It represents the number !!! Before use j j j Color );
So the transfer is f [ i ] [ j ] = f [ i − 1 ] [ j − 1 ] + f [ i − 1 ] [ j ] ∗ ( j − 1 ) f[i][j]=f[i-1][j-1]+f[i-1][j]*(j-1) f[i][j]=f[i−1][j−1]+f[i−1][j]∗(j−1); The former i − 1 i-1 i−1 Two positions are used j − 1 j-1 j−1 Color , So the first i i i The first position must be used j j j One color , If the former i − 1 i-1 i−1 Positions have been used j j j Color , So the first i i i As long as there is disagreement between the two positions i − 1 i-1 i−1 Color conflict of ;
Sure O ( ( max 1 n l i ) 2 ) O((\max_{1}^{n}{l_i})^2) O((max1nli)2) Preprocessing f f f Array ;
Next, consider the limitations of two adjacent layers , The color sets of two adjacent layers cannot be the same ;
Definition S [ i ] S[i] S[i] representative 1 − i 1-i 1−i The total number of schemes that meet the requirements of the layer , d p [ i ] [ j ] dp[i][j] dp[i][j] representative 1 − i 1-i 1−i The layer meets the requirements and the i i i Layer used before j j j The number of color schemes ;
Easy to get S [ i ] = ∑ 1 m i n ( l i , m ) d p [ i ] [ j ] ∗ C m j ∗ j ! S[i]=\sum_{1}^{min(l_i,m)}{dp[i][j]*C_{m}^{j}*j!} S[i]=∑1min(li,m)dp[i][j]∗Cmj∗j!; That is, enumerate the second i i i The number of colors of the layer , Each scheme uses C m j C_{m}^{j} Cmj, Take out the color set , Multiply by the total arrangement of the number of colors to be the number of schemes ;
The next question is d p dp dp Array transfer , It needs to be considered that it cannot be the same as the color selection scheme of the previous layer :
d p [ i ] [ j ] = ∑ 1 m i n ( l i , m ) f [ l [ i ] ] [ j ] ∗ ( S [ i − 1 ] − d p [ i − 1 ] [ j ] ∗ j ! ) dp[i][j]=\sum_{1}^{min(l_i,m)}{f[ l[i] ][j]*(S[i-1]-dp[i-1][j]*j!)} dp[i][j]=∑1min(li,m)f[l[i]][j]∗(S[i−1]−dp[i−1][j]∗j!)
That is, enumerate the second i i i Layers are in several colors , The number of programmes is f [ l [ i ] ] [ j ] f[ l[i] ][j] f[l[i]][j], Take the front i − 1 i-1 i−1 Layer all legal schemes minus the same scheme as the current color set ;
Last S [ n ] S[n] S[n] That's the answer. ;
Then the topic needs some t r i c k trick trick To get through :
1、 It's not hard to find out , d p dp dp Arrays are scrollable , After scrolling, you can solve the problem of space ;
2、 d p dp dp It needs to be emptied when transferring , Or add a judgment , To avoid illegal transfer
3、 In the process of transfer , Yes C m j ∗ j ! C_{m}^{j}*j! Cmj∗j!, however m m m It's big , And the modulus is uncertain , There may be no inverse , But it can be found that , This formula can be simplified , After simplification : m ! ( m − j ) ! \frac{m!}{(m-j)!} (m−j)!m!, That is to say m m m Of j j j The power of descent , Can be pretreated to get ;
Code :
#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;
#define ll long long
ll mod;
ll n,m;
ll l[1000050];
ll dp[5005][5005];
ll S[1000050];
ll dpp[2][5005];
ll jie[1000050];
ll ji[5006];
int main(){
scanf("%lld%lld%lld",&n,&m,&mod);
for(int i=1;i<=n;i++)scanf("%lld",&l[i]);
dp[0][0]=1;
jie[0]=1;
ji[0]=1;
for(int i=1;i<=5000;i++)ji[i]=ji[i-1]*i%mod;
for(int i=1;i<=m;i++)jie[i]=jie[i-1]*(m-i+1)%mod;
for(int i=1;i<=5000;i++){
for(int j=1;j<=5000;j++){
dp[i][j]=(dp[i-1][j-1]%mod+dp[i-1][j]*(j-1)%mod)%mod;
}
}
int now=1;
for(int i=1;i<=min(l[1],m);i++){
S[1]=(S[1]+dp[l[1]][i]*jie[i]%mod)%mod;
dpp[now][i]=dp[l[1]][i];
}
for(int i=2;i<=n;i++){
int to=now^1;
for(int j=1;j<=min(l[i],m);j++){
if(j<=l[i-1])dpp[to][j]=dp[l[i]][j]*((S[i-1]-dpp[now][j]*ji[j]%mod+mod)%mod)%mod;
else dpp[to][j]=dp[l[i]][j]*S[i-1]%mod;
S[i]=(S[i]+dpp[to][j]*jie[j]%mod)%mod;
}
now=to;
}
printf("%lld\n",S[n]);
return 0;
}
Subpermutation
The question :
Sign interpretation :
- p n p_n pn: n The whole arrangement . for example , p 3 p_3 p3={1,2,3,1,3,2,2,1,3,2,3,1,3,1,2,3,2,1} .
- S n S_n Sn: n The set of all permutations . for example , S 3 S_3 S3={ {1,2,3},{1,3,2},{2,1,3},{2,3,1},{3,1,2},{3,2,1}}.
- f ( s , t ) f(s,t) f(s,t): s s s Medium is equal to t t t The number of consecutive subsequences of . for example , f({1,2,12,1,2},{1,2})=2.
T T T Time to ask , T < = 1 e 5 T<=1e5 T<=1e5, Every time I ask , Given n n n and m m m, 1 < = m < = n < = 1 e 6 1<=m<=n<=1e6 1<=m<=n<=1e6, For each inquiry , Calculation ∑ t ∈ S m f ( p n , t ) \sum_{t∈S_m} {f(p_n,t)} ∑t∈Smf(pn,t) m o d mod mod 1 e 9 + 7 1e9+7 1e9+7.
Ideas :
First consider , When m m m The elements are arranged in a n n n The case in the meta arrangement , And 1 − m 1-m 1−m It's in the same paragraph , Any arrangement in this paragraph is ok , Other elements can be arranged in any way , So the total number of schemes is m ! ∗ ( n − m + 1 ) ! m!*(n-m+1)! m!∗(n−m+1)!
Next , consider m m m The meta arrangement spans two adjacent n n n The arrangement of elements , You can consider A property of total permutation : Assume that the current arrangement is p 1 , p 2 , ⋯ , p n p_1 , p_2 , ⋯ , p_n p1,p2,⋯ ,pn , And meet ∃ k ∈ [ 1 , n − 1 ] ∃ k ∈ [ 1 , n − 1 ] ∃k∈[1,n−1], ∀ i ∈ [ k + 1 , n − 1 ] , p i > p i + 1 ∀ i ∈ [ k + 1 , n − 1 ] , p_i > p_{i + 1} ∀i∈[k+1,n−1],pi>pi+1 , And p k < p k + 1 p_k<p_{k+1} pk<pk+1;
Then there is the next permutation , And the arrangement of writing p 1 , p 2 , ⋯ , p k − 1 , p j , p n , p n − 1 , ⋯ , p j + 1 , p k , p j − 1 , ⋯ , p k + 1 p_1 , p_2 , ⋯ , p_{k − 1} , p_j , p_n , p_{n − 1} , ⋯ , p_{j + 1} , p_k , p_{j − 1} , ⋯ , p_{k + 1} p1,p2,⋯ ,pk−1,pj,pn,pn−1,⋯ ,pj+1,pk,pj−1,⋯ ,pk+1 , among p j > p k p_j>p_k pj>pk And is the first greater than p k p_k pk The number of .
That is, when the arrangement can be written p 1 , p 2 , ⋯ , p k < p k + 1 > p k + 2 > ⋯ > p n p_1 , p_2 , ⋯ , p_k < p_{k + 1} > p_{k + 2} > ⋯ > p_n p1,p2,⋯ ,pk<pk+1>pk+2>⋯>pn when , There is a next permutation , And will exchange the next largest number in the back to the front , Other numbers are flipped in order .
take k k k This position is called Change point , Next we follow the m m m The starting position of the meta arrangement i i i stay k k k Discuss the left and right respectively :
1、 i < = k i<=k i<=k, hypothesis m m m The elements are arranged next n n n The end position of the meta arrangement is j j j, It's not hard to find out , Next arranged 1 − j 1-j 1−j The position and current arrangement of 1 − j 1-j 1−j The numbers are the same , It also needs the present n n n The permutation of elements satisfies 1 − j 1-j 1−j and i − n i-n i−n All are 1 − m 1-m 1−m Number of numbers , The number of schemes that meet this condition is m ! ∗ ( n − m ) ! m!*(n-m)! m!∗(n−m)!, But need to satisfy i < = k i<=k i<=k, therefore m ! m! m! The whole arrangement of is too much , Need to subtract i − n i-n i−n There is no change point , Also and from m m m Selected from the number n − i + 1 n-i+1 n−i+1 Number , Arrange them in descending order , Other positions can be arranged randomly , So the number of solutions is ( n − m ) ! ∗ ( m ! − C m n − i + 1 ∗ ( m − n + i − 1 ) ! ) (n-m)!*(m!-C_{m}^{n-i+1}*(m-n+i-1)!) (n−m)!∗(m!−Cmn−i+1∗(m−n+i−1)!)
2、 i > = k + 1 i>=k+1 i>=k+1, First, consider whether the change point will be m m m Meta permutation , And the next arrangement p 1 , p 2 , ⋯ , p k − 1 , p j , p n , p n − 1 , ⋯ , p j + 1 , p k , p j − 1 , ⋯ , p k + 1 p_1 , p_2 , ⋯ , p_{k − 1} , p_j , p_n , p_{n − 1} , ⋯ , p_{j + 1} , p_k , p_{j − 1} , ⋯ , p_{k + 1} p1,p2,⋯ ,pk−1,pj,pn,pn−1,⋯ ,pj+1,pk,pj−1,⋯ ,pk+1, Exchange the next largest value p j p_j pj, Because what we need is m m m Meta permutation ( 1 − m 1-m 1−m) As a subsequence , If the change point of the next arrangement p j p_j pj stay m m m Meta permutation , Then the original sequence p k < p j p_k<p_j pk<pj, It must also be in the arrangement , This is related to the condition i > = k + 1 i>=k+1 i>=k+1 Not in conformity with , So the change point will not be m m m Meta permutation , hypothesis m m m The elements are arranged next n n n The end position of the meta arrangement is j j j, It's not hard to find out , Next arranged 1 − j 1-j 1−j The position and current arrangement of 1 − j 1-j 1−j The numbers are the same , It also needs the present n n n The permutation of elements satisfies 1 − j 1-j 1−j and i − n i-n i−n All are 1 − m 1-m 1−m Number of numbers .
because i > = k + 1 i>=k+1 i>=k+1 therefore ( i − n i-n i−n) All numbers of are in descending order , hypothesis m m m The elements are arranged next n n n The end position of the meta arrangement is j j j, Next sequence 1 − j 1-j 1−j It doesn't matter in order , The number of programmes is C m n − i + 1 ∗ ( m − n + i − 1 ) ! C_{m}^{n-i+1}*(m-n+i-1)! Cmn−i+1∗(m−n+i−1)!, At the same time, it needs to meet the change points k k k stay j − i j-i j−i Between , And i − j i-j i−j It cannot be monotonically descending , The number of programmes is ( ( n − m ) ! − 1 ) ((n-m)!-1) ((n−m)!−1), So the total number of schemes in this case is C m n − i + 1 ∗ ( m − n + i − 1 ) ! ∗ ( ( n − m ) ! − 1 ) C_{m}^{n-i+1}*(m-n+i-1)!*((n-m)!-1) Cmn−i+1∗(m−n+i−1)!∗((n−m)!−1)
So the general answer is m ! ∗ ( n − m + 1 ) ! + ∑ i = n − m + 2 n ( n − m ) ! ∗ ( m ! − C m n − i + 1 ∗ ( m − n + i − 1 ) ! ) + C m n − i + 1 ∗ ( m − n + i − 1 ) ! ∗ ( ( n − m ) ! − 1 ) m!*(n-m+1)!+\sum_{i=n-m+2}^{n}{(n-m)!*(m!-C_{m}^{n-i+1}*(m-n+i-1)!)+C_{m}^{n-i+1}*(m-n+i-1)!*((n-m)!-1)} m!∗(n−m+1)!+∑i=n−m+2n(n−m)!∗(m!−Cmn−i+1∗(m−n+i−1)!)+Cmn−i+1∗(m−n+i−1)!∗((n−m)!−1)
Because there are many groups asking , This formula needs to be simplified , After expansion, it is found that it can offset , Readers try it on their own , Here are the results :
m ! ∗ ( n − m + 1 ) ! + ( m − 1 ) ∗ m ! ∗ ( n − m ) ! − m ! ∗ ∑ i = n − m + 2 n 1 ( n − i + 1 ) ! m!*(n-m+1)!+(m-1)*m!*(n-m)!-m!*\sum_{i=n-m+2}^{n}{\frac{1}{(n-i+1)!}} m!∗(n−m+1)!+(m−1)∗m!∗(n−m)!−m!∗∑i=n−m+2n(n−i+1)!1
∑ i = n − m + 2 n 1 ( n − i + 1 ) ! = ∑ i = 1 m − 1 1 i ! \sum_{i=n-m+2}^{n}{\frac{1}{(n-i+1)!}}=\sum_{i=1}^{m-1}\frac{1}{i!} ∑i=n−m+2n(n−i+1)!1=∑i=1m−1i!1
This formula , It can be obtained by preprocessing ; Then the answer to each question is ok O ( 1 ) O(1) O(1) We have found ;
Code :
#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;
#define ll long long
const ll mod=1e9+7;
ll qp(ll x,ll n){
ll ans=1;
while(n){
if(n&1)ans=ans*x%mod;
x=x*x%mod;
n/=2;
}
return ans;
}
ll t,n,m;
ll jie[1000060];
ll inv[1000050];
ll dp[1000050];
int main(){
scanf("%lld",&t);
jie[0]=1;
for(int i=1;i<=1000000;i++)jie[i]=jie[i-1]*i%mod;
inv[1000000]=qp(jie[1000000],mod-2);
for(int i=1000000;i>=1;i--)inv[i-1]=inv[i]*i%mod;
for(int i=1;i<=1000000;i++)dp[i]=(dp[i-1]+inv[i])%mod;
while(t--){
scanf("%lld%lld",&n,&m);
ll ans=jie[m]*jie[n-m+1]%mod;
ans=(ans+(m-1)*jie[m]%mod*jie[n-m]%mod)%mod;
ans=(ans-jie[m]*dp[m-1]%mod+mod)%mod;
printf("%lld\n",ans);
}
return 0;
}
边栏推荐
- 爬取常见英文名
- google chrome revercecaptcha广告屏蔽
- Snowflake ID (go Implementation)
- DRF learning notes (V): viewset
- MapReduce instance (II): Average
- Some queries of TP5
- DRF learning notes (I): Data Serialization
- Your password does not satisfy the current policy requirements (modify MySQL password policy setting simple password)
- Mazak handwheel maintenance Mazak little giant CNC machine tool handle operator maintenance av-eahs-382-1
- 重新配置cubemx后,生成的代码用IAR打开不成功
猜你喜欢

Time series - use tsfresh for classification tasks

Kmeans implementation

Web test learning notes 01

DRF learning notes (I): Data Serialization

The image displayed online by TP5 is garbled

【论文阅读】Single- and Cross-Modality Near Duplicate Image PairsDetection via Spatial Transformer Compar

Replication of complex linked list

Time series ARIMA model

*List reversal

Leetcode234 question - simple method to judge palindrome linked list
随机推荐
Script file ‘D:\programs\anaconda3\Scripts\pip-script.py‘ is not present.
pdf 提取文字
Sudden! 28 Chinese entities including Hikvision / Dahua / Shangtang / Kuangshi / ITU / iFLYTEK have been blacklisted by the United States
The difference between select/poll/epoll
Crmeb Pro v1.4 makes the user experience more brilliant!
The class declared by the scala object keyword directly calls methods and associated objects
Product axure9 English version, using repeater repeater to realize drop-down multi selection box
KMEANS 实现
低代码是开发的未来吗?浅谈低代码平台
DRF learning notes (III): model class serializer modelserializer
插入word中的图片保持高dpi方法
Flowable process custom attribute
将IAR工程文件夹转移目录后无法进入函数定义解决
4-digit random data
word中插入度的方法
Implementation of ByteDance service grid based on Hertz framework
第31回---第52回
Replication of complex linked list
Notes on implementation and acquisition of flowable custom attributes
Opencv (I) -- basic knowledge of image