CodeForces 622A Infinite Sequence
简单题,公式打了个表,查询的时候二分一下就行。也可以直接o(1)公式出解。
#include <stdio.h> #include <algorithm> #include <string.h> #include <queue> #include <stack> #include <map> #include <vector> using namespace std; long long a[20000005]; int main() { for (int i = 1; i <= 20000000; i++) { long long now = (long long)i; a[i] = (1 + now)*now / 2; } long long n; while (~scanf("%lld", &n)){ int l = 1, r = 20000000; int pos; while (l <= r) { int mid = (l + r) / 2; if (a[mid] <= n) { pos = mid; l = mid + 1; } else r = mid - 1; } if (a[pos] == n) printf("%d\n", pos); else printf("%lld\n", n - a[pos]); } return 0; }