Got a question on 1862D (from #894)

Revision en1, by clocosacide, 2023-08-24 20:05:51

In the contest that has just ended, my code for problem D failed on testcase 6. This is my code:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
double n;
ll solve(){
   double ans = (sqrt(1 + ll(8.0 * n)) - 1.0) / 2;
   double ret = (ll)ans + ((ll)n - (ll)(ans + 1) * (ll)ans / 2);
   return ll(ret + 1); 
}
int main(){
    int t;
    cin >> t;
    while(t--){
        cin >> n;
        cout << solve() << endl;
    }
}

It works perfectly fine with case 1 to 5. The main idea of this program is to use the formula for quadratic functions to get the minimum value of $$$x$$$ such that $$$x * (x + 1) / 2$$$ is not larger than $$$n$$$.

Thx a lot to anyone who's willing to help me!!!

Tags help me

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en2 English clocosacide 2023-08-24 20:07:00 0 (published)
en1 English clocosacide 2023-08-24 20:05:51 767 Initial revision (saved to drafts)