【POJ3208】 (DP)
Apocalypse SomedayDescription
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
187Sample Output
1666
2666
66666
【题意】
询问第k大含“666”的数。
【分析】
DP,考虑下面填数方案:

反正我就用这种笨方法了~~
代码如下:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define INF 0xfffffff
#define LL long long LL f[],d[]; void pri(int x,LL y)
{
int z=;
LL yy=y;
while(yy) z++,yy/=;
for(int i=;i<=x-z;i++) printf("");
if(y!=) printf("%I64d",y);
} int main()
{
freopen("a.in","r",stdin);
freopen("a.out","w",stdout);
int T;
scanf("%d",&T);
while(T--)
{
int n;
scanf("%d",&n);
f[]=;f[]=f[]=;
d[]=;d[]=;
for(int i=;i<=;i++) d[i]=d[i-]*;
for(int i=;i<=;i++)
{
f[i]=d[i-]+(f[i-]+f[i-]+f[i-])*;
}
for(int i=;i<=;i++) if(f[i]>=n&&f[i-]<n)
{
LL now=n-f[i-];//减掉开头为0
for(int j=i;j>=;j--)//枚举填数
{
for(int k=;k<=;k++)//填1~9
{
if(j==i&&k==) continue;
LL x;
if(k==) x=f[j-]*+f[j-]*+d[j-];
else x=f[j-];
if(now>x) now-=x;
else
{
if(k==)
{
LL y;
if(now<=*f[j-])
{
y=(now-)/f[j-];
now=now%(f[j-]);now=now==?f[j-]:now;
printf("6%I64d",y);j--;break;
}now-=*f[j-];
if(now<=*f[j-])
{
y=(now-)/f[j-];
now=now%(f[j-]);now=now==?f[j-]:now;
printf("66%I64d",y);j-=;break;
}now-=*f[j-];
if(now<=d[j-])
{
printf("",now);
if(j!=) pri(j-,now-);
j=;break;
}now-=d[j-];
if(now<=*f[j-])
{
y=(now-)/f[j-];
now=now%(f[j-]);now=now==?f[j-]:now;
printf("66%I64d",y+);j-=;break;
}now-=*f[j-];
y=(now-)/f[j-];
now=now%(f[j-]);now=now==?f[j-]:now;
printf("6%I64d",y+);j--;break;
}
else printf("%d",k);
break;
}
}
}
break;
}
printf("\n");
}
return ;
}
[POJ3208]
2016-07-17 10:03:41
【POJ3208】 (DP)的更多相关文章
- 【STSRM10】dp只会看规律
[算法]区间DP [题意]平面上有n个点(xi,yi),用最少个数的底边在x轴上且面积为S的矩形覆盖这些点(在边界上也算覆盖),n<=100. [题解]随机大数据下,贪心几乎没有错误,贪心出奇迹 ...
- CS academy Growing Trees【模板】DP求树的直径
[题意概述] 给出一棵树,树上的边有两个值a和b,你可以在[0,limit]范围内选择一个整数delta,树上的边的权值为a+b*delta,现在问当delta为多少的时候树的直径最小.最小直径是多少 ...
- 【USACO】DP动态规划小测(一)
{20160927 19:30~21:30} 总分400分,我113.33,稳稳地垫底了......(十分呼应我上面的博客名,hhh~)过了这么多天我才打完所有代码,废话我也就不多说了.不过,虽然时间 ...
- 【专章】dp基础
知识储备:dp入门. 好了,完成了dp入门,我们可以做一些稍微不是那么裸的题了. ----------------------------------------------------------- ...
- 【专章】dp入门
动态规划(简称dp),可以说是各种程序设计中遇到的第一个坎吧,这篇博文是我对dp的一点点理解,希望可以帮助更多人dp入门. ***实践是检验真理的唯一标准,看再多文章不如自己动手做几道!!!*** 先 ...
- 【算法】DP解决旅行路径问题
问题描述 : After coding so many days,Mr Acmer wants to have a good rest.So travelling is the best choice ...
- 【POJ3208】Apocalypse Someday
Description 666号被认为是神秘的"野兽之数",在所有以启示录为主题的大片中都是一个被广泛使用的数字.但是,这个数字666不能总是在脚本中使用,所以应该使用1666这样 ...
- 【BZOJ1084】dp
题目很简单 分析蛮无聊的一道题.状态转移十分显然然后就做完了. #include <bits/stdc++.h>#define sc(n) scanf("%d",&am ...
- 【基础】dp系列1
序列双段最大子段和问题 (也许很水但蒟蒻刚刚学dp就来记录一下) 题目链接 题意就是求序列中的任意两段的最大子段和最大. 我们先预处理出来前缀和,方便求最大子段和. 对于每一个i都求一遍1到i的最大子 ...
随机推荐
- 项目FAQ
报错: Conversion from String Literal to Char* is deprecated http://stackoverflow.com/questions/1369030 ...
- hdu2047.java递推题
阿牛的EOF牛肉串 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total S ...
- VS2010无法打开CSS问题
安装了VS2010的SP1补丁后,发现打开css文件时出现下面问题: 一点击css文件就弹出:未能完成操作.未指定的错误.无法正常进入. [解决方法]安装最新Web Standards Update补 ...
- javascript-图片横向无缝隙滚动(可在服务器运行)
前两次弄'图片横向滚动'javascript,在本地上运行得很美,可是一上到我们学校后台的服务器,就有很多问题,这个算是行的了. css代码: <style type="text/cs ...
- Windows下的进程【一】
什么是进程?进程就是一个正在运行的程序的实例,由两部分组成: 内核对象.操作系统用内核对象对进程进行管理,内核对象是操作系统保存进程统计信息的地方. 地址空间.其中包含所有可执行文件或DLL模块的代码 ...
- oracle session 相关优化
导读: 同学们是不是都用遇到过这种情况,一个业务系统开发期业务并发量只是估算一个值,而系统上线后这个并发量可能会出现溢出或是不够的 情况.在这种情况下我们DBA怎么给出合理的性能优化建议呢?本文就 ...
- 2.Oracle11g体系结构
2.1逻辑存储结构 2.1.1数据块(Data Blocks) 数据块是Oracle逻辑结构中最小的逻辑单位,也是执行数据库输入输出最小的存储单位.通常Oracle数据库是操作系统块的整数倍,如果操作 ...
- ios 字符串替换方法
string=[string stringByReplacingOccurrencesOfString:@"-"withString:@"/"];
- libConfuse的使用
前言 在软件编程中经常会使用到一些参数,在存储方面一般有使用XML的,也有使用文本文件的,或者使用数据库的等.我们软件平台一些参数是使用XML文件存储,在读取方面的速度还可以,但在写回文件速度一般.也 ...
- 对N个数组进行操作。先把这N个一维数组合并成一个2为数组;然后进行操作
using System;using System.Collections.Generic;using System.Linq;using System.Collections;using Syste ...