数位DP加二分

//数位dp,dfs记忆化搜索
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long LL;
#define N 20 LL dp[N][3];//dp[i][j]表示长度为i,前面有j个6时不含666的数的个数
int num[N]; //c6表示前面6的个数
LL dfs(int len, int c6, bool ismax){
if(len == 0){
return 1;
}
if(!ismax && dp[len][c6] >= 0){
return dp[len][c6];
}
int max = ismax? num[len]:9;
LL cnt = 0;
for(int i = 0; i <= max; i++){
if(c6 == 2 && i == 6){
continue;
}
cnt += dfs(len-1, i == 6? c6 + 1: 0, ismax && i == max);
}
return ismax?cnt:dp[len][c6]=cnt;
} LL solve(LL n){
LL t = n;
int len = 0;
while(n){
num[++len]=n%10;
n/=10;
}
return t + 1 - dfs(len, 0, true);
} int main(){
memset(dp,-1,sizeof(dp));
int t;
cin>>t;
while(t--){
LL n;
scanf("%I64d", &n);
LL l = 666, r = 50000000666ll;
while(l < r){
LL m = (l + r)>>1;
if(solve(m) < n){
l = m + 1;
}else{
r = m;
}
}
printf("%I64d\n", l);
} return 0;
}

  

POJ3208 Apocalypse Someday(二分 数位DP)的更多相关文章

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

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

  2. 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的数. ...

  3. shuoj 1 + 2 = 3? (二分+数位dp)

    题目传送门 1 + 2 = 3? 发布时间: 2018年4月15日 22:46   最后更新: 2018年4月15日 23:25   时间限制: 1000ms   内存限制: 128M 描述 埃森哲是 ...

  4. Codeforces Round #460 (Div. 2) B Perfect Number(二分+数位dp)

    题目传送门 B. Perfect Number time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  5. POJ3208 Apocalypse Someday

    题意 Language:Default Apocalypse Someday Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 2 ...

  6. hihocoder #1301 : 筑地市场 二分+数位dp

    #1301 : 筑地市场 题目连接: http://hihocoder.com/problemset/problem/1301 Description 筑地市场是位于日本东京都中央区筑地的公营批发市场 ...

  7. CF431D Random Task 二分+数位dp

    One day, after a difficult lecture a diligent student Sasha saw a graffitied desk in the classroom. ...

  8. 【poj3208-Apocalypse Someday】数位DP

    题意:问你在所有包含666的数中,第n大的是多少.(1 ≤ n ≤ 50,000,000) .开头几个是666, 1666, 2666, 3666, 4666, 5666… 题解: 这题可以用AC自动 ...

  9. poj3208 Apocalypse Someday[数位DP]

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

随机推荐

  1. 9. JEB 1.5插件编写一

    2.0之后好像新增加了很多API,有所区别 本文采用Java+Eclipse作为开发环境 插件支持的语言  文档: jeb-1.5/doc/index.html 一.Hello World工程 1. ...

  2. 2017/1/7 学习笔记 jar包,maven

    ① 关于tar,jar,war文件 tar是通用的另一种打包格式,为了部署到服务器时方便. jar是java app server识别的java部署格式,其实是Zip文件,只是内部的文件有规范. wa ...

  3. CSS中的float与clear

    参考: http://www.cnblogs.com/iyangyuan/archive/2013/03/27/2983813.html http://www.cnblogs.com/iloveyou ...

  4. Linux下cp -rf总是提示覆盖的解决办法

    通常情况下使用cp -rf进行文件或者文件夹的管理时一般就不再提醒是否覆盖.然而在内网的一台机器上使用cp -rf却提示是否覆盖.难道和常用的命令不同? [root@xxxx test]# cp -r ...

  5. 一步步搭建docker私有仓库并从私有仓库中下载镜像

    一步步搭建docker私有仓库 #下载镜像 docker pull registry#查看镜像 docker images #运行私有仓库,指定端口和数据卷 docker run -d -p : -v ...

  6. 2. javacript高级程序设计-在HTML中使用JavaScript

    1.1 <script>元素 向HTML页面中插入JavaScript的主要方法,就是使用<script>元素,<script>元素定义了一下6个元素: (1). ...

  7. Java for LeetCode 210 Course Schedule II

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  8. Django函数——url()

    The url() function is passed four arguments, two required: regex and view, and two optional: kwargs, ...

  9. C# 对象操作

    //********************************************************************************* //************** ...

  10. 【leetcode】Evaluate Reverse Polish Notation(middle)

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...