随笔 - 531  文章 - 0  评论 - 3  阅读 - 10215 
只要某数字的十进制表示中有三个连续的 6 ,古代人也认为这是个魔鬼的数,比如 666,1666,6663,16666,6660666

古代典籍中经常用“第X小的魔鬼的数”来指代这些数,

输入XX

,输出对应的第X个魔鬼数

 

二分 X ,看1~X 有几个这样的数,以此检验

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <vector>
#include <algorithm>
#include <vector>
#include <cstring>
using namespace std ;
#define int long long
 const int M =1e15;
  
  
 vector<int> vec;
 int f[30][2][10][3];
  
 int dfs(int id,int flag,int cnt,int num){
    if(cnt>=3) num=1;
    if(id<0) return num;
    if(f[id][flag][cnt][num]!=-1) return f[id][flag][cnt][num];
     
    int end =( flag? vec[id]:9 );
    int t=0;
    for(int i=0;i<=end;i++){
        if(i==6)
        t+= dfs(id-1,flag&&(i==end),cnt+1,num);
        else
        t+=dfs(id-1,flag&&(i==end),0,num);
    }
    if(flag==0) f[id][flag][cnt][num]=t;
    return t;
 }
 int chk(int x){
    vec.clear();
    while(x){
        vec.push_back(x%10);
        x/=10;
    }
    return dfs(vec.size()-1, 1,0,0);
 }
 void solve(){
    int k,ans=0;
    int l=0, r= M;
    cin>>k;
        while(l<=r){
            int md=(l+r)/2;
            if(chk(md)>=k) ans=md,r=md-1;
            else l=md+1;
        }
        cout<<ans<<endl;
 }
 signed main(){
    memset(f,-1,sizeof f);
    int tes;
    cin>>tes;
    while(tes--)
        solve();
 }

 


 


 

 

 



 

posted on   towboat  阅读(20)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示