Advertisement
_ash__

Untitled

Dec 28th, 2020
477
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.50 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define Gene template< class
  5. #define Rics printer& operator,
  6. Gene c> struct rge{c b, e;};
  7. Gene c> rge<c> range(c i, c j){ return {i, j};}
  8. struct printer{
  9.     ~printer(){cerr<<endl;}
  10.     Gene c >Rics(c x){ cerr<<boolalpha<<x; return *this;}
  11.     Rics(string x){cerr<<x;return *this;}
  12.     Gene c, class d >Rics(pair<c, d> x){ return *this,"(",x.first,", ",x.second,")";}
  13.     Gene ... d, Gene ...> class c >Rics(c<d...> x){ return *this, range(begin(x), end(x));}
  14.     Gene c >Rics(rge<c> x){
  15.         *this,"["; for(auto it = x.b; it != x.e; ++it)
  16.             *this,(it==x.b?"":", "),*it; return *this,"]";}
  17. };
  18. #define debug() cerr<<"LINE "<<__LINE__<<" >> ", printer()
  19. #define dbg(x) "[",#x,": ",(x),"] "
  20. mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  21. int my_rand(int l, int r) {
  22.     return uniform_int_distribution<int>(l, r) (rng);
  23. }
  24.  
  25. const int N = 5e6+10, M = 20;
  26. const int mod = 1e9+7;
  27.  
  28. int C[M][M];
  29. int dp[N][2][11];
  30.  
  31. int main() {
  32. //    freopen("in.txt", "r", stdin);
  33.     ios::sync_with_stdio(0);
  34.     cin.tie(0);
  35.  
  36.     int n, m;
  37.     cin >> n >> m;
  38.     string s;
  39.     cin >> s;
  40.     s = "$" + s;
  41.  
  42.     vector<int> sub(n+1);
  43.  
  44.     for(int u = n; u >= 1; u--) {
  45.         vector<int> ways(m+1);
  46.         dp[u][0][0] = 1;
  47.         if(s[u] == '0') dp[u][1][1] = 1;
  48.         sub[u] = 1;
  49.         for(int v : {2*u, 2*u+1}) {
  50.             if(v > n) continue;
  51.             vector<int> newDp0(m+1), newDp1(m+1);
  52.             for(int x = 0; x <= m && x <= sub[u]; x++) {
  53.                 for(int y = 0; x+y <= m && y <= sub[v]; y++) {
  54.                     newDp0[x+y] += 1ll*dp[u][0][x]*(dp[v][0][y]+dp[v][1][y])%mod;
  55.                     newDp1[x+y] += 1ll*dp[u][1][x]*dp[v][0][y]%mod;
  56.                     if(newDp0[x+y] >= mod) newDp0[x+y] -= mod;
  57.                     if(newDp1[x+y] >= mod) newDp1[x+y] -= mod;
  58.                 }
  59.             }
  60.             sub[u] += sub[v];
  61.             for(int k = 0; k <= m; k++) dp[u][0][k] = newDp0[k], dp[u][1][k] = newDp1[k];
  62.         }
  63. //        debug(), dbg(dp[u]);
  64.     }
  65.  
  66.     for(int i = 0; i < M; i++) {
  67.         C[i][0] = C[i][i] = 1;
  68.         for(int j = 1; j < i; j++) {
  69.             C[i][j] = (C[i-1][j]+C[i-1][j-1])%mod;
  70.         }
  71.     }
  72.  
  73.     int totalWays = 0;
  74.  
  75.     for(int k = 1; k <= m; k++) {
  76.         int ways = (dp[1][0][k]+dp[1][1][k])%mod;
  77.         ways = 1ll*ways*C[m-1][k-1]%mod;
  78.         totalWays += ways;
  79.         totalWays %= mod;
  80.     }
  81.     cout << totalWays << endl;
  82.  
  83. }
  84.  
  85.  
  86.  
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement