poj 3208 Apocalypse Someday 数位dp+二分答案

news/2024/7/7 16:02:19

Apocalypse Someday

Time Limit: 1000MS Memory Limit: 131072K
Total Submissions: 2203 Accepted: 1110

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

 

题意:

求第nn小的数位中含有三个连续的6的数。

简单数位DP+二分

#include<iostream>
#include<cstdio>
#include<cstring>

using namespace std;
typedef long long ll;
ll dp[30][30];
int a[30];
ll dfs(int pos,int sta,bool limit)
//计算
{
	if(pos==-1){
		return sta==3;
	}
	if(!limit&&dp[pos][sta]!=-1) 
	return dp[pos][sta];
	int up=limit?a[pos]:9;
	ll tmp=0;
	for(int i=0;i<=up;i++)
	{
	 if(sta==3)
	 tmp+=dfs(pos-1,sta,limit&&i==up);
	 else if(i==6)
	 tmp+=dfs(pos-1,sta+1,limit&&i==up);
	 else
	 tmp+=dfs(pos-1,0,limit&&i==up);
	}
	if(!limit) 
	dp[pos][sta]=tmp;
	return tmp; 
}
 
ll solve(ll x)
{
	ll t=x;
    int pos=0;
    while(x)
    {
        a[pos++]=x%10;
        x/=10;
    }
    return dfs(pos-1,0,true);
    
}
int main()
{
	
    memset(dp,-1,sizeof(dp));
    int T;
    cin>>T;
    for(int i=1;i<=T;i++)
    {
	ll n,m;
	scanf("%lld",&n);
	ll l=666,r=1e18,mid;
	while(l<=r)
	{
	
	 mid=(l+r)/2;
	 if(solve(mid)>=n)
	 	r=mid-1;
	 else
	 	l=mid+1;
	 	
	}
	printf("%lld\n",l);
    }

    return 0;
}

 


http://www.niftyadmin.cn/n/3628382.html

相关文章

emacs使用笔记

C-h t tutorial [移动基本操作]C-f C-b C-p C-n 前后上下 C-v C-a 行首 C-e行尾C-a 和 C-e 可以将光标移动到"一行"的头部和尾部。M-a 和 M-e 则将光标移动到“一句”的头部和尾部。M-f 向后移动一个单词 M-b 向前移动一个单词C-k 删除 C-v 下一页 M-v 上一页M-< …

red hat Linux配置ip

初次接触Linux &#xff0c;最开始的需要就是上网。配置IP成了学习使用Linux的第一个功课。 1.设置IP地址 查看你使用的IP网卡 ifconfig -a 找到你要使用的网卡 vi/etc/sysconfig/network-scripts/ifcfg-eth0 #IntelCorporation82801BA/BAM/CA/CAMEthernetController DEVICEet…

nyist 14 会场安排问题

以结束时间排序&#xff0c;一开始用了先按照开始时间排序&#xff0c;开始时间相同的按结束时间排&#xff0c;很荣幸的WA了&#xff0c;沉思了一会&#xff0c;用了以结束时间来排序&#xff0c;提交 AC了&#xff0c; 纠结了一会儿&#xff0c;这个。。。。。。。。。。 若以…

2018年9月16号 开学训练日志

昨天没有网络赛就忘了写训练日志。 刚把一道题看明白&#xff0c;昨天下午看了一下午&#xff0c;今天又看了一早上&#xff0c;现在我要按我自己的对数位DP的理解去调试代码&#xff0c;又要开始疯狂的写bug了。

WinForm 之 自定义标题栏的窗体移动

通过标题栏的鼠标事件实现窗体移动&#xff0c;代码如下&#xff1a; bool m_isMouseDown false; //窗体是否移动Point m_mousePos; //记录窗体的位置/// <summary>/// 鼠标按下&#xff0c;开启移动/// </summary>/// <param name"sender"></…

第15周实践项目1——程序填空

/* *Copyright (c) 2016,烟台大学计算机学院 *All rights reserved. *文件名称 : *作 者 : 刘云 *完成日期 : 2016年6月7号 *版 本 号 : v6.0 * *问题描述 : 请填空将程序补充完整。 *输入描述 :无 *程序输出 : */#include <iostream> #include <vector> #include …

面试题 链表中的若干问题

一次遍历找链表倒数第n个节点通过一次遍历找到单链表中倒数第n个节点&#xff0c;链表可能相当大&#xff0c;可使用辅助空间&#xff0c;但是辅助空间的数目必须固定&#xff0c;不能和n有关。不管是顺数n个还是倒数n个&#xff0c;其实都是距离-标尺问题。标尺是一段距离可以…

opera12.10中文版打开dragonfly

从网上看到opera有dragonfly这样一个调试工具&#xff0c;功能相当强大&#xff0c;还可远程调试手机上的opera mobile&#xff0c;就想试试。我的PC上的opera是12.10中文版&#xff0c;哪知找了半天也没发现怎么打开dragonfly的界面-_-!&#xff0c;在网上看有人说是menu --&g…