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

Sponsor

一道数位dp+二分

很久没写数位dp了 借此题重新复习一下

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#define ll long long
int a[20];
ll dp[20][2][2][2];
ll n, m;
using namespace std;ll dfs(int pos, int pre1, int pre2, int k, bool limit){if(pos == 0)return k;if(!limit && dp[pos][pre1][pre2][k] != -1)return dp[pos][pre1][pre2][k];int up = limit? a[pos] : 9;ll cnt = 0;for(int i = 0; i <= up; i++){if(k == 1)cnt += dfs(pos - 1, i == 6,pre1, 1, i == a[pos] && limit);elsecnt += dfs(pos - 1, i == 6,pre1, (i == 6) && pre1 && pre2, i == a[pos] && limit);}if(!limit)dp[pos][pre1][pre2][k] = cnt;return cnt;
}ll solve(ll x){int sum = 0;while(x){a[++sum] = x % 10;x = x / 10; }return dfs(sum, 0, 0, 0, 1);
}int main(){memset(dp, -1, sizeof(dp));int t;cin >> t;while(t--){cin >> n;ll l = 1, r = 0x3f3f3f3f3f3f3f3f, mid;while(l <= r){mid = (l + r) / 2;if(solve(mid) < n)l = mid + 1;elser = mid - 1;}cout << l << endl;}}

poj3208 Apocalypse Someday (数位dp + 二分)相关推荐

  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 + 二分)

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

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

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

  4. 数位DP POJ3208 Apocalypse Someday

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

  5. POJ3208:Apocalypse Someday

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

  6. poj3208 Apocalypse Someday

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

  7. HDU3271 SNIBB(数位dp+二分)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3271 题意: 有两种询问: q=1,在[x,y]区间内,转换成b进制数,数位和为m的有多少个: q=2 ...

  8. poj3208 Apocalypse Someday 题解报告

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

  9. hrbust 2382(数位dp+二分)

    极难的问题 Time Limit: 1000 MS Memory Limit: 200000 K Total Submit: 20(9 users) Total Accepted: 6(6 users ...

最新文章

  1. 手把手教你刷新BACKUP Bios
  2. 跟着开涛学SpringMVC 第一章源代码下载
  3. Java基础day16
  4. 最终幻想4 android,《最终幻想4》安卓版发布:重温那段曲折的冒险故事
  5. [翻译]Windows Phone 7用户界面框架
  6. mybatis的if test 条件判断
  7. Python之IO编程——文件读写、StringIO/BytesIO、操作文件和目录、序列化
  8. java闹钟程序声音_跪求高手帮忙写一个JAVA手机闹钟程序 实现添加铃声和设置多闹钟...
  9. 程序员:站在“自学”鄙视链顶端的王者
  10. yarn vite vue3.x
  11. MyBatis学习总结(21)——自定义MyBatis Generator插件
  12. 查看linux的系统命令,命令行查看Linux的系统版本和内核版本以及机器字长
  13. MAC查看CPU信息
  14. 193.有效电话号码
  15. 关于冒泡、快排、二分排序算法分析
  16. python类使用异步_异步Python类
  17. uni-app实战仿微信app开发
  18. 漂亮的html跳转页面代码,HTML网页实例代码:简洁漂亮的跳转等待页面
  19. MATLAB之M文件
  20. Miracle密码算法开源库(三)分析 :mrarth1.c

热门文章

  1. 中望3D2022建模注意事项
  2. 计算机网络地址分类及子网划分
  3. python应用异常解决:sheet.column_dimensions[‘C‘].font=italic24Font无效
  4. android ViewFlipper使用示例(注册引导页)
  5. biee 12c linux 安装,oracle biee 12c linux ha 集群安装配置手册.pdf
  6. 脑机接口3——生理基础
  7. 免费的VR全景网站是什么?免费VR全景网站有什么用?
  8. 基于phpqrcode生成带LOGO图标的二维码(源代码例子)
  9. Spring-webflow基础讲解
  10. tableau:多维地图