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.

计数类dp的变种。
先预处理出dp[i][0..3],0..2表示i位数里开头有0..2个6的【非魔鬼数数量】,3表示i位数里的魔鬼数数量。
然后从高位向低位试填。在每一位数字从小到大试填。如果填上这个数总数就多了,那这一位就填这个数【但是不累加进总数】,否则将总数累加,并试填下一个数。
如何计算填好该位后的总数呢?不妨设i位填j之后,结尾6的个数为cnt。【这里注意,和前面的dp数组定义时一样,cnt=3代表是魔鬼数(即前面已经出现了666),并不一定结尾要有666,同理cnt<3代表结尾有cnt个6,且不是魔鬼数。例如5666066___的cnt是3】那么后面可以填dp数组下标为3,2,…,3-cnt的数
即tot=now+∑(dp[i-1][j]|cnt+j>=3)。now表示之前的总数。

#include<cstdio>
#include<cstring>
int dp[20][5],ans[20];
int main()
{int i,j,k,m,n,p,q,x,y,z,T,now,cnt,tot;dp[0][0]=1;for (i=0;i<=15;i++)for (j=0;j<=3;j++){dp[i+1][j==3?3:j+1]+=dp[i][j];dp[i+1][j==3?3:0]+=dp[i][j]*9;}scanf("%d",&T);while (T--){memset(ans,0,sizeof(ans));scanf("%d",&x);p=3;while (dp[p][3]<x) p++;now=cnt=0;for (i=p;i>=1;i--){for (j=0;j<=9;j++){y=cnt;if (cnt<3){if (j==6) cnt++;else cnt=0;}tot=now;for (k=3;k>=0;k--)if (cnt+k>=3) tot+=dp[i-1][k];if (tot>=x){ans[i]=j;break;}now=tot;cnt=y;}}for (i=p;i>=1;i--)printf("%d",ans[i]);printf("\n");}
}

poj3208 Apocalypse Someday相关推荐

  1. POJ3208:Apocalypse Someday

    题目描述 The number 666 is considered to be the occult "number of the beast" and is a well use ...

  2. poj3208 Apocalypse Someday (数位dp + 二分)

    The number 666 is considered to be the occult "number of the beast" and is a well used num ...

  3. poj3208 Apocalypse Someday 题解报告

    题目传送门 [题目大意] 包含连续的至少三个6的数称为"beastly number",将这些数从小到大排序,求第n个数. [思路分析] (注:以下加粗的"数" ...

  4. 数位DP POJ3208 Apocalypse Someday

    题目大意 求第X个有3个连续的6的数. 解题思路 #include<iostream> #include<algorithm> using namespace std; int ...

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

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

  6. poj 3208 Apocalypse Someday 数位dp+二分答案

    Apocalypse Someday Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 2203   Accepted: 11 ...

  7. Apocalypse Someday(POJ-3208)

    Problem Description The number 666 is considered to be the occult "number of the beast" an ...

  8. 【POJ3208】Apocalypse Someday

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

  9. poj 3208 Apocalypse Someday(数位dp)

    题意:给定n,输出第n大包含666的数字. 分析:数位dp,详见<算法竞赛进阶指南>P342-344. 代码: #include<iostream> #include<c ...

最新文章

  1. 你哪来这么多事(六):职工信息查找
  2. Android canvas.translate
  3. 聚类分析与相关算法(Kmeans等)详解
  4. 企业文档管理_为什么这么多企业文档如此糟糕?
  5. 1040. Longest Symmetric String (25)-PAT甲级真题
  6. Visio中插入公式
  7. dell系统重装后无法进入系统_戴尔装win7后无法进入系统怎么办?戴尔装win7后进不了系统解决方法...
  8. mysql tmp mysql.sock_MySQL搭建过程中的“/tmp/mysql.sock错误解决
  9. ENFJ型的人:什么样的人很适合人工智能方向
  10. 从零到大神,135排版训练营给你实实在在的排版!
  11. 携程测试经理网盘爆出面试题!!!【内附答案】
  12. 3.3. debug ip igrp
  13. Apache Dubbo详解
  14. 分数化成有限小数的方法_分数化小数的方法|小数化分数题目
  15. procast2021学习笔记
  16. 小额支付管理平台的设计与实现
  17. 基恩士CSV点云文件转PCD文件 PYTHON版
  18. python的mapl画图y轴排_Python三维绘图之Matplotlib库的使用方法
  19. java声明多个同类变量方法
  20. 1.1.8 怎样在Word的页眉中插入一级标题

热门文章

  1. 页数数字出现次数统计
  2. 【人工智能】模糊逻辑基本原理
  3. 中台 (Middle Office)
  4. 联想一键恢复不能用的情况下,win8无缝升级win10的方法
  5. 鑫众棋牌源码下载架设教程支持PC安卓苹果附说明
  6. 动力节点王鹤Spring Boot笔记
  7. Au 闪退解决方法(很邪门)
  8. 通知短信有什么作用?它的应用场景有哪些?
  9. 第二单元笔记——应用层万字长文
  10. 一、项目整体管理(输入/工具与技术/输出)