Binary Lexicographic Sequence URAL - 1081 (有关数的排列)

 

 1 #include<iostream>
 2 #include<vector>
 3 #include<algorithm>
 4 #include<map>
 5 #include<set>
 6 #include<string>
 7 using namespace std;
 8 typedef unsigned long long LL;
 9 const int maxn = 46;
10 LL dp[maxn][2];
11
12 void init()
13 {
14     dp[1][0] = dp[1][1] = 1;
15     for (int i = 2; i < maxn; i++) {
16         dp[i][0] = dp[i - 1][0] + dp[i - 1][1];
17         dp[i][1] = dp[i - 1][0];
18     }
19 }
20
21 void solve(int n, int k)
22 {
23     if (k > dp[n][0] + dp[n][1]) { //若k大于所有可能排列数
24         cout << "-1" << endl;
25         return ;
26     }
27     vector<char> ans;
28     for (int i = n; i >= 1; i--) { //
29         if (dp[i][0] >= k) cout << "0";
30         else {
31             k -= dp[i][0];
32             cout << "1";
33         }
34     }
35     cout << endl;
36 }
37
38 int main()
39 {
40     init();
41     int n, k;
42     while (cin >> n >> k)
43         solve(n, k);
44 }

原文地址:https://www.cnblogs.com/tcctw/p/8645364.html

时间: 2024-08-29 10:45:13

Binary Lexicographic Sequence URAL - 1081 (有关数的排列)的相关文章

递推DP URAL 1081 Binary Lexicographic Sequence

题目传送门 1 /* 2 dp[i][1]/dp[i][0] 表示从左往右前i个,当前第i个放1或0的方案数 3 k -= dp[n][0] 表示当前放0的方案数不够了,所以必须放1,那么dp[n][0]个方案数都不能用了 4 相当于k减去这么多 5 详细解释:http://www.cnblogs.com/scau20110726/archive/2013/02/05/2892587.html 6 */ 7 #include <cstdio> 8 #include <algorithm&

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) 5-num[2]=2 ... #include<stdio.h> int num[45]; void init() { num[0]=1; num[1]=2; int k=2; while(k<44) { num[k]=num[k-1]+num[k-2]; k++; } } int main

URAL1081 Binary Lexicographic Sequence(递归)

URAL 1081. Binary Lexicographic Sequence Time limit: 0.5 second Memory limit: 64 MB Description 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 l

Ural 1081 Binary Lexicographic Sequence(DP)

题目地址:Ural 1081 先用dp求出每个长度下的合法序列(开头为1)的个数.然后求前缀和.会发现正好是一个斐波那契数列.然后每次判断是否大于此时长度下的最少个数,若大于,说明这一位肯定是1,若小于,则肯定是0.就这样不断输出出来即可. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #in

ural Binary Lexicographic Sequence (dp + dfs)

http://acm.timus.ru/problem.aspx?space=1&num=1081 有一个二进制序列,定义为不能有两个连续的1出现,才是合法的.给出序列的长度n,求合法的二进制序列中按字典序排序后第k个序列是什么. 设dp[i][0]和dp[i][1]分别表示第i位上是0和1的个数. 那么dp[i][0] = dp[i-1][0] + dp[i-1][1]:dp[i][1] = dp[i-1][0],打表发现和斐波那契数列相似. 求第k个合法的序列时,因为是按字典序排序,可以发现

URAL1081——DP—— Binary Lexicographic Sequence

Description 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 seq

数的排列问题

突然想到一个问题,对于给定的几个数怎样用程序把所有的数的排列全部输出呢 查了一下运用的思想是这样的 其实这个和深度优先搜索很像 把数组分成两个部分下标0~s为排好的,s~e为没有排的 对数的操作从s开始 1 void sort(vector<int> &nums,int s,int e){ 2 for(int i = s ; i<e ;++i){ 3 swap(nums,s,i); 4 sort(nums,s+1,e); } 7 } 对于上图的同一列,先s和s交换(相等,实质没有

有环积分号的单行数式排列规则

使用MathType的用户大部分都知道,有环积分中的积分函数式需要排在两个积分号的居中位置.但是在操作过程中一些用户还不是很了解,本教程将详解有环积分的单行数式排列规则.有环积分样式如下: 在这个数式中,积分函数式“T(m) nk…”.“R(x)+Q(y)”应与“=”对齐,排在两个积分号的居中位置,不能与底线平齐.下面的排法是不正确的: 积分号上下限原则上应该排在它的上面和下面,一般用七号字.如: 在格式上,当上下限在积分号上面和下面时,不能排在上和下的居中位置,而是上限偏右,下限偏左.但如果只

P1384 幸运数与排列

P1384 幸运数与排列 神奇的(逆)康托展开:求1到n的全排列中字典序第k小的排列 $k<=10^9<13!$,显然$k$最多只会影响后$13$位 前面一大串都是有序从小到大排列的,于是搞个数位dp 后面一小串用逆康托展开求出原串,枚举是否符合条件. #include<iostream> #include<cstdio> #include<cstring> #include<vector> #include<algorithm> u