Ural 1081. Binary Lexicographic Sequence


1081. Binary Lexicographic Sequence

Time limit: 0.5 second
Memory limit: 64 MB
Consider all the sequences with length (0 < N < 44), containing only the elements 0 and 1, and no two ones are adjacent (110 is not a valid sequence of length 3, 0101 is a valid sequence of length 4). Write a program which finds the sequence, which is on K-th place (0 < K < 109) in the lexicographically sorted in ascending order collection of the described sequences.

Input

The first line of input contains two positive integers N and K.

Output

Write the found sequence or −1 if the number K is larger then the number of valid sequences.

Sample

input output
3 1
000
Problem Author: Emil Kelevedzhiev
Problem Source: Winter Mathematical Festival Varna '2001 Informatics Tournament
Difficulty: 256    Printable version    Submit solution    Discussion (24)

求字典序下,第k个长度为n的合法字符串(合法字符串满足:只有0和1,且不能有两个1相邻)

打表会发现,长度为n的合法序列一共有Fibonacci[n]个。其中有Fibonacci[n - 1]个是由0开头的(也就是长度为n-1前面的补0),剩下的是以1开头 的。


#include 
   
   
    
    
#include 
    
    
#include 
     
     
      
      
#include 
      
      
       
       
#include 
       
       
         #include 
        
          #include 
         
           #include 
          
            #include 
           
             #include 
            
              #include 
             
               #include 
              
                #include 
               
                 #include 
                
                  #include 
                 
                   #include 
                  
                    #include 
                   
                     #include 
                    
                      #define pi acos(-1.0) #define maxn (101000 + 50) #define mol 1000000009 #define inf 0x3f3f3f3f #define Lowbit(x) (x & (-x)) using namespace std; typedef long long int LLI; LLI fib[maxn]; int main() { // freopen("in.txt","r",stdin); // freopen("out.txt","w",stdout); fib[0] = 1; fib[1] = 2; for(int i = 2; i <= 44; i ++) fib[i] = fib[i - 1] + fib[i - 2]; LLI n,k; scanf("%lld%lld",&n,&k); if(k > fib[n]) printf("-1\n"); else { for(int i = n; i >= 1; i --) { if(k > fib[i - 1]) { printf("1"); k -= fib[i - 1]; } else printf("0"); } printf("\n"); } return 0; } //000 //001 //010 //100 //101 
                     
                    
                   
                  
                 
                
               
              
             
            
           
          
         
       
      
      
     
     
   
   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值