【POJ3208】 (DP)

Apocalypse Someday

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

 

【题意】

  询问第k大含“666”的数。

 

【分析】

  DP,考虑下面填数方案:

    

  反正我就用这种笨方法了~~

 

代码如下:

 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<cstring>
 4 #include<iostream>
 5 #include<algorithm>
 6 #include<queue>
 7 using namespace std;
 8 #define INF 0xfffffff
 9 #define LL long long
10 
11 LL f[15],d[15];
12 
13 void pri(int x,LL y)
14 {
15     int z=0;
16     LL yy=y;
17     while(yy) z++,yy/=10;
18     for(int i=1;i<=x-z;i++) printf("0");
19     if(y!=0) printf("%I64d",y);
20 }
21 
22 int main()
23 {
24     freopen("a.in","r",stdin);
25     freopen("a.out","w",stdout);
26     int T;
27     scanf("%d",&T);
28     while(T--)
29     {
30         int n;
31         scanf("%d",&n);
32         f[3]=1;f[2]=f[1]=0;
33         d[0]=1;d[1]=10;
34         for(int i=1;i<=11;i++) d[i]=d[i-1]*10;
35         for(int i=4;i<=13;i++)
36         {
37             f[i]=d[i-3]+(f[i-1]+f[i-2]+f[i-3])*9;
38         }
39         for(int i=1;i<=12;i++) if(f[i]>=n&&f[i-1]<n)
40         {
41             LL now=n-f[i-1];//减掉开头为0
42             for(int j=i;j>=1;j--)//枚举填数
43             {
44                 for(int k=0;k<=9;k++)//填1~9
45                 {
46                     if(j==i&&k==0) continue;
47                     LL x;
48                     if(k==6) x=f[j-3]*9+f[j-2]*9+d[j-3];
49                     else x=f[j-1];
50                     if(now>x) now-=x;
51                     else
52                     {
53                         if(k==6)
54                         {
55                             LL y;
56                             if(now<=6*f[j-2])
57                             {
58                                 y=(now-1)/f[j-2];
59                                 now=now%(f[j-2]);now=now==0?f[j-2]:now;
60                                 printf("6%I64d",y);j--;break;
61                             }now-=6*f[j-2];
62                             if(now<=6*f[j-3])
63                             {
64                                 y=(now-1)/f[j-3];
65                                 now=now%(f[j-3]);now=now==0?f[j-3]:now;
66                                 printf("66%I64d",y);j-=2;break;
67                             }now-=6*f[j-3];
68                             if(now<=d[j-3])
69                             {
70                                 printf("666",now);
71                                 if(j!=3) pri(j-3,now-1);
72                                 j=1;break;
73                             }now-=d[j-3];
74                             if(now<=3*f[j-3])
75                             {
76                                 y=(now-1)/f[j-3];
77                                 now=now%(f[j-3]);now=now==0?f[j-3]:now;
78                                 printf("66%I64d",y+7);j-=2;break;
79                             }now-=3*f[j-3];
80                                 y=(now-1)/f[j-2];
81                                 now=now%(f[j-2]);now=now==0?f[j-2]:now;
82                                 printf("6%I64d",y+7);j--;break;
83                         }
84                         else printf("%d",k);
85                         break;
86                     }
87                 }
88             }
89             break;
90         }
91         printf("\n");
92     }
93     return 0;
94 }
[POJ3208]

 

2016-07-17 10:03:41

 

转载于:https://www.cnblogs.com/Konjakmoyu/p/5677944.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值