T1 on the examination
DP was found 70 min water ah, f [i] [j] i is assigned to the current position of the program j teams
We use the prefix and statistics, will be deleted i, j reverse enumeration, you can delete one-dimensional (arrays can also scroll rolled up)
1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 #include<cstring> 5 #include<string> 6 #include<algorithm> 7 #include<vector> 8 #include<map> 9 #define MAXN 110001 10 #define int long long 11 using namespace std; 12 int f[MAXN],sum[MAXN]; 13 int n,m,K; 14 const int mod=998244353; 15 int ans=0; 16 signed main() 17 { 18 // freopen("text.in","r",stdin); 19 // freopen("a.out","w",stdout); 20 scanf("%lld%lld%lld",&n,&m,&K); 21 m-=n;K-=1; 22 if(m<0||K<0) 23 { 24 printf("0\n"); 25 return 0; 26 } 27 f[0]=1;sum[0]=1; 28 for(int j=1;j<=m;++j)sum[j]=sum[j-1]+f[j]; 29 for(int i=1;i<=n;++i) 30 { 31 for(int j=m;j>=0;--j) 32 { 33 if(j-K-1>=0) 34 f[j]=(sum[j]-sum[j-K-1]+mod)%mod; 35 else 36 f[j]=sum[j]%mod; 37 // printf("%lld f[%lld]=%lld\n",i,j,f[j]); 38 } 39 sum[0]=f[0]; 40 for(int j=1;j<=m;++j) 41 { 42 sum[j]=(sum[j-1]+f[j]+mod)%mod; 43 } 44 } 45 printf("%lld\n",f[m]%mod); 46 } 47 /* 48 3 7 3 49 */
Correct answer is the inclusion-exclusion
Every enumeration has at least i do not meet the conditions, it is clear that meet the inclusion and exclusion Theorem
And C (n, i) n represents any of cities in the selected i-th non-compliance
C (m-1-i * K, n-1) is clearly baffle method, i * K now meet the conditions of this section are not deleted, then in the remaining zone
Insert the n-1 board, it means is divided into n parts,
And because C (n, i) is selected i do not meet, and then multiplied, is at least the case when all i
We found that at least include the case of i +, i + 2 ..... case i. 1,
So we can see that the situation i + 1 can be seen as at least as when i, determine the i, they chose a
At least one example,
Selected for the case 2 is C (2,1) times (two numbers set 2 are selected from a set of repeated too)
Then add about inclusion and exclusion at least changed to 2:00
As proved more rigorous inclusion and exclusion, you can see other people blog, I will not permit the .......
1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 #include<cstring> 5 #include<string> 6 #include<algorithm> 7 #include<vector> 8 #include<map> 9 #define MAXN 31000001 10 #define int long long 11 using namespace std; 12 const int mod=998244353; 13 int n,m,K; 14 int ni_c[MAXN],jie[MAXN],ni[MAXN]; 15 int C(int x,int y) 16 { 17 if(y>x)return 0; 18 if(y==0)return 1; 19 return jie[x]*ni_c[y]%mod*ni_c[x-y]%mod; 20 } 21 signed main() 22 { 23 scanf("%lld%lld%lld",&n,&m,&K); 24 int ans=0; 25 jie[1]=1;ni[1]=1 ; ni_c [ 1 ] = 1 ; 26 Jie [ 0 ] = 1 ; nine [ 0 ] = 1 ; ni_c [ 0 ] = 1 ; 27 for ( int i = 2 ; i <= m + 1 ; ++ i) 28 { 29 Jie [i] = Jie [i- 1 ] * as% v; 30 nine [i] = (v-v / i) * Nine [v% i]% v; 31 ni_c [i] = ni_c [i- 1 ] * Nine [i]% v; 32 } 33 ans = C (m-1,n-1)%mod; 34 int base=n; 35 for(int i=1;i<=n;++i) 36 { 37 if(m-(i*K)<n)break; 38 if((i%2)==0) 39 { 40 ans=(ans+base*C(m-1-(i*K),n-1)%mod+mod)%mod; 41 } 42 else 43 { 44 ans = (ANS base * C (M 1 - (I * K), n 1 )% v + v)% v; 45 } 46 Base = ( Base * (n) v%) * Nine [i + 1 ]% v; 47 } 48 printf ( " % lld \ n " , ans% MOD); 49 }