题目大意:求词典序下第K个长度为N且无相邻位置都为1的0、1序列。无解时输出-1。

例如:

input:     output:

3 1        000(所有符合条件的序列有:000,001,010,100,101)

Time Limit:500MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

数据规模:0<N<44,0<K<10^9。

理论基础:无。

题目分析:先观察:1-5位的答案。

1:01

2:00 01 10

3:000 001 010100 101

4:00000001 00100100 01011000 10011010

5:0000000001 0001000100 0010101000 0100101010 10000 10001 10010 1010010101

观察数目:1:2,2:3,3:5,4:8,5:13...这是巧合吗?不是。我们看新序列的生成方式。在n-1位的所有序列的最高位添加0,数目为ans[i-1],在n-1位的所有最高位为0的序列前添加1,数目为ans[i-2](为什么呢?想想最高位为0的序列都是怎么来的???)这样,我们就得出:ans[i]=ans[i-1]+ans[i-2]。可是要的不是数目啊,是第K个序列啊。。。好吧,状态压缩dp可以解决,但是有没有更简单的方法呢?

我们再来观察:当N=5,K<=ans[4]时,我们输出的最高位肯定是0,这时只需要输出ans[4]时的K不就行了?同样此时如果K<=ans[3]递推下去。那如果K>ans[3]呢?那我们肯定输出的最高位是1,这时只需要输出ans[3]时的K-ans[3]不就行了?问题得到解决。。。

代码如下:

#include<iostream>
#include<cstring>
#include<string>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
#include<ctime>
#include<vector>
using namespace std;
typedef double db;
#define DBG 0
#define maa (1<<31)
#define mii ((1<<31)-1)
#define ast(b) if(DBG && !(b)) { printf("%d!!|\n", __LINE__); while(1) getchar(); } //调试
#define dout DBG && cout << __LINE__ << ">>| "
#define pr(x) #x"=" << (x) << " | "
#define mk(x) DBG && cout << __LINE__ << "**| "#x << endl
#define pra(arr, a, b) if(DBG) {\
dout<<#arr"[] |" <<endl; \
for(int i=a,i_b=b;i<=i_b;i++) cout<<"["<<i<<"]="<<arr[i]<<" |"<<((i-(a)+1)%8?" ":"\n"); \
if((b-a+1)%8) puts("");\
}
template<class T> inline bool updateMin(T& a, T b) { return a>b? a=b, true: false; }
template<class T> inline bool updateMax(T& a, T b) { return a<b? a=b, true: false; }
typedef long long LL;
typedef long unsigned int LU;
typedef long long unsigned int LLU;
#define N 43
int Fibonacci[N+1]={1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,
2584,4181,6765,10946,17711,28657,46368,75025,121393,196418,317811,514229,
832040,1346269,2178309,3524578,5702887,9227465,14930352,24157817,39088169,
63245986,102334155,165580141,267914296,433494437,701408733,1134903170};
int n,k;
int main()
{
while(~scanf("%d%d",&n,&k))
{
if(k>Fibonacci[n])
{
puts("-1");
continue;
}
while(n)
{
if(k<=Fibonacci[--n])printf("0");
else
{
printf("1");
k-=Fibonacci[n];
}
}
puts("");
}
return 0;
}

其中,Fibonacci数列即为ans。

by:Jsun_moon http://blog.csdn.net/Jsun_moon

