HyperlinkHyperlinkHyperlink

http://poj.org/problem?id=3208


DescriptionDescriptionDescription

定义包含三个连续的6的数为魔鬼数
求第nnn小的魔鬼数

数据范围:n≤5×109n\leq 5\times 10^9n≤5×109


SolutionSolutionSolution

设f[i][j]f[i][j]f[i][j]表示长度为iii,前jjj位都是6的魔鬼数个数(当j>3j>3j>3时,默认存到j=3j=3j=3处),该dpdpdp允许前导0的存在

对于f[i][0]f[i][0]f[i][0],只要这一位填的不是6,都可以转移过来,即
f[i][0]=9×(f[i−1][0]+f[i−1][1]+f[i−1][2])f[i][0]=9\times (f[i-1][0]+f[i-1][1]+f[i-1][2])f[i][0]=9×(f[i−1][0]+f[i−1][1]+f[i−1][2])

对于f[i][1]f[i][1]f[i][1],这一位只能填6,即
f[i][1]=f[i−1][0]f[i][1]=f[i-1][0]f[i][1]=f[i−1][0]

显然f[i][2]=f[i−1][1]f[i][2]=f[i-1][1]f[i][2]=f[i−1][1],f[i][3]=f[i−1][2]f[i][3]=f[i-1][2]f[i][3]=f[i−1][2]
但是,如果上一位前已经满足是魔鬼数,则这一位可以随便填,即
f[i][3]+=f[i−1][3]×10f[i][3]+=f[i-1][3]\times 10f[i][3]+=f[i−1][3]×10

接下来用试填法或二分答案转判定都可


CodeCodeCode

#include<cstdio>
#include<cctype>
#include<cstring>
#include<algorithm>
#define LL long long
using namespace std;int T,k;
LL n,f[25][4],m,cnt;
inline LL read()
{char c;LL d=1,f=0;while(c=getchar(),!isdigit(c)) if(c=='-') d=-1;f=(f<<3)+(f<<1)+c-48;while(c=getchar(),isdigit(c)) f=(f<<3)+(f<<1)+c-48;return d*f;
}
signed main()
{f[0][0]=1;for(register int i=0;i<21;i++){for(register 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;}T=read();while(T--){n=read();m=3;while(f[m][3]<n) m++;k=0;//计算答案的位数,k表示目前已经填了几个连续的6for(register int i=m;i;i--){for(register int j=0;j<=9;j++)//考虑j填0~9,第i位的填法{cnt=f[i-1][3];if(j==6||k==3)//如果这一位填的是6,如果是第一个,则后面要加上f[i-1][2]//如果是第二个6,则后面还要加上f[i-1][1]//如果是第三个6,则后面还要加上f[i-1][0]for(register int l=max(3-k-(j==6),0);l<3;l++)cnt+=f[i-1][l];if(cnt<n) n-=cnt;else{putchar(j+48);if(k<3) {if(j==6) k++;else k=0;}break;}}}putchar(10);}
}

POJ 3208 Apocalypse Someday相关推荐

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

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

  2. poj 3208 Apocalypse Someday(数位dp)

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

  3. #数位dp#poj 3208 Apocalypse Someday

    题目 定义一种神奇的数当且仅当数位上至少有3个6,问第n个神奇的数是多少 分析 那么可以预处理出由i位数字构成的魔鬼/非魔鬼数, f[i][0]=9∗(f[i−1][0]+f[i−1][1]+f[i− ...

  4. Poj 3208 Apocalypse Someday(数位dp + 二分)

    题目大意:   有三个连续的6在一起的数就是beast数,前几个beast数是:666,1666,2666,-   T组数据,每组询问排名为n的beast的数是什么? 解题思路 第一眼看到懵逼了,不是 ...

  5. Apocalypse Someday poj 3208

    题目 问你在所有包含666的数中,第n大的是多少.(1 ≤ n ≤ 50,000,000) .开头几个是666, 1666, 2666, 3666, 4666, 5666- 分析 套路 先dp预处理, ...

  6. Apocalypse Someday(POJ-3208)

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

  7. POJ3208:Apocalypse Someday

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

  8. poj3208 Apocalypse Someday

    Description The number 666 is considered to be the occult "number of the beast" and is a w ...

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

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

最新文章

  1. Mysql数据库的打开和关闭
  2. 【原】Linux设备网络硬件管理
  3. 【学习笔记】局域网基本概念和体系结构,以太网、无线局域网与PPP协议、HDLC协议
  4. 我们为什么需要实施实验室管理系统?
  5. UVA 10098 Generating Fast
  6. php pcre 什么用,PHP—PCRE正则表达式性能 - pcre
  7. 14 款命令行常用工具的替代品
  8. java实验报告6:异常处理程序设计
  9. The Jackknife and Bootstrap
  10. Springboot web项目简单统计在线人数
  11. C++入门 “引用”,“内联函数” 详解
  12. 普通相片打印纸如何长时间保存
  13. 什么是软件测试(功能、接口、性能、自动化)详解
  14. win7出现无法连接到代理服务器的错误,不能上网的问题的解决
  15. 充电误区那点事和电池医生类软件
  16. Excel统计所有工作簿数量?
  17. JAVA毕设项目众筹网(Vue+Mybatis+Maven+Mysql+sprnig+SpringMVC)
  18. 手机存储卡知识及真假辨别 提防山寨卡
  19. 【报告分享】中国音乐市场年度综合分析2021-易观智库(附下载)
  20. 《利用Python进行数据分析》学习笔记ch02-1(1)

热门文章

  1. Halcon焊点检测例子解析
  2. 【java】家庭收支管理系统
  3. linux dd 编辑,Vim编辑器中,在命令模式下的dd命令是用来( )。
  4. 【深度学习之模型优化】模型剪枝、模型量化、知识蒸馏概述
  5. 重组es6之es6转es5的方式
  6. 计蒜客 腾讯的一笔画游戏
  7. arcGIS10安装出现错误1911出现无法注册的dll文件库
  8. GAN靠「伪造思维」登上Nature子刊:首次合成神经活动数据,脑机接口训练速度提升20倍...
  9. Win11老是弹出弹窗广告怎么办?
  10. JavaScript中string与number