【POJ3208】Apocalypse Someday

10 篇文章 0 订阅

                                       Apocalypse Someday

Time Limit: 1000MS Memory Limit: 131072K
Total Submissions: 2232 Accepted: 1129

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

 

解析:
       二分+数位DP。

       能够二分的原因是随着数的增大,个数也呈递增的形式,所以二分找满足条件的最小的数即为答案。

 

代码:
 

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#define int long long 
using namespace std;

const int Max=12;
int t,l,r,mid,n,m,f[Max][Max][Max][2],num[Max];

inline int dfs(int pos,int p1,int p2,int tag,int limit)
{
	if(!pos) return tag;
	if(!limit&&~f[pos][p1][p2][tag]) return f[pos][p1][p2][tag];
	int mx=limit?num[pos]:9,ans=0;
	for(int i=0;i<=mx;i++)
	  ans+=dfs(pos-1,i,p1,tag||(p1==6&&p2==6&&i==6),limit&(i==mx));
	if(!limit) return f[pos][p1][p2][tag]=ans;
	return ans;
}

inline int check(int x)
{
	n=0;
	while(x)num[++n]=x%10,x/=10;
	return dfs(n,0,0,0,1);//注意必须从n开始不能reverse后从1开始,具体原因手动模拟一下
}

signed main()
{
	memset(f,-1,sizeof(f));
	scanf("%I64d",&t);
	while(t--)
	{
	  scanf("%I64d",&m);
	  l=0,r=1e10;
	  while(l<r)
	  {
	 	mid=(l+r)>>1;
	  	if(check(mid)>=m) r=mid;
	  	else l=mid+1;
	  }
	  printf("%I64d\n",r);
	}
	return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值