K-th Nya Number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others)
Total Submission(s): 3155    Accepted Submission(s): 1021

Problem Description
Arcueid likes nya number very much.
A nya number is the number which has exactly X fours and Y sevens(If X=2 and Y=3 , 172441277 and 47770142 are nya numbers.But 14777 is not a nya number ,because it has only 1 four).
Now, Arcueid wants to know the K-th nya number which is greater than P and not greater than Q.
 
Input
The first line contains a positive integer T (T<=100), indicates there are T test cases.
The second line contains 4 non-negative integers: P,Q,X and Y separated by spaces.
( 0<=X+Y<=20 , 0< P<=Q <2^63)
The third line contains an integer N(1<=N<=100).
Then here comes N queries.
Each of them contains an integer K_i (0<K_i <2^63).
 
Output
For each test case, display its case number and then print N lines.
For each query, output a line contains an integer number, representing the K_i-th nya number in (P,Q].
If there is no such number,please output "Nya!"(without the quotes).
 
Sample Input
1
38 400 1 1
10
1
2
3
4
5
6
7
8
9
10
 
Sample Output
Case #1:
47
74
147
174
247
274
347
374
Nya!
Nya!
 
Author
hzhua

题意:

求区间内第k个有且仅有x个4和y个7的数

代码:

//注意细节
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
int t,x,y,bit[];
ll f[][][],P,Q;
ll dfs(int pos,int xx,int yy,int limit)
{
if(pos==) return (xx==&&yy==);
if(xx<||yy<) return ;
if(!limit&&f[pos][xx][yy]!=-) return f[pos][xx][yy];
int max_b=limit?bit[pos]:;
ll ans=;
for(int i=;i<=max_b;i++){
ans+=dfs(pos-,xx-(i==),yy-(i==),limit&&(i==max_b));
}
if(!limit) f[pos][xx][yy]=ans;
return ans;
}
ll solve(ll O)
{
int pos=;
while(O){
bit[++pos]=O%;
O/=;
}
return dfs(pos,x,y,);
}
int main()
{
//freopen("in.txt","r",stdin);
scanf("%d",&t);
for(int cas=;cas<=t;cas++){
printf("Case #%d:\n",cas);
memset(f,-,sizeof(f));
scanf("%lld%lld%d%d",&P,&Q,&x,&y);
ll tmp1=solve(P);
ll tmp2=solve(Q);
int n;ll k;
scanf("%d",&n);
while(n--){
scanf("%lld",&k);
if(tmp2-tmp1<k) { puts("Nya!");continue; }
ll l=P+,r=Q,ans;
while(l<=r){
ll mid=(l+r)>>;
ll tmp=solve(mid);
if(tmp-tmp1>=k) { ans=mid;r=mid-; }
else l=mid+;
}
printf("%lld\n",ans);
}
}
return ;
}

HDU 3943 数位dp+二分的更多相关文章

  1. HDU 3271 数位dp+二分

    SNIBB Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  2. hdu 4507 数位dp(求和,求平方和)

    http://acm.hdu.edu.cn/showproblem.php?pid=4507 Problem Description 单身! 依旧单身! 吉哥依旧单身! DS级码农吉哥依旧单身! 所以 ...

  3. hdu 4352 数位dp + 状态压缩

    XHXJ's LIS Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  4. poj3208 Apocalypse Someday 数位dp+二分 求第K(K <= 5*107)个有连续3个6的数。

    /** 题目:poj3208 Apocalypse Someday 链接:http://poj.org/problem?id=3208 题意:求第K(K <= 5*107)个有连续3个6的数. ...

  5. hihocoder #1301 : 筑地市场 数位dp+二分

    题目链接: http://hihocoder.com/problemset/problem/1301?sid=804672 题解: 二分答案,每次判断用数位dp做. #include<iostr ...

  6. 2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6156 数位DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6156 题意:如题. 解法:数位DP,暴力枚举进制之后,就转化成了求L,R区间的回文数的个数,这个直接做 ...

  7. UPC 2223: A-Number and B-Number(数位DP+二分)

    积累点: 1: (l&r)+((l^r)>>) == (l+r)/2 2: 注意判断现在是否有限制.当枚举下一个量时,是(isQuery && j==end),不要 ...

  8. CodeChef FAVNUM FavouriteNumbers(AC自动机+数位dp+二分答案)

    All submissions for this problem are available. Chef likes numbers and number theory, we all know th ...

  9. hdu:2089 ( 数位dp入门+模板)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2089 数位dp的模板题,统计一个区间内不含62的数字个数和不含4的数字个数,直接拿数位dp的板子敲就行 ...

随机推荐

  1. python中数据分析常用函数整理

    一. apply函数 作用:对 DataFrame 的某行/列应用函数之后,Apply 返回一些值.函数既可以使用默认的,也可以自定义.注意:在第二个输出中应用 head() 函数,因为它包含了很多行 ...

  2. 20135234mqy 实验四

    北京电子科技学院(BESTI) 实     验    报     告 课程:java程序设计 班级:1352  姓名:mqy  学号:20135234 成绩:            指导教师:娄嘉鹏  ...

  3. youi软件测试计划

    beta版本中,我们将重视软件开发中的测试. 我们的软件是需要测试的,不测试怎么知道好与不好呢?有的程序不测试甚至都不能运行-- 我们的目标呢:就是经过测试之后软件的质量得到有效的保证.不管什么情况都 ...

  4. java怎样把后台值传到前台

    后台代码:if(this.Request["type"]!=null){aaa=this.Request["type"].ToString(); try{bbb ...

  5. python处理xml实例

    """ Author = zyh FileName = read_xml_1.py Time = 18-9-26 下午5:19 """ fr ...

  6. Leetcode题库——7.反转整数

    @author: ZZQ @software: PyCharm @file: IntReverse.py @time: 2018/9/16 16:36 要求:整数反转(给定一个 32 位有符号整数,将 ...

  7. 学习Web Service、wcf、webapi的区别

    csdn:关于wcf,webservice,webapi或者其他服务或者接口有什么区别. wcf,webservice采用的是rpc协议,这个协议很复杂,所以每次要传递.要校验的内容也很复杂,别看我们 ...

  8. mongodb的命令介绍

    db.help() 查看库级别的命令 db.stats() 查看数据库状态 db.version() 查看数据库版本 db.serverStatus() 查看数据库服务器状态 db.mycoll.he ...

  9. https 的理解

    前言: 本篇博文来记录下对http及https的理解.(会有点 杂,补缺补漏) 引用:https://blog.csdn.net/u011109589/article/details/80306479 ...

  10. #Leetcode# 692. Top K Frequent Words

    https://leetcode.com/problems/top-k-frequent-words/ Given a non-empty list of words, return the k mo ...