HDU 3943 K-th Nya Number(数位DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3943
题目大意:求出区间 (P,Q] 中找到第K个满足条件的数,条件是该数包含X个4和Y个7
分析一:
先预处理dp[i][j][k]表示第 i 位有 j 个4和 k 个7的数量。之后就可以通过高位开始枚举,求出区间内有多少个规定的数,如果询问大于总数,则输出“Nya!”
之后找第k大的数:首先可以确定出位数,dp[i][x][y]表示 i 位时的满足数,那么大于dp[len-1][x][y],而小于dp[len][x][y],len表示目标位数。
确认了位数之后,依然从高位开始。比如说高位首先是0,而dp[len-1][x][y]小于k,说明0开头的目标小于所求,继续往后找,把之前的删掉
有点意思
代码如下:
# include<iostream>
# include<cstdio>
# include<cstring>
# define LL __int64
using namespace std; LL dp[][][]; //dp[i][j][k]表示i位的数,有j个4,k个7的数量
LL l,r;
int x,y; void init()
{
int i,j,k;
memset(dp,,sizeof(dp));
dp[][][] = ;
for(i=; i<=; i++)
for(j=; j<i; j++)
for(k=; k+j<i; k++)
{
dp[i][j][k+] += dp[i-][j][k];
dp[i][j+][k] += dp[i-][j][k];
dp[i][j][k] += dp[i-][j][k]*; //在高位上加除4、7以外的8个数字
}
} LL get_count(LL n)
{
int bit[],len=;
while(n)
{
bit[++len] = n%;
n /= ;
}
LL ans = ;
int cx=x, cy=y;
for(int i=len; i; i--) //从高位开始枚举
{
for(int j=; j<bit[i]; j++)
if(j==)
{
if(cx)
ans += dp[i-][cx-][cy];
}
else if(j==)
{
if(cy)
ans += dp[i-][cx][cy-];
}
else
ans += dp[i-][cx][cy];
if(bit[i]==)
cx--;
if(bit[i]==)
cy--;
//如果高位出现的4、7的数量已经超过所求,则退出
if(cx< || cy<)
break;
}
return ans;
} LL solve(LL k)
{
int len = ;
while()
{
//找到目标长度
if(dp[len-][x][y]<k && dp[len][x][y]>=k)
break;
len++;
}
LL ret = ;
int cx=x, cy=y;
for(int i=len; i; i--)
{
//从高位开始枚举
for(int j=; j<; j++)
{
int tx = cx,ty=cy;
if(j==)
{
tx--;
if(tx<)
continue;
}
if(j==)
{
ty--;
if(ty<)
continue;
}
if(dp[i-][tx][ty] >= k)
{
ret = ret*+j;
cx=tx;
cy=ty;
break;
}
k -= dp[i-][tx][ty];
}
}
return ret;
} int main()
{
init();
int T,cas;
scanf("%d",&T);
for(cas=; cas<=T; cas++)
{
scanf("%I64d%I64d%d%d",&l,&r,&x,&y);
LL a=get_count(r+);
LL b=get_count(l+); //注意左开区间
int n;
scanf("%d",&n);
printf("Case #%d:\n",cas);
while(n--)
{
LL k;
scanf("%I64d",&k);
if(k > a-b)
puts("Nya!");
else
printf("%I64d\n",solve(k+b));
}
}
return ;
}
分析二:
将solve() 函数改成二分写法,答案更简洁
LL solve(LL k)
{
LL mid,ans=-,ret,left=l,right=r;
while(left<=right)
{
mid = (left+right)>>;
ret = get_count(mid+);
if(ret>=k)
{
ans =mid;
right = mid-;
}
else
left=mid + ;
}
return ans;
}
HDU 3943 K-th Nya Number(数位DP)的更多相关文章
- 多校5 HDU5787 K-wolf Number 数位DP
// 多校5 HDU5787 K-wolf Number 数位DP // dp[pos][a][b][c][d][f] 当前在pos,前四个数分别是a b c d // f 用作标记,当现在枚举的数小 ...
- hdu 5898 odd-even number 数位DP
传送门:hdu 5898 odd-even number 思路:数位DP,套着数位DP的模板搞一发就可以了不过要注意前导0的处理,dp[pos][pre][status][ze] pos:当前处理的位 ...
- HDU 5787 K-wolf Number 数位DP
K-wolf Number Problem Description Alice thinks an integer x is a K-wolf number, if every K adjacen ...
- HDU 3709 Balanced Number (数位DP)
Balanced Number Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) ...
- HDU 5179 beautiful number 数位dp
题目链接: hdu: http://acm.hdu.edu.cn/showproblem.php?pid=5179 bc(中文): http://bestcoder.hdu.edu.cn/contes ...
- hdu 5898 odd-even number(数位dp)
Problem Description For a number,if the length of continuous odd digits is even and the length of co ...
- HDU 5898 odd-even number (数位DP) -2016 ICPC沈阳赛区网络赛
题目链接 题意:一个数字,它每个数位上的奇数都形成偶数长度的段,偶数位都形成奇数长度的段他就是好的.问[L , R]的好数个数. 题解:裸的数位dp, 从高到低考虑每个数位, 状态里存下到当前位为止的 ...
- codeforces Hill Number 数位dp
http://www.codeforces.com/gym/100827/attachments Hill Number Time Limits: 5000 MS Memory Limits: ...
- beautiful number 数位DP codeforces 55D
题目链接: http://codeforces.com/problemset/problem/55/D 数位DP 题目描述: 一个数能被它每位上的数字整除(0除外),那么它就是beautiful nu ...
- Fzu2109 Mountain Number 数位dp
Accept: 189 Submit: 461Time Limit: 1000 mSec Memory Limit : 32768 KB Problem Description One ...
随机推荐
- JavaScript的this简单实用
1.默认绑定全局变量,在全局函数中: function fn(){ console.log(this.a); } var a=2; fn();//这里调用的是window 2.隐式绑定: functi ...
- PHP魔术变量总结
php手册里面的解释 __FUNCTION__ returns only the name of the function 返回的仅仅是函数的名称 __METHOD__ returns t ...
- Asp.Net事务和异常处理:
Asp.Net事务和异常处理: 一.什么是事务处理? 事务处理是一组组和成逻辑工作单元的数据库操作,虽然系统中可能会出错,但事务将控制和维护每个数据库的一致性和完整性. 如果在事务过程中没有遇到错误, ...
- iOS相机权限、相册权限、定位权限判断
1.判断用户是否有权限访问相册 #import <AssetsLibrary/AssetsLibrary.h> ALAuthorizationStatus author = [ALAsse ...
- Linux curl命令详解
用途说明 curl命令是一个功能强大的网络工具,它能够通过http.ftp等方式下载文件,也能够上传文件.其实curl远不止前面所说的那些功能,大家可以通过man curl阅读手册页获取更多的信息.类 ...
- MySQL如何选择float, double, decimal
http://yongxiong.leanote.com/post/mysql_float_double_decimal
- 实例源码--Android智能家居系统源码
下载源码 技术要点: 1.Android应 用开发基础框架 2.SQLITE数据库的 使用 3.网络通信 4.GOOGLE地图模块 5.源码带有非常详 细的中文注释 ...... 详细介绍: ...
- java_泛型,设置类型通配符的上限
package ming; import java.util.ArrayList; import java.util.Collection; import java.util.List; class ...
- Controllers
Controllers Controllers are the bread and butter of the framework they control when a model is used ...
- Internationalization
Internationalization If you are building a site for an international audience, you will likely want ...