• 【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("
    ");
    92     }
    93     return 0;
    94 }
    [POJ3208]

    2016-07-17 10:03:41

  • 相关阅读:
    编程题练习
    算法:快速排序
    ansible的playbook剧本
    Django框架之验证码生成示例
    Django之auth模块用户认证模块
    Django框架之models和不依赖Qquery的ajax请求
    Django框架之序列化和上传文件
    Django高级篇一RESTful架构及API设计
    Git工具和GitHub的使用
    python之Flask框架
  • 原文地址:https://www.cnblogs.com/Konjakmoyu/p/5677944.html
Copyright © 2020-2023  润新知