livingonhope's blog

By livingonhope, history, 12 months ago, In English

Heres the code and problem link as well plzz help me undertand why is it failing .it will be very helpfull Problem Link — https://codeforces.me/contest/1829/problem/G Code ( C++) :

include <bits/stdc++.h>

using namespace std;

define ll long long

set ans;

int compute_row(int x) { int l = 1, r = 50000; while (l < r) { int m = (l + r) / 2; if (m * (m + 1) / 2 >= x) r = m; else l = m + 1; } return l; }

vector dp;

void solve(int n) { if (n <= 0 || dp[n]) return; dp[n] = 1;

ans.insert(n);

int row = compute_row(n);
int val1 = n - row;
int val2 = n - row + 1;

if (val1 >= 1 && compute_row(val1) < row)
    solve(val1);

if (val2 >= 1 && compute_row(val2) < row)
    solve(val2);

}

int main() { int t; cin >> t; while (t--) { int n; cin >> n; ans.clear(); dp.assign(n + 10, 0);

solve(n);

    ll sum = 0;
    for (int x : ans) sum += 1LL * x * x;

    cout << sum << endl;
}

}

  • Vote: I like it
  • -4
  • Vote: I do not like it