[UVA][排列合] 10232 - Bit-wise Sequence@Morris' Blog|PChome Online 人新台
2014-02-17 09:06:52| 人1,739| 回0 | 上一篇 | 下一篇

[UVA][排列合] 10232 - Bit-wise Sequence

0 收藏 0 0 站台

 

Problem D: Bit-wise Sequence

Dr. DoLots is an expert in number theory and he is currently studying prime numbers. In fact, some people say that he has some formula which can generate the prime numbers in increasing order. But, his formula needs to order the non-negative integers according to the increasing number of ones in their binary representation. That is, all non-negative integers with m ones in their binary representation should be ordered before all the non-negative integers with m+1 ones.

More formally, lets define the non-negative integer sequence Bn such that for every non-negative integers m and n where n<m
            1. either O(Bn)< O(Bm)
            2. or O(Bn) = O(Bm) and Bn< Bm.
where O(n) is the number of ones in the binary representation of n.

Dr. DoLots wants you to write a program which computes the value of Bn for arbitrary values of n, 0 n 2147483647. For simplicity, we consider only non-negative integers less than 2147483648 to appear in the sequence. You might be wondering why the doctor cannot do it himself. He is very busy and he wants to spent his time on more important things (That is what he says, actually, he is a bit lazy, but you can help him, can't you ?).

Input

Input consists of several lines specifying one non-negative integer value for n.

Output

Output should follow the same format as the input except that n is replaced by Bn.

Sample Input

0 1 2 31 32 

Sample Output

0 1 2 1073741824 3 

Arun Kishore



目描述:


31 bits 的所有字,按照 bits 大小再按照字大小排序,得到它的大小。

在逆向反求,定第,出字何。

#include <stdio.h>
long long C[50][50] = {};
void init() {
    C[0][0] = 1;
    int i, j, k;
    for(i = 1; i < 50; i++) {
        C[i][0] = 1;
        for(j = 1; j <= i; j++)
            C[i][j] = C[i-1][j] + C[i-1][j-1];
    }
}
int main() {
    init();
    long long n;
    while(canf("%lld", &n) == 1) {
        long long ret = 0;
        int i, j, k;
        int ones;
        for(i = 0; i <= 31; i++) {
            if(n < C[31][i]) {
                Ones= i;
                break;
            }
            n -= C[31][i];
        }
        for(i = 31; i >= 0; i--) {
            if(C[i][ones] > n)
                ret = ret << 1;
            else
                ret = ret << 1 | 1, n -= C[i][ones], ones--;
        }
        printf("%lld\n", ret);
    }
    return 0;
}

台: Morris
人(1,739) | 回(0)| 推 (0)| 收藏 (0)|
全站分: 教育(修、留、研究、教育概) | 人分: UVA |
此分下一篇:[UVA][] 10273 - Eat or Not to Eat
此分上一篇:[UVA][概率] 10217 - A Dinner with Schwarzenegger

是 (若未登入"人新台"看不到回覆唷!)
* 入:
入片中算式的果(可能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