Problem 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

题意:t 组询问,每组输出第 n 个包含连续 的三个 6 的数

思路:数位dp,用 dp[i][0]、dp[i][1]、dp[i][2]、dp[i][3] 分别表示 i 位数中首位不含 666、首位 1个 6 且不含 666、首位连续 2 个6 且不含 666、含有 666 的数的数量,用二分不断划分区间找第 n 个满足条件的数即可,由于数据范围,二分的右边界需要设一极大值,一般的 0x3f3f3f3f 仍达不到上界,需要换成 0x3f3f3f3f3f3f3f3f

Source Program

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstdlib>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<ctime>
#include<vector>
#define INF 0x3f3f3f3f3f3f3f3fll
#define PI acos(-1.0)
#define N 31
#define MOD 10007
#define E 1e-6
typedef long long LL;
using namespace std;
int bit[N];
LL dp[N][4];
int judge(int sta,int x)
{if(sta==3)return 3;if(x!=6)return 0;return sta+1;
}
LL dfs(int pos,int sta,bool limit)
{if(pos<0)return sta==3;if(!limit && dp[pos][sta]!=-1)return dp[pos][sta];LL temp=0;int up=limit?bit[pos]:9;for(int i=0;i<=up;i++)temp+=dfs(pos-1,judge(sta,i),limit&&i==up);if(!limit)dp[pos][sta]=temp;return temp;
}
LL solve(LL x)
{int pos=0;while(x){bit[pos++]=x%10;x/=10;}return dfs(pos-1,0,true);
}
LL Find(int x)
{LL left=0,right=INF;LL res=-1;while(left<=right){LL mid=(left+right)/2;if(solve(mid)<x)left=mid+1;else{res=mid;right=mid-1;}}return res;
}
int main()
{int t;scanf("%d",&t);memset(dp,-1,sizeof(dp));while(t--){int n;scanf("%d",&n);printf("%lld\n",Find(n));}return 0;
}

Apocalypse Someday(POJ-3208)相关推荐

  1. poj 3208 Apocalypse Someday(数位dp)

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

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

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

  3. Bailian2734 十进制到八进制【入门】(POJ NOI0113-45)

    问题链接:POJ NOI0113-45十进制到八进制 2734:十进制到八进制 总时间限制: 1000ms 内存限制: 65536kB 描述 把一个十进制正整数转化成八进制. 输入 一行,仅含一个十进 ...

  4. Bailian2676 整数的个数【入门】(POJ NOI0105-11)

    问题链接:POJ NOI0105-11 整数的个数 2676:整数的个数 总时间限制: 1000ms 内存限制: 65536kB 描述 给定k(1 < k < 100)个正整数,其中每个数 ...

  5. Bailian4029 数字反转【进制】(POJ NOI0105-29)

    问题链接:POJ NOI0105-29 数字反转 4029:数字反转 总时间限制: 1000ms 内存限制: 65535kB 描述 给定一个整数,请将该数各个位上数字反转得到一个新数.新数也应满足整数 ...

  6. Bailian2735 八进制到十进制【入门】(POJ NOI0113-46)

    问题链接:POJ NOI0113-46 八进制到十进制 2735:八进制到十进制 总时间限制: 1000ms 内存限制: 65536kB 描述 把一个八进制正整数转化成十进制. 输入 一行,仅含一个八 ...

  7. Silver Cow Party (POJ - 3268 )

    Silver Cow Party (POJ - 3268 ) 这道题是我做的最短路专题里的一道题,但我还没做这个,结果比赛就出了,真是.......... 题目: One cow from each ...

  8. 吴昊品游戏核心算法 Round 7 —— 熄灯游戏AI(有人性的Brute Force)(POJ 2811)

    暴力分为两种,一种属于毫无人性的暴力,一种属于有人性 的暴力.前面一种就不说了,对于后面一种情况,我们可以只对其中的部分问题进行枚举,而通过这些子问题而推导到整个的问题中.我称之为有人性的Brute ...

  9. 【二分】Best Cow Fences(poj 2018)

    Best Cow Fences poj 2018 题目大意: 给出一个正整数数列,要你求平均数最大,长度不小于M的字串,结果乘1000取整 输入样例 10 6 6 4 2 10 3 8 5 9 4 1 ...

  10. 昂贵的聘礼(poj 1062)

    Description 年轻的探险家来到了一个印第安部落里.在那里他和酋长的女儿相爱了,于是便向酋长去求亲.酋长要他用10000个金币作为聘礼才答应把女儿嫁给他.探险家拿不出这么多金币,便请求酋长降低 ...

最新文章

  1. 第一次作业词频分析之王熹篇
  2. 云服务器如何导入文件,如何将文件导入云服务器中
  3. vs code linux opencv,ubuntu+vscode 测试运行opencv
  4. C#中如何将字符串转换byte[],同时如何将byte[]换成字符串?
  5. linux系统 qt开发,老板让我写一个Windows程序,结果我在Linux下用Qt开发
  6. 巧用Ajax的beforeSend 提高用户体验
  7. 等比数列求和python_python计算等差数列
  8. CISCO路由器的备份与还原(2)
  9. html 怎么让他变成圆角,html让图片变圆角
  10. 张一鸣的大学四年收获及工作感悟
  11. 嵌入式如何学习与职业规划
  12. GTD时间管理学习心得(1)
  13. 将用户需求转成产品需求
  14. 2020年—岁月静好,温暖如初
  15. 遥感的几何校正、正射校正、辐射校正
  16. Python面试——基础面试题
  17. Windows Phone 7体验
  18. [leetcode]488. Zuma Game
  19. Q1利润不及预期股价仍大涨,疫情“摧残”下Facebook求生有新机?
  20. 海思SDK安装过程以及OSDRV编译不成功出现的问题的解决

热门文章

  1. 干货 | 用Python做图像处理:图像导数实战
  2. STM32之ADC单通道连续例程
  3. 再有人问你为什么MySQL用B+树做索引,就把这篇文章发给她
  4. 阿里文娱技术专家战獒: 领域驱动设计详解之What, Why, How?
  5. 备战双 11!96秒,100亿,支付宝万级规模 K8s 集群管理系统如何设计?
  6. SpringBoot2.0 整合 Shiro 框架,实现用户权限管理
  7. 用户管理实用命令(第二版)
  8. Zabbix3.2.6之通过JMX监控Tomcat
  9. 《Springboot极简教程》Springboot使用Kotlin和Java混合编程
  10. Vijos P1196吃糖果游戏[组合游戏]