Let g(n) denote the n'th number that satifies f(k) > f(k-1). We can know that
g(0) = 1, g(1) = 2, g(2) = 4, ...
From the definition of g(n), we can know that all the values of f(k) where k belongs to the range of [g(n-1), g(n)) is n. Since there are exactly f(k) occurrences of k in the sequence, there must be f(1) + f(2) + ... + f(f(k)-1) elements before f(k) if f(k) > f(k-1). Thus, we can know that
g(n) = f(1) + f(2) + ... + f(f(k)-1) + 1.
In fact f(k)-1 == n, so we have
g(n) = f(1) + f(2) + ... + f(n) + 1.
Also, we can draw a simple recurrence of
g(n) = g(n-1) + f(n).
We have already known that the values of f(k) in the range of [g(n-1), g(n)) is n. So we can compute g(n) in this way:
1. Let g(1) = 2, g(2) = 4, i = 1
2. for n in [g(i), g(i+1)), we know f(n) = i+1, so we can compute g(n) with
g(n) = g(n-1) + (i+1)
3. i = i+1, repeat 2 util g(n) is big enough.
Code:
- /***************************************************************************
- * Copyright (C) 2008 by Liu Kaipeng *
- * LiuKaipeng at gmail dot com *
- ***************************************************************************/
- /* @JUDGE_ID 00000 10049 C++ "Self-describing Sequence" */
- #include <algorithm>
- #include <cmath>
- #include <cstdio>
- #include <cstring>
- #include <deque>
- #include <fstream>
- #include <iostream>
- #include <list>
- #include <map>
- #include <queue>
- #include <set>
- #include <stack>
- #include <string>
- #include <vector>
- using namespace std;
- /*
- * Let g(n) denote the n'th number that satifies f(k) > f(k-1). We can know that
- * g(0) = 1, g(1) = 2, g(2) = 4, ... From the definition of g(n), we can know
- * that all the values of f(k) where k belongs to the range of [g(n-1), g(n)) is n.
- * Since there are exactly f(k) occurrences of k in the sequence, there must be
- * f(1) + f(2) + ... + f(f(k)-1) elements before f(k) if f(k) > f(k-1). Thus, we can
- * know that g(n) = f(1) + f(2) + ... + f(f(k)-1) + 1. In fact f(k)-1 == n, so we
- * have g(n) = f(1) + f(2) + ... + f(n) + 1. Also, we can draw a simple recurrence of
- * g(n) = g(n-1) + f(n).
- * We have already known that the values of f(k) in the range of [g(n-1), g(n)) is n.
- * So we can compute g(n) in this way:
- * 1. Let g(1) = 2, g(2) = 4, i = 1
- * 2. for n in [g(i), g(i+1)), we know f(n) = i+1, so we can compute g(n) with
- * g(n) = g(n-1) + (i+1)
- * 3. i = i+1, repeat 2 util g(n) is big enough.
- */
- int numbers[700000];
- int size = 0;
- void gen_numbers()
- {
- int const max_number = 2000000000;
- numbers[0] = 1;
- numbers[1] = 2;
- numbers[2] = 4;
- int i = 1;
- for (; numbers[numbers[i]-1] < max_number; ++i)
- for (int j = numbers[i]; j < numbers[i+1]; ++j)
- numbers[j] = numbers[j-1] + i + 1;
- size = numbers[i] - 1;
- }
- int main(int argc, char *argv[])
- {
- #ifndef ONLINE_JUDGE
- freopen((string(argv[0]) + ".in").c_str(), "r", stdin);
- freopen((string(argv[0]) + ".out").c_str(), "w", stdout);
- #endif
- gen_numbers();
- for (int n; cin >> n && n != 0; )
- cout << upper_bound(numbers, numbers + size, n) - numbers << '/n';
- return 0;
- }