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 ...
随机推荐
- laravel学习笔记(三)
模型传值 路由: Route::get('/posts/{post}','\App\Http\Controllers\PostController@show'); 方法: public functio ...
- html文本溢出显示省略字符的两种常用方法
方法一:使用CSS溢出省略的方式解决 解决效果如下: css代码: display: -webkit-box; display: -moz-box; white-space: pre-wrap; wo ...
- Python3简明教程(六)—— 数据结构
简单的来说,数据结构(data structure)是计算机中存储.组织数据的方式.比如我们之前使用过的列表就是一种数据结构,在这里我们还会深入学习它.之前也有简单的介绍. 列表 >>&g ...
- jquery命名冲突
nodeName是jquery的关键字
- SSM框架删除/更新时返回影响条数
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer"> < ...
- vector性能调优之resize与reserve
vector的resize与reserve reserve()函数为当前vector预留至少共容纳size个元素的空间.(译注:实际空间可能大于size) resize() 函数( void resi ...
- SCOI2013 数数
题目描述 题解: 很玄学的一道数位$dp$,看了很多篇题解才懂. 直接挂$l$的题解. 代码: #include<cstdio> #include<cstring> #incl ...
- python计算圆面积
#coding=gbk #coding=utf-8 #-*- coding: UTF-8 -*- #调用math包处理相关的运算 import math #圆半径 r = 2 #计算圆面积π*r*r与 ...
- 如何用纯 CSS 创作一种有削铁如泥感觉的菜单导航特效
效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/XqYroe 可交互视频教 ...
- 开发语言之---Python
Python,如果你想进军AI,或是不想被自动化运维淘汰,Python是一门必须课. 在未来的大学课堂上,也许也会将Python加入必修中,就像Java一样. Python之“Hello World” ...