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 ...
随机推荐
- chrome调试之Workspaces
可通过workspaces来编辑本地文件 workspaces是Chrome DevTools的一个强大功能,这使DevTools变成了一个真正的IDE.Workspaces会将Sources选项卡中 ...
- IOS代码收集
http://mobile.51cto.com/hot-410417.htm 退回输入键盘: - (BOOL) textFieldShouldReturn:(id)textField{ [textFi ...
- Java递归调用改成非递归
在java语言中,使用递归调用时,如果过多的调用容易造成java.lang.StackOverflowError即栈溢出和程序执行过慢.这是一个潜在Bug和影响程序执行效率问题,需要谨慎使 ...
- 纯css滚动公告栏目
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Codeforces GYM 100741A . Queries
time limit per test 0.25 seconds memory limit per test 64 megabytes input standard input output stan ...
- DBMS的工作模式
数据库管理系统(DBMS)是指数据库系统中对数据进行管理的软件系统,它是数据库系统的核心组成部分,对数据库的一切操作(增删改查)都是通过DBMS进行的 DBMS的工作模式如下: 1>接受应用程序 ...
- UGUI世界坐标转换为UI本地坐标
以下是实现hud跟随3D物体的脚本,只是测试用,不是开发中的代码,脚本挂在任意游戏物体上 demo下载 using UnityEngine; public class SceneFollowUI : ...
- vue props 下有验证器 validator 验证数据返回true false后,false给default值
vue props 下有验证器 validator 验证数据返回true false后,false给default值 props: { type: { validator (value) { retu ...
- python基础一 day2 数据类型
int: bool: 类型转换: str到int有条件,str必须是数字, "123e"是错误的 bool转换为int类型,需要int(x) 结果: 结果: 空字 ...
- 【转】Delphi 2010 Lite加装帮助文件
基于爱好,下载了一个delphi 2010 lite,业余玩玩. 不过这东西是网友重新打包的,没有带帮助.在网上搜索一下加摸索后搞定.步骤如下: Delphi 2010本身的帮助(MSDN风格的)1. ...