Advertisement
Guest User

Pascal Walk AC solution

a guest
Apr 11th, 2020
363
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.65 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define int long long
  3. #define uint unsigned long long
  4. #define double long double
  5. #pragma GCC optimize("O3,Ofast,no-stack-protector,fast-math,unroll-loops")
  6. using namespace std;
  7.  
  8. int pasc[501][501];
  9.  
  10. void init() {
  11.     pasc[1][1] = 1;
  12.     for (int i = 2; i <= 500; i++) {
  13.         pasc[i][1] = 1;
  14.         pasc[i][i] = 1;
  15.         for (int j = 2; j < i; j++) {
  16.             pasc[i][j] = pasc[i - 1][j - 1] + pasc[i - 1][j];
  17.             pasc[i][j] %= 1000000000000;
  18.         }
  19.     }
  20. }
  21.  
  22. bool solve(int i, int n, int z, bool show) {
  23.     int x = 0;
  24.     int q = 0;
  25.     int r = 1;
  26.     int k = 1;
  27.     if (show) {
  28.         cout << "Case #" << i << ":\n";
  29.     }
  30.     while (x < n) {
  31.         q++;
  32.         x += pasc[r][k];
  33.         if (show) {
  34.             cout << r << ' ' << k << '\n';
  35.         }
  36.         if (r < z) {
  37.             r++;
  38.         } else {
  39.             bool cond;
  40.             if (r == k) {
  41.                 cond = (x + pasc[r + 1][k + 1] <= n);
  42.             } else {
  43.                 cond = (x + pasc[r + 1][k + 1] <= n);
  44.             }
  45.             if (cond) {
  46.                 r++;
  47.                 k++;
  48.             } else {
  49.                 k++;
  50.             }
  51.         }
  52.     }
  53.     return q <= 499 && x == n;
  54. }
  55.  
  56. signed main() {
  57.     init();
  58.     ios_base::sync_with_stdio(false);
  59.     cin.tie(0);
  60.     cout.tie(0);
  61.  
  62.     int t;
  63.     cin >> t;
  64.     for (int i = 1; i <= t; i++) {
  65.         int n;
  66.         cin >> n;
  67.         for (int z = 16; z >= 1; z--) {
  68.             if (solve(i, n, z, 0)) {
  69.                 solve(i, n, z, 1);
  70.                 break;
  71.             }
  72.         }
  73.     }
  74.  
  75.     fflush(stdout);
  76.     return 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement