10049-self-describing Sequence
Time limit:3.000 seconds
Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=34&page=show_problem &problem=990
Solomon Golomb ' s selfdescribing sequence is the only nondecreasing sequence of positive It contains exactly f (k) occurrences of K for each k. A few moments thought reveals that sequence must begin as follows:
In this problem your are expected to write a program that calculates the value of f (n) Given the value of N.
Input
The input may contain multiple test cases. Each test case occupies a separate line and contains an integer n (). The input terminates with a test case containing a value of 0 for N and the must is processed.
Output
For each test case in the input output the value of f (n) on a separate line.
Sample Input
9999
123456
1000000000
0
Sample Output
356
1684
438744
1. How do I construct a recursive type with a faster growth rate?
This article URL address: http://www.bianceng.cn/Programming/sjjg/201410/45359.htm
Note that f (n) grows slowly, and you can get f (n) by using its inverse function g (n) =max{m | f (M) =n} (such as G (4) =8), and then the inverse function f (n) =min{k | g[k]>=n} is obtained.
Then the definition of f (n) has g (n) =g (n-1) +f (n) =g (n-1) +min{k | g[k]>=n}
(e.g. G (4) =g (3) +f (4) =5+3=8,g (5) =g (4) +f (5) =8+3=11)
2. How to calculate Min{k | g[k]>=n}?
Use a binary lookup.
Complete code:
/*0.075s*/
#include <cstdio>
#include <algorithm>
const int M = 700000;
using namespace std;
Long long g[m];
int main ()
{
g[1] = 1;
G[2] = 3;
for (int i = 3; i < M ++i)
g[i] = g[i-1] + (Lower_bound (g + 1, g + I, i)-g);
int n;
while (scanf ("%d", &n), N)
printf ("%d\n", Lower_bound (g + 1, G + M, N)-G);
return 0;
}