数位DP POJ
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数位DP POJ相关的知识,希望对你有一定的参考价值。
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
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 }
以上是关于数位DP POJ的主要内容,如果未能解决你的问题,请参考以下文章