deadwing97

MDN Editorialist

Jun 27th, 2019
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.81 KB | None | 0 0
  1.  
  2. #include<bits/stdc++.h>
  3.  
  4. using namespace std;
  5.  
  6. const int MX = (1<<20);
  7.  
  8. typedef long long ll;
  9.  
  10. int n , M , K , arr[MX];
  11.  
  12. int dp[2][105][55];
  13.  
  14. int MOD = 1e9 + 7;
  15.  
  16. int main(){
  17.  
  18.     scanf("%d %d %d",&n,&K,&M);
  19.  
  20.     for(int j = 1 ; j <= n ; j++){
  21.         scanf("%d",&arr[j]);
  22.     }
  23.  
  24.     for(int j = 1 ; j <= n ; j++){
  25.         if(arr[j] >= M) arr[j] = 1;
  26.         else arr[j] = 0;
  27.     }
  28.  
  29.     dp[0][0][0] = 1;
  30.  
  31.     int cur = 1;
  32.  
  33.     long long ans = 0;
  34.  
  35.     int upper = 51;
  36.  
  37.     for(int pos = 1 ; pos <= n ; pos++){
  38.  
  39.         dp[cur][0][0] = dp[cur^1][0][0];
  40.  
  41.         dp[cur][1][0] = dp[cur^1][1][0];
  42.  
  43.         dp[cur][1][1] = dp[cur^1][1][1];
  44.  
  45.         dp[cur][1][arr[pos]] += pos; dp[cur][1][arr[pos]] %= MOD;
  46.  
  47.         for(int take = 2 ; take <= min(pos , K) ; take++){
  48.  
  49.             for(int sum = 0 ; sum < upper ; sum++){
  50.  
  51.                 dp[cur][take][sum] = dp[cur^1][take][sum];
  52.  
  53.                 dp[cur][take][sum] += dp[cur^1][take - 1][sum - arr[pos]];
  54.  
  55.                 dp[cur][take][sum] %= MOD;
  56.             }
  57.  
  58.  
  59.             if(take >= upper){
  60.  
  61.                 dp[cur][take][upper] = dp[cur^1][take][upper];
  62.  
  63.  
  64.                 dp[cur][take][upper] += dp[cur^1][take - 1][upper];
  65.                 dp[cur][take][upper] %= MOD;
  66.  
  67.  
  68.                 if(arr[pos] == 1)
  69.                     dp[cur][take][upper] += dp[cur^1][take - 1][upper - arr[pos]];
  70.  
  71.                 dp[cur][take][upper] %= MOD;
  72.             }
  73.  
  74.         }
  75.  
  76.  
  77.         for(int take = (K + 1)/2 ; take <= min(K , upper) ; take++){
  78.             long long theta = dp[cur][K][take] - dp[cur^1][K][take];
  79.             theta += MOD; theta %= MOD;
  80.             theta *= (n - pos + 1);
  81.             theta %= MOD;
  82.             ans += theta;
  83.             ans %= MOD;
  84.         }
  85.  
  86.         cur ^= 1;
  87.     }
  88.  
  89.     cout<<ans<<endl;
  90.  
  91. }
Add Comment
Please, Sign In to add comment