數位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
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 }
數位DP POJ