数位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. 我的Vim配置(自动补全/树形文件浏览)

    配置文件的下载路径在这里  http://files.cnblogs.com/files/oloroso/vim.configure.xz.gz 这实际上是一个 xz 格式的文件,添加的 gz 文件后 ...

  2. windows下的socket网络编程

    windows下的socket网络编程 windows下的socket网络编程 clinet.c 客户端 server.c 服务器端 UDP通信的实现 代码如下 已经很久没有在windows下编程了, ...

  3. 1.3---字符串重新排列后是否能够变成另一个字符串(CC150)

    import java.util.*; public class Same { public boolean checkSam(String str1, String str2) { // write ...

  4. 1.7---将矩阵元素为0的行列清零0(CC150)

    答案: import java.util.ArrayList; import java.util.List; public class Solution{ public static void mai ...

  5. 【leetcode】Merge Intervals

    Merge Intervals Given a collection of intervals, merge all overlapping intervals. For example,Given  ...

  6. XPath 教程

    http://www.w3school.com.cn/xpath/xpath_syntax.asp

  7. ios awakeFromNib 和 initWithCoder:

    During the instantiation process, each object in the archive is unarchived and then initialized with ...

  8. FFmpeg-20160413-snapshot-bin

    ESC 退出 0 进度条开关 1 屏幕原始大小 2 屏幕1/2大小 3 屏幕1/3大小 4 屏幕1/4大小 S 下一帧 [ -2秒 ] +2秒 ; -1秒 ' +1秒 下一个帧 -> -5秒 F ...

  9. sqlserverJDBC驱动链接

    final String DRIVER="com.microsoft.jdbc.sqlserver.SQLServerDriver"; final String URL=" ...

  10. sqlserver 动态行转列

    DECLARE @SQL VARCHAR(8000)SET @SQL = 'select overcode 'SELECT @SQL = @SQL + ' , max(case header when ...