URAL 1081的更多相关文章

  1. 递推DP URAL 1081 Binary Lexicographic Sequence

    题目传送门 题意:问第k个长度为n的01串是什么(不能有相邻的1) 分析:dp[i][0/1] 表示前i个,当前第i个放1或0的方案数,先预处理计算,dp[i][1]只能有dp[i-1][0]转移过来 ...

  2. URAL 1081 Binary Lexicographic Sequence

    第13个位置第5个Bit :13>num[4] =>1 第四个bit 13-num[4]=5 :5<num[3] =>0 ,3-1 第三个Bit 5>num[2](3) ...

  3. D - Binary Lexicographic Sequence URAL - 1081 (贪心)

    题目链接:https://cn.vjudge.net/contest/275079#problem/D 具体思路:首先,我们可以观察到1-n位数的种数连起来是一个很有规律的数列,然后我们开始倒序建立. ...

  4. URAL DP第一发

    列表: URAL 1225 Flags URAL 1009 K-based Numbers URAL 1119 Metro URAL 1146 Maximum Sum URAL 1203 Scient ...

  5. URAL 1146 Maximum Sum & HDU 1081 To The Max (DP)

    点我看题目 题意 : 给你一个n*n的矩阵,让你找一个子矩阵要求和最大. 思路 : 这个题都看了好多天了,一直不会做,今天娅楠美女给讲了,要转化成一维的,也就是说每一列存的是前几列的和,也就是说 0 ...

  6. URAL(DP集)

    这几天扫了一下URAL上面简单的DP 第一题 简单递推 1225. Flags #include <iostream> #include<cstdio> #include< ...

  7. 后缀数组 POJ 3974 Palindrome && URAL 1297 Palindrome

    题目链接 题意:求给定的字符串的最长回文子串 分析:做法是构造一个新的字符串是原字符串+反转后的原字符串(这样方便求两边回文的后缀的最长前缀),即newS = S + '$' + revS,枚举回文串 ...

  8. ural 2071. Juice Cocktails

    2071. Juice Cocktails Time limit: 1.0 secondMemory limit: 64 MB Once n Denchiks come to the bar and ...

  9. ural 2073. Log Files

    2073. Log Files Time limit: 1.0 secondMemory limit: 64 MB Nikolay has decided to become the best pro ...

随机推荐

  1. MyBatis 学习总结(二)

    1.MyBatis基础环境的搭建 1.1 核心配置文件mybatis-config.xml <?xml version="1.0" encoding="UTF-8& ...

  2. 码表 ASCII Unicode GBK UTF-8

    2017-1-3 [ASCII]一个字节(7位,128个字符,2个16进制) 不包含中文 ASCII(American Standard Code for Information Interchang ...

  3. gridview动态添加列的问题

    相信大家也和我一样遇到过这种问题,gridview在生成列的时候当列不确定怎么办?下面分享一下自己的解决方法. 举个列子说明一下. 普通列的添加比较简单. BoundField bf = new Bo ...

  4. 【转】mysql数据库中实现内连接、左连接、右连接

    [转]mysql数据库中实现内连接.左连接.右连接 内连接:把两个表中数据对应的数据查出来 外连接:以某个表为基础把对应数据查出来 首先创建数据库中的表,数据库代码如下: /* Navicat MyS ...

  5. Java实现文件上传

    最近自己在做一个小系统玩的时候涉及到了文件的上传,于是在网上找到Java上传文件的方案,最后确定使用common-fileupload实现上传操作. 需求说明 用户添加页面有一个“上传”按钮,点击按钮 ...

  6. 关于asp.net C# 导出Excel文件 打开Excel文件格式与扩展名指定格式不一致的解决办法

    -----转载:http://blog.csdn.net/sgear/article/details/7663502 关于asp.net C# 导出Excel文件 打开Excel文件格式与扩展名指定格 ...

  7. PHPexcel数据按模板导出

    <?php header("Content-type: text/html; charset=gb2312"); error_reporting(E_ALL); ini_se ...

  8. Windows上帝模式

    在桌面新建一个文件夹 按F2重命名为 GodMode.{ED7BA470-8E54-465E-825C-99712043E01C} 完成

  9. UIViewController的View显示在导航栏下面如何解决?

    ios7之前的版本中UIViewController中的view在显示后会自动调整为去掉导航栏的高度的,控件会自动在导航栏以下摆放. 在iOS7中UIViewController的wantsFullS ...

  10. Android入门随记

    1.Activity是通过startActivity()开始的,结束后不反回任何结果,而用startActivityForResult(Intent intent, int resquestCode) ...