UVa 10049 Self-describing Sequence:自描述序列&二分遞推

來源:互聯網
上載者:User

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 self­describing sequence is the only non­decreasing sequence of positive integers with the property that it contains exactly f(k) occurrences of k for each k. A few moments thought reveals that the sequence must begin as follows:

In this problem you 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 0 for n and this case must not be processed.

Output

For each test case in the input output the value of f(n) on a separate line.

Sample Input

100999912345610000000000

Sample Output

213561684438744

1. 如何構造一個增長速率較快的遞推式?

本文URL地址:http://www.bianceng.cn/Programming/sjjg/201410/45359.htm

注意到f(n)增長緩慢,不妨利用其反函數g(n)=max{m | f(m)=n}(比如g(4)=8),然後再求一次反函數f(n)=min{k | g[k]>=n}即可得到f(n)。

隨後由f(n)的定義有g(n)=g(n-1)+f(n)=g(n-1)+min{k | g[k]>=n}

(比如g(4)=g(3)+f(4)=5+3=8,g(5)=g(4)+f(5)=8+3=11)

2. 如何計算min{k | g[k]>=n}?

用二分尋找。

完整代碼:

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.