UVa Problem Solution: 10049 - Self-describing Sequence


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:
  1. /***************************************************************************
  2.  *   Copyright (C) 2008 by Liu Kaipeng                                     *
  3.  *   LiuKaipeng at gmail dot com                                           *
  4.  ***************************************************************************/
  5. /* @JUDGE_ID 00000 10049 C++ "Self-describing Sequence" */
  6. #include <algorithm>
  7. #include <cmath>
  8. #include <cstdio>
  9. #include <cstring>
  10. #include <deque>
  11. #include <fstream>
  12. #include <iostream>
  13. #include <list>
  14. #include <map>
  15. #include <queue>
  16. #include <set>
  17. #include <stack>
  18. #include <string>
  19. #include <vector>
  20. using namespace std;
  21. /*
  22.  * Let g(n) denote the n'th number that satifies f(k) > f(k-1). We can know that
  23.  * g(0) = 1, g(1) = 2, g(2) = 4, ... From the definition of g(n), we can know 
  24.  * that all the values of f(k) where k belongs to the range of [g(n-1), g(n)) is n.
  25.  * Since there are exactly f(k) occurrences of k in the sequence, there must be 
  26.  * f(1) + f(2) + ... + f(f(k)-1) elements before f(k) if f(k) > f(k-1). Thus, we can
  27.  * know that g(n) = f(1) + f(2) + ... + f(f(k)-1) + 1. In fact f(k)-1 == n, so we
  28.  * have g(n) = f(1) + f(2) + ... + f(n) + 1. Also, we can draw a simple recurrence of
  29.  *   g(n) = g(n-1) + f(n).
  30.  * We have already known that the values of f(k) in the range of [g(n-1), g(n)) is n.
  31.  * So we can compute g(n) in this way:
  32.  * 1. Let g(1) = 2, g(2) = 4, i = 1
  33.  * 2. for n in [g(i), g(i+1)), we know f(n) = i+1, so we can compute g(n) with 
  34.  *      g(n) = g(n-1) + (i+1)
  35.  * 3. i = i+1, repeat 2 util g(n) is big enough.
  36.  */
  37. int numbers[700000];
  38. int size = 0;
  39. void gen_numbers()
  40. {
  41.   int const max_number = 2000000000;
  42.   numbers[0] = 1;
  43.   numbers[1] = 2;
  44.   numbers[2] = 4;
  45.   int i = 1;
  46.   for (; numbers[numbers[i]-1] < max_number; ++i)
  47.     for (int j = numbers[i]; j < numbers[i+1]; ++j)
  48.       numbers[j] = numbers[j-1] + i + 1; 
  49.   size = numbers[i] - 1;
  50. }
  51. int main(int argc, char *argv[])
  52. {
  53. #ifndef ONLINE_JUDGE
  54.   freopen((string(argv[0]) + ".in").c_str(), "r", stdin);
  55.   freopen((string(argv[0]) + ".out").c_str(), "w", stdout);
  56. #endif
  57.   gen_numbers();
  58.   for (int n; cin >> n && n != 0; )
  59.     cout << upper_bound(numbers, numbers + size, n) - numbers << '/n';
  60.   return 0;
  61. }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值