【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的最大子 ...
随机推荐
- linux shell wc 命令
1. 语法与选项 Short Option Long Option Option Description -c –bytes print the byte counts -m –chars print ...
- 路E施工管理ERP系统
前 景 目前公路工程由于点多.线长.面广.周期久.投资大等原因,管理很难到位,施工过程中存在着大量问题: 规章制度欠缺或不健全,即便是有好的规章制度,在施工过程中也往往形同虚设,不能与现场施 ...
- less编码规范
Less 编码规范 简介 因为自己最近写css用的比较多还是less,整理了一份less规范, 代码组织 代码按如下形式按顺序组织: @import 变量声明 样式声明 // ✓ @import &q ...
- Gradle插件
1.方法数统计 classpath 'com.getkeepsafe.dexcount:dexcount-gradle-plugin:0.6.1' apply plugin: 'com.getkeep ...
- JavaScript中一些你不一定知道的问题(持续更新中。。。。)
一些js的问题与解析 1) ["1","2","3"].map(parseInt);的运行结果是? A.["1",&qu ...
- (转)Spring读书笔记-----Spring的Bean之Bean的基本概念
从前面我们知道Spring其实就是一个大型的工厂,而Spring容器中的Bean就是该工厂的产品.对于Spring容器能够生产那些产品,则取决于配置文件中配置. 对于我们而言,我们使用Spring框架 ...
- 在VS中关于MySQL的相关问题
最近在vs上折腾mysql数据库 遇到了一些小问题,这里记录一下 问题一:数据源选择中没有mysql数据库的选项 解放方法: 1.安装MySql的VS插件(版本请下载最新版)mysql-for-vis ...
- Android基础问题汇总
一.android:gravity 和android:layout_gravity的区别: android;gravity是自己的内容相对于自己的控件的位置,而android:layout_gravi ...
- SQL小细节
平时有些小细节,不留意的话很容易得到错误的答案,我们来看下下面的代码,看看你是否能答对呢? ) ,) SELECT @str = '中国CH',@info='MyTest' SELECT [字符串]= ...
- Maven配置jar(war)包自动打包上传Maven服务器的配置
Maven配置jar(war)包自动打包上传Maven服务器的配置 创建jar(war)包工程 创建一个maven工程 在工程中穿件一个测试类 配置pom.xml <distributionMa ...