数位DP POJ3208 Apocalypse Someday

本文介绍了一个名为ApocalypseSomeday的编程题目,该题要求找出包含至少三个连续数字6的“beastly numbers”。文章给出了一个C++实现方案,通过递推算法计算出第n个beastly number。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Apocalypse Someday
Time Limit: 1000MS Memory Limit: 131072K
Total Submissions: 1870 Accepted: 902

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

Source

POJ Monthly--2007.03.04, Ikki, adapted from TCHS SRM 2 ApocalypseSomeday
 
 
 
我不明白我为什么要写这个题,好难……
lyd的题解 OTZ

 

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 int t,n,m,k;
 7 long long f[21][4];
 8 void work(){
 9     f[0][0]=1;
10     for(int i=0;i<20;i++){
11         for(int j=0;j<3;j++){
12             f[i+1][j+1]+=f[i][j];
13             f[i+1][0]+=f[i][j]*9;
14         }
15         f[i+1][3]+=f[i][3]*10;
16     }
17 }
18 int main(){
19     work();
20     scanf("%d",&t);
21     while(t){
22         t--;
23         scanf("%d",&n);
24         for(m=3;f[m][3]<n;m++);
25         k=0;
26         for(int i=m;i;i--){
27             for(int j=0;j<=9;j++){
28                 long long tmp=f[i-1][3];
29                 if(k==3||j==6)
30                     for(int p=max(0,3-k-(j==6));p<3;p++)
31                         tmp+=f[i-1][p];
32                 if(tmp<n)  n-=tmp;
33                 else{
34                     if(k<3&&j==6) k++;
35                     if(k<3&&j!=6) k=0;
36                     printf("%d",j);
37                     break;
38                 }
39             }
40         }
41         printf("\n");
42     }
43     return 0;
44 }

 

 

转载于:https://www.cnblogs.com/sdfzxh/p/7108023.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值