codeforces 1288C. Two Arrays(dp)

Link: https: //codeforces.com/contest/1288/problem/C

C. Two Arrays

The meaning of problems: Given a number n and a number m, so constructed two arrays a and b satisfy the condition, a value of all elements in the array between 1 ~ n, a and b are the length of the array is m.. 2. a arrays are monotonically decreasing, b array 3 is not incremented monotonically arbitrary position i, there are A I <B = I

 

Idea: You can do a combination of mathematics, can also dp, dp following is practice. First, if a, b merge into two arrays A . 1 , A 2 , A . 3 , ....... A m , B m , B m-. 1 , B m-2 , B m. 3- ... b ........ 3 , b 2 , b 1, could find that the number of columns is not monotonically decreasing, then we can do dp,

DP [i] [j] denotes the i-th position may be enlarged to the number of programs equal to j, then the transfer equation is dp [i] [j] = dp [i-1] [j] + dp [i] [j + 1]

 

AC Code:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<vector>
 6 #include<queue> 
 7 using namespace std;
 8 typedef long long ll;
 9 const int maxm = 12;
10 const int maxn = 1e3+5;
11 const int mod = 1e9+7;
12 ll dp[maxm*2][maxn]; 
13 int main(){
14     int n,m;
15     scanf("%d%d",&n,&m);
16     for(int i = 1;i<=n;i++) dp[1][i] = 1;
17     for(int i = 2;i<=2*m;i++){
18         for(int j = n;j>=1;j--){
19             dp[i][j] = (dp[i][j+1] + dp[i-1][j])%mod;
20         }
21     }
22     ll ans = 0;
23     for(int i = 1;i<=n;i++){
24         ans = (ans+dp[2*m][i])%mod;
25     }
26     printf("%d",ans);
27     return 0;
28 }

Guess you like

Origin www.cnblogs.com/AaronChang/p/12210864.html