Codeforces 8E

E. Beads
time limit per test
5 seconds
memory limit per test
64 megabytes
input
standard input
output
standard output

One Martian boy called Zorg wants to present a string of beads to his friend from the Earth — Masha. He knows that Masha likes two colours: blue and red, — and right in the shop where he has come, there is a variety of adornments with beads of these two colours. All the strings of beads have a small fastener, and if one unfastens it, one might notice that all the strings of beads in the shop are of the same length. Because of the peculiarities of the Martian eyesight, if Zorg sees one blue-and-red string of beads first, and then the other with red beads instead of blue ones, and blue — instead of red, he regards these two strings of beads as identical. In other words, Zorg regards as identical not only those strings of beads that can be derived from each other by the string turnover, but as well those that can be derived from each other by a mutual replacement of colours and/or by the string turnover.

It is known that all Martians are very orderly, and if a Martian sees some amount of objects, he tries to put them in good order. Zorg thinks that a red bead is smaller than a blue one. Let's put 0 for a red bead, and 1 — for a blue one. From two strings the Martian puts earlier the string with a red bead in the i-th position, providing that the second string has a blue bead in the i-th position, and the first two beads i - 1are identical.

At first Zorg unfastens all the strings of beads, and puts them into small heaps so, that in each heap strings are identical, in his opinion. Then he sorts out the heaps and chooses the minimum string in each heap, in his opinion. He gives the unnecassary strings back to the shop assistant and says he doesn't need them any more. Then Zorg sorts out the remaining strings of beads and buys the string with index k.

All these manupulations will take Zorg a lot of time, that's why he asks you to help and find the string of beads for Masha.

Input

The input file contains two integers n and k (2 ≤ n ≤ 50;1 ≤ k ≤ 1016) —the length of a string of beads, and the index of the string, chosen by Zorg.

Output

Output the k-th string of beads, putting 0 for a red bead, and 1 — for a blue one. If it s impossible to find the required string, output the only number -1.

Examples
input
4 4
output
0101
Note

Let's consider the example of strings of length 4 — 0001, 0010, 0011, 0100, 0101, 0110, 0111, 1000, 1001, 1010, 1011, 1100, 1101, 1110. Zorg will divide them into heaps: {0001, 0111, 1000, 1110}, {0010, 0100, 1011, 1101}, {0011, 1100}, {0101, 1010}, {0110, 1001}. Then he will choose the minimum strings of beads in each heap: 0001, 0010, 0011, 0101, 0110. The forth string — 0101.

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, a[52];  
ll k;  
bool vis[55][55][2][2];  
ll dp[55][55][2][2]; 
ll ans; ll cnt = 0;
ll dfs(int l,int r,int rev,int inv)
{
    if(l>r)return 1;
    if(vis[l][r][rev][inv]==true) return dp[l][r][rev][inv];
    vis[l][r][rev][inv] =true;//不用vis会超时 中间会遇到很多相同的情况 ex: 49 40394027154620 ->repeat = 2.4k
    ll &ans=dp[l][r][rev][inv];
    ans = 0;
    for(int i=0;i<2;i++)
    {  
        if(a[l] == -1 || a[l] == i)
        {  
            for(int j = 0; j < 2; j++)
            {  
                if(a[r] == -1 || a[r] == j)
                {  
                    if(l < r || i == j) //特判n为奇数的时候,走到最中间的一位  
                    { 
                        if(rev || i <= j) //rev = 0 1..l n-1..r identical
                        {  
                            if(inv || i <= 1 - j) //inv = 0 1..l ~(n-1..r) identical
                            {  
                                ans = ans + dfs(l + 1, r - 1, rev || i < j , inv || i < 1 - j);  
                            }  
                        }  
                    }  
                }  
            }  
        }  
    }
    return ans; 
}
int main()
{
    memset(a,-1,sizeof(a));
    cin>>n>>k;
    k++; //0000...0000不允许 而0-1-1-1...的情况肯定会算进0000...0000
    a[0]=0;
    if(dfs(0,n-1,0,0)<k){
        return 0*puts("-1");
    }
    for(int i=1;i<n;i++)
    {
 
        a[i]=0;       
       /* puts("before");
        for (int j= 0; j<n; j++){
            cout<<a[j];
        }
        puts("");*/
        memset(vis,0,sizeof(vis));
        ll cur=dfs(0,n-1,0,0);
        if(cur<k)
        {
            k -= cur;
            a[i]=1;
        }
        /*puts("after");
        for (int j= 0; j<n; j++){
            cout<<a[j];
        }
        puts("");
        cout<<cur<<endl;*/
    }
    return 0;
}
补充题解集: 从左到右一位一位确定是0还是1 然后先假设该位是0 dp一下 发现不够的话 换成1 以为如果改为是1 肯定囊括了前面0的情况 然后再计算后面的 所以要k -= cur; 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值