hdu 3943
数位dp
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <vector>
#include <sstream>
#include <cstring>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <iostream>
#define maxn 105
#define INF 0x3f3f3f3f
#define inf 10000000
#define MOD 100000000
#define ULL unsigned long long
#define LL long long
#define _setm(houge) memset(houge, INF, sizeof(houge))
#define _clear(houge) memset(houge, 0, sizeof(houge))
using namespace std; LL dp[25][25][25], p, q;
int dig[25], x, y; void init() {
_clear(dp);
dp[0][0][0] = 1;
for(int i = 1; i <= 20; ++ i) {
for(int j = 0; j <= i; ++ j) {
for(int k = 0; k+j <= i; ++ k) {
dp[i][j][k+1] += dp[i-1][j][k];
dp[i][j+1][k] += dp[i-1][j][k];
dp[i][j][k] += dp[i-1][j][k]*8;
}
}
}
} int getdig(LL nx) {
_clear(dig);
int cnt = 0;
while(nx) {
dig[++cnt] = nx%10;
nx /= 10;
}
return cnt;
} LL countid(LL nx) {
int cnt = getdig(nx);
LL ans = 0;
int cx = x, cy = y;
for(int i = cnt; i > 0; -- i) {
for(int j = 0; j < dig[i]; ++ j) {
if(j == 4 && cx) ans += dp[i-1][cx-1][cy];
else if(j == 7 && cy) ans += dp[i-1][cx][cy-1];
else if(j != 4 && j != 7) ans += dp[i-1][cx][cy];
}
if(dig[i] == 4) cx --;
if(dig[i] == 7) cy --;
if(cx < 0 || cy < 0) break;
}
return ans;
} LL findd(LL k) {
int len = 1;
while(!(dp[len-1][x][y] < k && dp[len][x][y] >= k)) ++ len;
long long res = 0;
int cx = x, cy = y;
for(int i = len; i > 0; -- i)
for(int j = 0; j < 10; ++ j) {
int tx = cx, ty = cy;
if(j == 4) {
-- tx;
if(tx < 0)
continue;
}
if(j == 7) {
-- ty;
if(ty < 0)
continue;
}
if(dp[i-1][tx][ty] >= k) {
res = res*10+j;
cx = tx;
cy = ty;
break;
}
k -= dp[i-1][tx][ty];
}
return res;
} int main()
{
int t, ca = 0;
init();
scanf("%d", &t);
while(t --) {
scanf("%I64d%I64d%d%d", &p, &q, &x, &y);
LL a = countid(p+1), b = countid(q+1);
int n;
scanf("%d", &n);
printf("Case #%d:\n", ++ ca);
while(n --) {
LL k;
scanf("%I64d", &k);
if(k > b-a) puts("Nya!");
else printf("%I64d\n", findd(k+a));
}
}
return 0;
}
hdu 3943的更多相关文章
- HDU 3943 K-th Nya Number(数位DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3943 题目大意:求出区间 (P,Q] 中找到第K个满足条件的数,条件是该数包含X个4和Y个7 Samp ...
- HDU 3943 K-th Nya Number
K-th Nya Number Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on HDU. Origina ...
- HDU 3943 数位dp+二分
K-th Nya Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 125536/65536 K (Java/Others) ...
- hdu 3943 经典数位dp好题
/* 题意:求出p-q的第j个nya数 数位dp,求出p-q的所有nya数的个数很好求,但是询问求出最终那个第j个值时是我不会求了看了下别人的思路 具体就是把p-q的第j个转化成0-q的第low+j个 ...
- My_Plan part1 小结
数位DP AC十道题目以上 成就达成 八月份!三个月!想想就令人兴奋呢 开始写总结啦 貌似简单的数位DP只需要改改模板就可以啦 就按照我的做题顺序开始总结吧 先是学习了一发模板:http://www. ...
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
- hdu 4859 海岸线 Bestcoder Round 1
http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...
- HDU 4569 Special equations(取模)
Special equations Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
随机推荐
- ios项目中引用其他开源项目
1. 将开源项目的.xcodeproj拖入项目frameworks 2. Build Phases下 Links Binary With Libraries 引入.a文件.Target Depende ...
- 线程池ThreadPoolExecutor参数分析
概述 比如去火车站买票, 有7个(maximumPoolSize)售票窗口, 但只有3个(corePoolSize)窗口对外开放.那么对外开放的3个窗口称为核心线程数, 而最大线程数是7个窗口. 如果 ...
- 保密安全风险自评估单机版检查工具V1.0
下载链接:http://download.csdn.net/download/moremoretea1983/10273128
- (转)Spring的bean管理(注解方式)
http://blog.csdn.net/yerenyuan_pku/article/details/69663779 Spring的bean管理(注解方式) 注解:代码中的特殊标记,注解可以使用在类 ...
- ecpg - 嵌入的 SQL C 预处理器
SYNOPSIS ecpg [ option...] file... DESCRIPTION 描述 ecpg 是一个嵌入的用于C 语言的 SQL 预编译器. 它把嵌有 SQL 语句的 C 程序通过将 ...
- poptip 外面 放 input 使用 iview vue
外层套的是 <FormItem prop="name" label="姓名:"> <Input v-model="tFill.nam ...
- c++ vector容器遍历方式
#include <vector> #include <iostream> class Test { public: int a; int b; int c; Test() { ...
- python之int (整型)
用途: 用于计算和比较 整型的格式: 10203 123 3340 整型的运算: + - * / ** // % 整数的加: a = 10 b = 20 print(a + b) # 输出结果 30 ...
- DNS查询过程
DNS查询过程 假设www.abc.com的主机要查询www.xyz.abc.com的服务器ip地址. 知识点 1.hosts文件:以静态映射的方式提供IP地址与主机名的对照表,类似ARP表 2.域: ...
- [kuangbin带你飞]专题五 并查集
并查集的介绍可以看下https://www.cnblogs.com/jkzr/p/10290488.html A - Wireless Network POJ - 2236 An earthquake ...