数位DP POJ

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 }
时间: 2024-08-29 03:43:34

数位DP POJ的相关文章

POJ 3689 Apocalypse Someday [数位DP]

Apocalypse Someday Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 1807   Accepted: 873 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

POJ - 3286 - How many 0&#39;s? 【数位DP】

How many 0's? Time Limit: 1000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Description A Benedict monk No.16 writes down the decimal representations of all natural numbers between and including m and n, m ≤ n. How many 0's will he writ

TOJ 2294 POJ 3286 How many 0&#39;s? 数位dp

http://acm.tju.edu.cn/toj/showp2294.html http://poj.org/problem?id=3284 题外话:集训结束,回学校了.在宿舍看了这题,没什么好想法,去洗澡了.转了两个澡堂都特么没开..倒是在路上把这题想了.用回自己的电脑,不得不说苹果的字体渲染,真心高一个等级. 题意:给定两个数a和b,从a写到b,问一共写了多少个0. 分析:当然先转化为求0..a写多少个0.网上有更简单的做法,就是枚举每位作为0,则如果这一位本来是0,左边取1..a-1(不

POJ 3252 数位DP

链接: http://poj.org/problem?id=3252 题意: 给你一个区间l,r,求区间中有多少个数转化为二进制后1的个数大于等于0的个数 题解: 还是数位dp,不过多了前导0的判断 代码: 31 int a[40]; 32 int dp[40][80]; 33 34 int dfs(int pos, int sta, bool lead, bool limit) { 35 if (pos == -1) return sta >= 40; 36 if (!lead &&

POJ 3208 Apocalypse Someday 二分答案+数位DP

这题应该是POJ最强大的一道数位DP了吧 正解是AC自动机 不会 还是写数位DP吧 题目大意:我们令含有666的数字为不吉利数字,则可以得到一个递增数列: {an}=666,1666,2666,3666,4666,5666,6660,6661,.... 给定n,求an 首先我们把这个问题转化成另一个问题:给定n,求1~n中有多少个数含有666 解决了这个问题,把原问题二分答案即可 首先预处理f数组,令 f[i][0]表示i位数中首位不为6且不含666的数的数量 f[i][1]表示i位数中首位连续

POJ 1850 Code 数位DP

据说又是一道组合数学题,数学不好的我只想出的DP写法 注意如果输入不合法要输出0 #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdli

poj 3340 Barbara Bennett&#39;s Wild Numbers(数位DP)

Barbara Bennett's Wild Numbers Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 3153   Accepted: 1143 Description A wild number is a string containing digits and question marks (like 36?1?8). A number X matches a wild number W if they hav

POJ 3286 How many 0&#39;s(数位DP模板)

题目链接:http://poj.org/problem?id=3286 题目大意: 输入n,m,求[n,m]的所有数字中,0出现的总数是多少,前导零不算. 解题思路: 模板题,设dp[pos][num],pos为数位,num为当前0的数目,然后套数位DP模板即可. 还有之前的一些思考: 关于数位DP求0时,dp下标记录num有什么作用,num不是与后面的0的个数无关吗?是的,在(!limit&&!lead)的情况下,前面有多少0是不影响后面可以出现多少0的.但是,比如说dp[pos][nu

【POJ3208】传说中POJ最难的数位DP?(正解AC自动机,二解数位DP,吾异与之)

题意: 多组数据,每组求第n个包含'666'的数(不能断开),如1:666,2:1666,14:6667. 题解: AC自动机解法没去想,数位DP没学,这里有一种类似于数位DP,却又与数位DP不同,我称为数位树. 数位树: 将数n如线段树一样地拆分成多个小段,进行递归处理得出答案. 本题详(lue)解: 直接看每一位应该是什么数,然后n减去相应的数,使得在下一层转换为子问题"在开头有b个连续的6时,求第a个带'666'的数".就是如此简单,如此简单!!!! 代码来啦! #include