$O(1)$
답은 1+ 2+ ... + n <= s 를 만족하는 최대 n이다.
n(n+1)/2<=s를 풀면 $n=[(\sqrt{8s+1}−1)/2]$
#include<cstdio> #include<math.h> long long s; int main() { scanf("%lld", &s); printf("%d", ((int)sqrt(8 * s + 1) - 1) / 2); return 0; }
#include<cstdio> #include<math.h> long long s; int main() { scanf("%lld", &s); printf("%d", ((int)sqrt(8 * s + 1) - 1) / 2); return 0; }
댓글 없음 :
댓글 쓰기