UVa 10049 self-describing Sequence: self-describing sequence & binary recursion

Source: Internet
Author: User
Tags min time limit

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;  
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.