Apocalypse Someday
Time Limit: 1000MS   Memory Limit: 131072K
Total Submissions: 1807   Accepted: 873

Description

The number 666 is considered to be the occult “number of the beast” and is a well used number in all major apocalypse themed blockbuster movies. However the number 666 can’t always be used in the script so numbers such as 1666 are used instead. Let us call the numbers containing at least three contiguous sixes beastly numbers. The first few beastly numbers are 666, 1666, 2666, 3666, 4666, 5666…

Given a 1-based index n, your program should return the nth beastly number.

Input

The first line contains the number of test cases T (T ≤ 1,000).

Each of the following T lines contains an integer n (1 ≤ n ≤ 50,000,000) as a test case.

Output

For each test case, your program should output the nth beastly number.

Sample Input

3
2
3
187

Sample Output

1666
2666
66666

Source

POJ Monthly--2007.03.04, Ikki, adapted from TCHS SRM 2 ApocalypseSomeday

题意:给定一个数字串A,不含前导0,长为m。m<=9求第P小的包含666的数字 P<=109

课件上是个加强版:

给定一个数字串A,不含前导0,长为m。m<=9
求第P小的包含子串A的数字
P<=109

做法:

二分答案X,转为判断小于等于X的包含子串A的数字有多少个
F[i][j][k][l]表示,填完前i位,KMP指针指向A的第j位,之前是否出现过子串A的状态为k(0/1),下一位能否任意填数的状态为l(0/1),的方案数

这里也可以用类似的做法二分答案

或者可以不二分答案,因为答案是多少位可以判断出来,直接数位DP行了

d[i][0]表示i位数中首位不为6且不含666的数的数量
d[i][1]表示i位数中首位连续1个6并且不含666的数的数量
d[i][2]表示i位数中首位连续2个6并且不含666的数的数量
d[i][3]表示i位数中含有666的数的数量

然后依次从小到大判断每一位填什么行了,记录sum为当前含666的数字的个数,six为当前需要几个6

特判真的好麻烦,于是借鉴 http://blog.csdn.net/keshuai19940722/article/details/41087343 使用g[i][j]表示当前需要i个6,填了数字j后还需要几个6

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long ll;
const int N=;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-; c=getchar();}
while(c>=''&&c<=''){x=x*+c-''; c=getchar();}
return x*f;
}
int g[][] = {
{, , , , , , , , , },
{, , , , , , , , , },
{, , , , , , , , , },
{, , , , , , , , , }
};
int d[N][];
void dp(){
d[][]=;
for(int i=;i<=;i++){
d[i][]=(d[i-][]+d[i-][]+d[i-][])*;
d[i][]=d[i-][];
d[i][]=d[i-][];
d[i][]=d[i-][]*+d[i-][];
//printf("d %d %d\n",i,d[i][3]);
}
}
int n;
int main(){
//freopen("in.txt","r",stdin);
dp();
int T=read();
while(T--){
n=read();
int dit=,six=,sum=;
while(d[dit][]<n) dit++;
//printf("dit %d\n",dit);
while(dit){//printf("hidit %d %d\n",dit,sum);
for(int i=;i<=;i++){
int _=;
for(int j=;j>=g[six][i];j--) _+=d[dit-][j];
//printf("_ %d %d %d\n",_,sum,six);
if(sum+_>=n){
printf("%d",i);
six=g[six][i];
break;
}
sum+=_;
}
dit--;
}
putchar('\n');
}
}
 
 

POJ 3689 Apocalypse Someday [数位DP]的更多相关文章

  1. poj3208 Apocalypse Someday 数位dp+二分 求第K(K <= 5*107)个有连续3个6的数。

    /** 题目:poj3208 Apocalypse Someday 链接:http://poj.org/problem?id=3208 题意:求第K(K <= 5*107)个有连续3个6的数. ...

  2. poj3208 Apocalypse Someday[数位DP]

    数位中出现至少3个连续的'6'的数字(称魔鬼数),询问满足要求的排名k的数. 经典题型.采用试填法. 递推做法:预处理出$i$位数字中满足要求的数(下记为'魔鬼数').对每一位都从0到9试一遍,然而卡 ...

  3. POJ 3208 Apocalypse Someday

    题意: 将含有连续的三个6的数称为不吉利数,比如666,1666,6662,但是6266吉利.则666为第一个不吉利数,输入整数n,求第n个不吉利数.(n <= 5*10^7) 解法: 如果是给 ...

  4. poj 3252 Round Numbers 数位dp

    题目链接 找一个范围内二进制中0的个数大于等于1的个数的数的数量.基础的数位dp #include<bits/stdc++.h> using namespace std; #define ...

  5. poj 3252 Round Numbers(数位dp 处理前导零)

    Description The cows, as you know, have no fingers or thumbs and thus are unable to play Scissors, P ...

  6. POJ 3252 Round Numbers(数位dp&amp;记忆化搜索)

    题目链接:[kuangbin带你飞]专题十五 数位DP E - Round Numbers 题意 给定区间.求转化为二进制后当中0比1多或相等的数字的个数. 思路 将数字转化为二进制进行数位dp,由于 ...

  7. $POJ$3252 $Round\ Numbers$ 数位$dp$

    正解:数位$dp$ 解题报告: 传送门$w$ 沉迷写博客,,,不想做题,,,$QAQ$口胡一时爽一直口胡一直爽$QAQ$ 先港下题目大意嗷$QwQ$大概就说,给定区间$[l,r]$,求区间内满足二进制 ...

  8. POJ 3208-Apocalypse Someday(数位dp)

    题意:给定n,输出第n大包含666的数字. 分析:dp[i][j][k][l]表示 长度为i,当前位是否是6,前一位是否6,是否已经包含666,表示的数量,再用二分找出第n大的这样的数字. #incl ...

  9. POJ - 3252 - Round Numbers(数位DP)

    链接: https://vjudge.net/problem/POJ-3252 题意: The cows, as you know, have no fingers or thumbs and thu ...

随机推荐

  1. c语言基础学习03

    =============================================================================涉及到的知识点有:编码风格.c语言的数据类型. ...

  2. linux 内核提权

    不经意间找到了大牛总结的一些Linux提权exp 我直接借花献佛分享给大家 #CVE #Description #Kernels CVE-2017-1000367 [Sudo] (Sudo 1.8.6 ...

  3. TI-RTOS 之 GPIO中断(按键)

    TI-RTOS 之 GPIO中断(按键) 前面已经用过LED, 定时器,这次来了解GPIO的中断是怎么用的,从CC1310+TI-RTOS的例程可以直接找到相应的例子程序,它的关键是在于要使能中断,也 ...

  4. Google PageSpeed Tools 性能测试分析

    今天给大家介绍下一个工具:Google PageSpeed Tools,根据官方的介绍,简单梳理如下: Page Speed Insights能针对移动设备和电脑设备衡量网页的性能.该工具会抓取网址两 ...

  5. LockSupport理解

    一.背景 在看并发包源码的时候看见过LockSupport,今天恰巧看到LockSupport字眼,于是看下jdk1.7中的源码结构.想着它应该是运用多线程的锁工具的,到底似乎怎么实现的呢? 二.使用 ...

  6. 测试left join和where的优先级

    --create table tab1--(--id int,--size int--) --create table tab2--(--size int,--name varchar(10)--) ...

  7. SMTP错误码建议解决方法

    https://wenku.baidu.com/view/0af30e01e87101f69e3195b8.html SMTP 错误码 / 建议解决方法 错误总表 101 Cannot Open Co ...

  8. QQ邮箱开启SMTP服务的步骤

    首先要确保你的QQ邮箱已经要开启超过一个月.对于新开启的邮箱,腾讯是不开放这些功能的. 方法/步骤 首先点QQ头像旁边的信封符号进入邮箱. 当然你也可以使用 mail.qq.com进邮箱 进入邮箱后点 ...

  9. 【视频编解码·学习笔记】3. H.264视频编解码工程JM的下载与编解码

    一.下载JM工程: JM是H.264标准制定团队所认可的官方参考软件.网址如下 http://iphome.hhi.de/suehring/tml/ 从页面中可找到相应的工程源码,本次选择JM 8.6 ...

  10. Python3基础知识之元组、集合、字典

    1.元组 元组特点元组是不可变的两个元组可以做加法,不能做减法 元组的方法 >>> S('a', 'b', 'c', 'd', 'e')>>> S=('a','b' ...