Helicator

qbpal.cpp

Nov 7th, 2021
1,052
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.91 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define int long long
  5. #define vi vector<int>
  6. #define ii pair<int,int>
  7. #define fi first
  8. #define sc second
  9. #define stoi stoll
  10. #define popcnt __builtin_popcount
  11. #define getbit(x, k) ((x >> k) & 1)
  12. #define all(x) (x).begin(),(x).end()
  13.  
  14. typedef vector<int> bigNum;
  15.  
  16. string s;
  17. bigNum f[125][125];
  18.  
  19. const int BASE = 1e9;
  20.  
  21. bigNum operator+(bigNum a, bigNum b){
  22.     while (a.size() < b.size()) a.push_back(0);
  23.     while (a.size() > b.size()) b.push_back(0);
  24.     int n = a.size(), d = 0;
  25.     bigNum c; c.resize(n);
  26.     for (int i = 0; i < n; i++){
  27.         c[i] = (a[i] + b[i] + d) % BASE;
  28.         d = (a[i] + b[i] + d) / BASE;
  29.     }
  30.     if (d) c.push_back(d);
  31.     return c;
  32. }
  33.  
  34. bigNum operator-(bigNum a, bigNum b) {
  35.     while (b.size() < a.size()) b. push_back(0);
  36.     int n = a.size(), d = 0;
  37.     bigNum c; c.resize(n);
  38.     for (int i = 0; i < n; i++){
  39.         if (a[i] >= b[i] + d){
  40.             c[i] = a[i] - (b[i] + d);
  41.             d = 0;
  42.         }
  43.         else{
  44.             c[i] = (a[i] + BASE) - (b[i] + d);
  45.             d = 1;
  46.         }
  47.     }
  48.     return c;
  49. }
  50.  
  51. ostream& operator<<(ostream &os, bigNum a){
  52.     int n = a.size();
  53.     cout << a[n-1];
  54.     for (int i = n - 2; i >= 0; i--){
  55.         cout << setw(9) << setfill('0') << a[i];
  56.     }
  57.     return os;
  58. }
  59.  
  60. bigNum cal(int a, int b)
  61. {
  62.     if (a > b) return bigNum(1,1);
  63.     if (a == b) return bigNum(1,2);
  64.     if (f[a][b] != bigNum(1,-1)) return f[a][b];
  65.    
  66.     bigNum ans = cal(a+1,b) + cal(a,b-1) - cal(a+1,b-1);
  67.     if (s[a] == s[b]) ans = ans + cal(a+1,b-1);
  68.    
  69.     f[a][b] = ans;
  70.     return ans;
  71. }
  72.  
  73. void solve()
  74. {
  75.     cin >> s;
  76.     int n = s.size();
  77.     for (int i = 0 ; i <= n; i++)
  78.         for (int j = 0 ; j <= n; j++)
  79.             f[i][j] = bigNum(1,-1);
  80.     cout << cal(0,n-1) - bigNum(1,1);
  81. }
  82.  
  83. signed main()
  84. {
  85.     cin.tie(0)->sync_with_stdio(0);
  86.     freopen("in", "r", stdin);
  87.     freopen("out", "w", stdout);
  88.     int T = 1;
  89.     //cin >> T;
  90.     while (T--) {
  91.         solve();
  92.         cout << '\n';
  93.     }
  94.     cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
  95. }
Advertisement
Add Comment
Please, Sign In to add comment