d751. Q10049-Selfdescribing Sequence@Morris' Blog|PChome Online 人新台
2011-09-16 08:40:34| 人1,333| 回0 | 上一篇 | 下一篇

d751. Q10049-Selfdescribing Sequence

0 收藏 0 0 站台

d751. Q10049-Selfdescribing Sequence

容 :

Solomon Golomb's selfdescribing sequence $langle f(1), f(2), f(3), dots rangle$ is the only nondecreasing 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:

 

 

begin{displaymath}begin{array}{cvert cccccccccccc} mbox{boldmath $n$} & 1 &... ...)$} & 1 & 2 & 2 & 3 & 3 & 4 & 4 & 4 & 5 & 5 & 5 & 6 end{array}end{displaymath}

 

In this problem you are expected to write a program that calculates the value of f(n) given the value of n.

入明 :

The input may contain multiple test cases. Each test case occupies a separate line and contains an integer n ( $1 le n le 2,000,000,000$). The input terminates with a test case containing a value 0 for n and this case must not be processed.

出明 :

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

例入 :

100 9999 123456 1000000000 0 

例出 :

21 356 1684 438744 

提示 :

出 :

UVa ACM 10049 (管理:david942j)



作法 : 建出一部份的表
Solomon Golomb的自我描述序列(selfdescribing sequence)<f(1), f(2), f(3), ......>是一唯一的不下降序列。在此序列的正整的特k在序列中出f(k)次。

/**********************************************************************************/
/*  Problem: d751 "Q10049-Selfdescribing Sequence" from UVa ACM 10049           */
/*  Language: C (717 Bytes)                                                   nbsp;   */
/*  Result: AC(52ms, 280KB) judge by this@ZeroJudge                               */
/*  Author: morris1028 at 2011-09-16 08:36:37                                     */
/**********************************************************************************/


#include<stdio.h>
struct {
    int n, fn;
}A[4811];
int BinarySearch(int l, int r, int v) {
    int m;
    do {
        m = (l+r)/2;
        if(A[m].n < v)    l = m+1;
        else {
            if(A[m].n >= v && A[m-1].n < v)
                return m;
            r = m-1;
        }
    } while(l <= r);
}
int fn(int n, int limit) {
    int i, fn;
    if(n == 1)    return 1;
    i = BinarySearch(1, limit, n);
    i--;
    fn = (n - A[i].n)/i + A[i].fn;
    return fn;
}
main() {
    A[1].n = 1, A[1].fn = 1;
    A[2].n = 2, A[2].fn = 2;
    A[3].n = 6, A[3].fn = 4;
    int i;
    for(i = 4; i < 4811; i++) {
        A[i].n = A[i-1].n + fn(i-1, i-1)*(i-1);
        A[i].fn = A[i-1].fn + (A[i].n - A[i-1].n)/(i-1);
    }
    while(scanf("%d", &i) == 1 && i)
        printf("%d\n", fn(i, 4810));
    return 0;
}

台: Morris
人(1,333) | 回(0)| 推 (0)| 收藏 (0)|
全站分: 不分 | 人分: UVA |
此分下一篇:d223. Q10137 The Trip
此分上一篇:d664. 11725 - Colorful Board

是 (若未登入"人新台"看不到回覆唷!)
* 入:
入片中算式的果(可能0) 
(有*必填)
TOP
全文
ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86