TOJ 3867: Cow IDs -- 递归

3867: Cow IDs

Time Limit(Common/Java):1000MS/3000MS     Memory Limit:65536KByte
Total Submit: 41            Accepted:13

Description

Being a secret computer geek, Farmer John labels all of his cows with binary numbers. However, he is a bit superstitious, and only labels cows with binary numbers that have exactly K "1" bits (1 <= K <= 10). The leading bit of each label is always a "1" bit, of course. FJ assigns labels in increasing numeric order, starting from the smallest possible valid label -- a K-bit number consisting of all "1" bits. Unfortunately, he loses track of his labeling and needs your help: please determine the Nth label he should assign (1 <= N <= 10^7).

Input

* Line 1: Two space-separated integers, N and K.

Output

*Line 1: One number in binary form.

Sample Input

7 3

Sample Output

10110

Hint

INPUT DETAILS:

Among all binary numbers containing exactly 3 "1" bits, FJ wants to output the 7th in increasing sorted order.

Source

USACO Feb. 2012


分析:用s[i][j]表示用i + 1个1和不超过j个0最多产生多少个数,那么lower_bound(s[k - 1], s[k - 1] + len, n)就是第n个数含有的0的个数,再算出剩下部分(从第2个1开始的部分)有多少个0,它们的差就是前两个1之间0的个数,然后递归剩下以1开头的部分。

#include 
     
     
      
      
#include 
      
      
       
       
#include 
       
       
        
        
#include 
        
        
         
         
#include 
         
         
           #include 
           #include 
           
             #include 
            
              #include 
             
               #include 
              
                #include 
               
                 #include 
                
                  #include 
                 
                   #include 
                  
                    #include 
                   
                     #define mp make_pair using namespace std; typedef unsigned int ui; typedef long long ll; typedef unsigned long long ull; typedef pair 
                    
                      pii; typedef vector 
                     
                       vi; typedef vi::iterator vi_it; typedef map 
                      
                        mii; typedef priority_queue 
                       
                         pqi; typedef priority_queue 
                        
                          , greater 
                         
                           > rpqi; const int MAX_K = 10; const int MAX_N = 10000; const int MAX = 10000000; int s[MAX_K][MAX_N]; //s[i][j]: (i + 1) 1s and (<= j) 0s int cnt[MAX_K]; void ini(int n) { int i, j; for (i = 0; i < MAX_K; ++i) { s[i][0] = 1; } for (i = 0; i < MAX_N; ++i) { s[0][i] = i + 1; } for (i = 1; i < MAX_K; ++i) { for (j = 1; j < MAX_N; ++j) { s[i][j] = s[i][j - 1] + s[i - 1][j]; if (s[i][j] >= n) { cnt[i] = j + 1; break; } } } } void solve(int n, int k) { cout << 1; if (k == 1) { for (int i = 1; i < n; ++i) { cout << 0; } return; } else if (n == 1) { for (int i = 1; i < n; ++i) { cout << 1; } return; } int t1 = lower_bound(s[k - 1], s[k - 1] + cnt[k - 1], n) - s[k - 1]; n -= s[k - 1][t1 - 1]; int t2 = lower_bound(s[k - 2], s[k - 2] + cnt[k - 1], n) - s[k - 2]; for (int i = 0; i < t1 - t2; ++i) { cout << 0; } solve(n, k - 1); } int main(int argc, char *argv[]) { int n, k; cin >> n >> k; ini(n); solve(n, k); cout << endl; return 0; } 
                          
                         
                        
                       
                      
                     
                    
                   
                  
                 
                
               
              
             
            
         
        
        
       
       
      
      
     
     

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值