Advertisement
deushiro

dp

Nov 25th, 2019
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.24 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5.  
  6. string sum(string s1, string s2) {
  7.     string ans;
  8.     string a, b;
  9.     if(s1.size() >= s2.size()){
  10.         a = s1;
  11.         b = s2;
  12.     }
  13.     else{
  14.         a = s2;
  15.         b = s1;
  16.     }
  17.     int r = 0;
  18.     b = string(a.size() - b.size(), '0') + b;
  19.     for(int i = a.size() - 1; i > -1; --i){
  20.         int res = (int)a[i] + (int)b[i] - 2 * '0' + r;
  21.         if(res <= 9){
  22.             ans = ans + char('0' + res);
  23.             r = 0;
  24.         } else {
  25.             r = res / 10;
  26.             ans = ans + char('0' + res % 10);
  27.         }
  28.     }
  29.     if (r) {
  30.         ans += '1';
  31.     }
  32.     reverse(ans.begin(), ans.end());
  33.     return ans;
  34. }
  35.  
  36. int main() {
  37.     ios_base::sync_with_stdio(false);
  38.     cin.tie(0);
  39.     cout.tie(0);
  40.     int n;
  41.     cin >> n;
  42.     vector<vector<string>> dp(n + 1, vector<string>(n + 1, "0"));
  43.     dp[1][1] = "1";
  44.     dp[0][0] = "1";
  45.     for(int i = 2; i <= n; ++i){
  46.         for(int k = 1; k <= i; k += 2){
  47.             for(int j = 0; j <= k; ++j){
  48.                 dp[i][k] = sum(dp[i][k], dp[i- k][j]);
  49.             }
  50.         }
  51.     }
  52.     string sum1 = "0";
  53.     for(int i = 0; i <= n; ++i){
  54.         sum1 = sum(sum1, dp[n][i]);
  55.     }
  56.     cout << sum1 << endl;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement