#include<iostream>
#include<bits/stdc++.h>
using namespace std;
long long getseg(long long n)
{
long long ans=0;
long long l=1,r=n;//r
while(r-l>1)
{
long long mid=(l+r)>>1;
long long st,en=mid*(mid+1);
en>>=1;
st=en-mid+1;
if(n<st)r=mid;
else if(n>st)l=mid;
else {ans=mid;break;}
}
if(ans>0)return ans;
long long bl,br=l*(l+1);
br>>=1;
bl=br-l+1;
if(bl<=n && n<=br)return l;
else return r;
}
int main()
{
long long n;
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>n;
long long seg=getseg(n);
long long fin=seg*seg,en=seg*(seg+1);
en>>=1;
long long diff=en-n,ans=0;
diff<<=1;
ans=fin-diff;
cout<<ans<<"\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |