【USACO 3.2.2】位元01串

來源:互聯網
上載者:User

標籤:style   class   blog   code   color   檔案   

【描述】

考慮排好序的N(N<=31)位位元。

你會發現,這很有趣。因為他們是排列好的,而且包含所有可能的長度為N且含有1的個數小於等於L(L<=N)的數。

你的任務是輸出第I(1<=I<=長度為N的位元的個數)大的,長度為N,且含有1的個數小於等於L的那個位元。

注意:這裡“長度為N”包括長度小於N的數(我們認為高位用0補齊)

 【格式】PROGRAM NAME: kimbitsINPUT FORMAT:(file kimbits.in)

共一行,用空格分開的三個整數N,L,I。

OUTPUT FORMAT:(file kimbits.out)

共一行,輸出滿足條件的第I大的位元。

【分析】

簡單的組合數學的題目,用二分法,對每一位判斷當改位是0的時候的有多少個合格數就行了。

注意加個記憶化,注意開longlong

 1 #include <cstdlib> 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <cmath> 6 const int maxn=32+5; 7 using namespace std; 8 long long c[maxn][maxn],n,o=0; 9 long long C(long long  a,long long b) 10 {11     if (a==0) return 1;12     if(c[a][b]!=-1) return c[a][b];13     return a==1?b:c[a][b] = ((C(a-1,b)*(b-a+1)))/a;14 }15 long long total(long long num,long long len,long long Maxo);16 int main()17 {18     long long l,i,len;//cnt是已有1的個數 19     //檔案操作20     freopen("kimbits.in","r",stdin);21     freopen("kimbits.out","w",stdout); 22     char ans[maxn];23     memset(c,-1,sizeof(c));24     memset(ans,0,sizeof(ans));25     scanf("%lld%lld%lld",&n,&l,&i);26     for (len=0;len<n;len++)//枚舉長度 27     {28         long long temp=total(0,len,l);29         30         if (temp>=i) ans[len]=0+‘0‘;31         else {ans[len]=1+‘0‘;i=i-temp;o++;}32     }33     printf("%s",ans);34     return 0;35 }36 //剩下len位 37 long long total(long long num,long long len,long long Maxo)38 {39     long long cnt=0;40     len=n-len-1;41     for (long long i=0;(i+o)<=Maxo;i++)//剩餘位中1的個數 42     cnt+=C(i,len);43     return cnt;44 }

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.