poj 3208 Apocalypse Someday(数位DP,4级)

13 篇文章 0 订阅

Apocalypse Someday
Time Limit: 1000MS Memory Limit: 131072K
Total Submissions: 1385 Accepted: 632

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
思路:数位DP dp[位数][状态] 合法的含666的个数

             0 无六 1最后一位是6 2最后两位是6 3已经合法

#include<cstdio>
#include<cstring>
#include<iostream>
#define FOR(i,a,b) for(int i=a;i<=b;++i)
#define clr(f,z) memset(f,z,sizeof(f))
#define LL long long
using namespace std;
int dp[15][4];///0,1,2,3have how many 6
int turn[4][10];
LL ans;
void init()
{
  clr(dp,0);clr(turn,0);
  turn[0][6]=1;
  turn[1][6]=2;
  turn[2][6]=3;
  FOR(i,0,9)turn[3][i]=3;
  dp[0][3]=1;///终态
  FOR(i,1,14)
  FOR(j,0,3)FOR(k,0,9)
  dp[i][j]+=dp[i-1][ turn[j][k] ];
}
LL get(int n)
{ int len,staue=0;
  FOR(i,1,14)
  {
    if(dp[i][0]>n)
    { //cout<<i<<endl;
      len=i;break;
    }
  }
  int add;LL ans=0;
  for(int i=len-1;i>=0;--i)
  {
    for(int j=0;j<10;++j)
    {
      if(dp[i][ turn[staue][j] ]<n)
        n-=dp[i][ turn[staue][j] ];
      else {add=j;break;}

    }
    ans=ans*10+add;
    staue=turn[staue][add];
  }
  return ans;
}
int main()
{
  int cas,n;
  init();
  scanf("%d",&cas);
  while(cas--)
  {
    scanf("%d",&n);
    printf("%I64d\n",get(n));
  }
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值