Advertisement
Mehulcoder

PhonePe2019A

Oct 9th, 2020 (edited)
758
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.37 KB | None | 0 0
  1. /*
  2. Author: Mehul Chaturvedi
  3. */
  4.  
  5. #include <bits/stdc++.h>
  6. using namespace std;
  7.  
  8. using ll = long long;
  9.  
  10. #define vll vector<long long>
  11. #define rep(i, n) for (int i = 0, _n = (n); i < _n; i++)
  12. const ll MOD = 1e9 + 7;
  13.  
  14.  
  15. // Assuming the max value of n<=1e5
  16. // If not, then use map in place of array dp
  17.  
  18. //It can further be optimized for n<=1e12
  19. //Note that for every level there is always a left and a right
  20. //All numbers within that range have to be added
  21. //So no need of dp[][]
  22.  
  23. /*
  24. * Can very easily be proved with simple inequalities that
  25. * a number k will be jth element of ith row (1 based index)
  26. * where i = ceil((sqrtl(1.0 + 8.0 * num) - 1.0) / 2.0)
  27. * and j = num - (i * i - i) / 2.
  28. * Rest is recursion.
  29. */
  30.  
  31. const ll N = 500;
  32. bool dp[N][N];
  33. ll get(ll i, ll j) {
  34.     if (j > i || j <= 0) return 0;
  35.     if (dp[i][j] == 1) return 0;
  36.     if (i == 1) {
  37.         dp[i][j] = 1;
  38.         return 1;
  39.     }
  40.  
  41.     ll k = (j + (i * i - i) / 2);
  42.     ll ans = k;
  43.     ans += get(i - 1, j - 1) + get(i - 1, j);
  44.  
  45.     dp[i][j] = 1;
  46.     return ans % MOD;
  47. }
  48.  
  49. void solve() {
  50.     ll num; cin >> num;
  51.     memset(dp, 0, sizeof(dp));
  52.     ll i = ceil((sqrtl(1.0 + 8.0 * num) - 1.0) / 2.0);
  53.     ll j = num - (i * i - i) / 2;
  54.  
  55.     cout << get(i, j) << '\n';
  56.  
  57.     return;
  58. }
  59.  
  60. int main(int argc , char ** argv) {
  61.     ios_base::sync_with_stdio(false) ;
  62.     cin.tie(NULL) ;
  63.  
  64.     ll t = 1;
  65.     while (t--) {
  66.         solve();
  67.     }
  68.  
  69.     return 0 ;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement