题意

Language:Default
Apocalypse Someday
Time Limit: 1000MS Memory Limit: 131072K
Total Submissions: 2499 Accepted: 1288

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

定义十进制下有3个连续的6的数为魔鬼数。有T个询问,求第k小的魔鬼数。

T<=1000,k<=5e7

分析

参照xyc1719的题解。

由于K有5e7那么大,哪怕线性dp,常数稍大就会有TLE的风险。如果内存小于128MB又会有MLE的问题显然,预处理出第k大的魔鬼数是不可靠的。

由于T较小,我们转而考虑能否像计数dp一样将先大致预处理出辅助数组,再进行“拼凑”。回答是可行的。但dp数组的定义是与数字的位数有关。

定义\(f[i,k]\)表示\(i\)位数,开头有\(k\)个连续的6的数的数目。注意允许出现前导0

状态转移方程:

\[f[i,0]=9∗(f[i−1,0]+f[i−1,1]+f[i−1,2]\\
f[i,1]=f[i−1,0]\\
f[i,2]=f[i−1,1]\\
f[i,3]=f[i−1,2]+f[i−1,3]∗10
\]

每次在询问时,利用辅助数组进行转移。

注意设计状态转移方程时,注意不重复不遗漏

时间复杂度\(O(\log ans)\)

代码

#include<iostream>
#define rg register
#define il inline
#define co const
template<class T>il T read(){
rg T data=0,w=1;rg char ch=getchar();
while(!isdigit(ch)) {if(ch=='-') w=-1;ch=getchar();}
while(isdigit(ch)) data=data*10+ch-'0',ch=getchar();
return data*w;
}
template<class T>il T read(rg T&x) {return x=read<T>();}
typedef long long ll; ll f[21][4];
void prework(){
f[0][0]=1;
for(int i=0;i<20;++i){
for(int j=0;j<3;++j){
f[i+1][j+1]+=f[i][j];
f[i+1][0]+=f[i][j]*9;
}
f[i+1][3]+=f[i][3]*10;
}
}
int n,m;
int main(){
prework();
for(int t=read<int>();t--;){
read(n);
for(m=3;f[m][3]<n;++m);
for(int i=m,k=0;i;--i){
for(int j=0;j<=9;++j){
ll cnt=f[i-1][3];
if(j==6||k==3)
for(int l=std::max(3-k-(j==6),0);l<3;++l) cnt+=f[i-1][l];
if(cnt<n) n-=cnt;
else{
if(k<3){
if(j==6)++k;
else k=0;
}
printf("%d",j);
break;
}
}
}
puts("");
}
return 0;
}

POJ3208 Apocalypse Someday的更多相关文章

  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)

    数位DP加二分 //数位dp,dfs记忆化搜索 #include<iostream> #include<cstdio> #include<cstring> usin ...

  3. poj3208 Apocalypse Someday[数位DP]

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

  4. POJ-3208 Apocalypse Someday (数位DP)

    只要某数字的十进制表示中有三个6相邻,则该数字为魔鬼数,求第X小的魔鬼数\(X\le 5e7\) 这一类题目可以先用DP进行预处理,再基于拼凑思想,用"试填法"求出最终的答案 \( ...

  5. POJ 3689 Apocalypse Someday [数位DP]

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

  6. Apocalypse Someday

    Apocalypse Someday 定义一个数是合法的,当且仅当中间出现至少一个连续的大于三个的6,求第x个合法的数,\(x\leq 50,000,000\) 解 首先,注意到求第几个,即想到试填法 ...

  7. POJ3208:Apocalypse Someday

    传送门 很神奇的一道题,正解是AC自动机+数位DP,个人感觉POPOQQQ大爷的方法更方便理解. 按照一般套路,先搞个DP预处理,设$f[i][0/1/2/3]$分别表示对于$i$位数,其中有多少个前 ...

  8. 【POJ3208】Apocalypse Someday

    Description 666号被认为是神秘的"野兽之数",在所有以启示录为主题的大片中都是一个被广泛使用的数字.但是,这个数字666不能总是在脚本中使用,所以应该使用1666这样 ...

  9. POJ 3208 Apocalypse Someday

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

随机推荐

  1. volatile与synchronized有什么区别?

    下列说法正确的是()? A.我们直接调用Thread对象的run方法会报异常,所以我们应该使用start方法来开启一个线程 B.一个进程是一个独立的运行环境,可以被看做一个程序或者一个应用.而线程是在 ...

  2. 基于vue项目的组件中导入mui框架初始化滑动等效果时需移除严格模式的问题

    基于vue项目的组件中导入mui框架初始化滑动等效果时,控制台报错:Uncaught TypeError: 'caller', 'callee', and 'arguments' properties ...

  3. 泊爷带你学go -- 反射的经典玩法

    package main import ( "fmt" "reflect" ) type order struct { ordId int customerId ...

  4. JAVAEE期末项目------文章发布系统

    项目文档和代码的GitHub地址:https://github.com/xiangbaobaojojo/- 1.项目介绍 (计科四班  蔡春燕 20150141401)和我 陈香宇(计科四班  201 ...

  5. 关于jQuery实现CheckBox全选只能生效一次的问题

    //这代码只有一次全选.全不选的效果 第三次点击checkall会没有任何效果 $("#checkall").click(function(){ $('input[name=&qu ...

  6. Java课程----自我介绍

      我是一名信息院的学生,今年今日是大二下学期,马上就要大三了,自己对于专业的认知还是太浅.主要是因为之前的大学生活特别懒散,并不积极向上.但是我想说的是,我们大学生,一定不要碌碌无为,要有所作为.我 ...

  7. 鼠标跟随效果 vue或者js通用

    this.$refs.tooltip.getBoundingClientRect() => 用于获取某个元素相对于视窗的位置集合.集合中有top, right, bottom, left等属性. ...

  8. 部署Qt应用时候报错0xc000007b

    情况: 在开发环境可以运行,部署到其他电脑无法运行: 排错:百度.谷歌了很多方法不行,后来发现添加了Qt\5.11.0\mingw53_32\bin环境变量,程序执行正常,去掉就报错:猜测估计是dll ...

  9. python面试题收集

    1.请问如何修改以下Python代码,使得下面的代码调用类A的show方法? class A(object) def show(self): print 'derived show' class B( ...

  10. linux 常用命令及实例

    转载:https://www.cnblogs.com/xiaoyafei/p/9163976.html 在linux中,绝大多数命令都参照 命令 选项 参数 选项:适用于调整命令的功能的 参数:指的是 ...