【數論】[HNOI2006]鬼谷子的錢袋

題目

倍增以及二進制的思想
比如我們要算的是10 如果要組成的一個數能由一部分加上另一部分組成就ok了 6~10可以由1~5加5組成 所以要選5 接下來就把5除2然後再用小的一部分組成大的一部分 一直除2到不能再除
因爲要從小到大輸出 但第一個確定的一定是最大的 所以可以用棧存儲

代碼如下



#include<iostream>
#include<cstdio>
#include<cctype>

    using namespace std;
    #define in = read();
    typedef long long ll;
    typedef unsigned int ui;
    const ll size = 100 + 1;

        int n , top;
        int stack[size];

inline ll read(){
        ll num = 0 , f = 1;    char ch = getchar();

        while(!isdigit(ch)){
                if(ch == '-')   f = -1;
                ch = getchar();
        }
        while(isdigit(ch)){
                num = num*10 + ch - '0';
                ch = getchar();
        }

        return num*f;
}

int main(){
        n in;

        while(n){
                stack[++ top] = ((n + 1)>>1);
                n >>= 1;
        }

        printf("%d\n" , top);
        while(top){
                printf("%d " , stack[top]);
                top --;
        }
}


//COYG
發佈了102 篇原創文章 · 獲贊 30 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章