HDU 3271 SNIBB
SNIBB
This problem will be judged on HDU. Original ID: 3271
64-bit integer IO format: %I64d Java class name: Main
One day our small HH finds some more interesting property of some numbers. He names it the “Special Numbers In Base B” (SNIBB). Small HH is very good at math, so he considers the numbers in Base B. In Base B, we could express any decimal numbers. Let’s define an expression which describe a number’s “SNIBB value”.(Note that all the “SNIBB value” is in Base 10)
Here N is a non-negative integer; B is the value of Base.
For example, the “SNIBB value” of “1023” in Base “2” is exactly:10
(As we know (1111111111)2=(1023)(10))
Now it is not so difficult to calculate the “SNIBB value” of the given N and B.
But small HH thinks that must be tedious if we just calculate it. So small HH give us some challenge. He would like to tell you B, the “SNIBB value” of N , and he wants you to do two kinds of operation:
1. What is the number of numbers (whose “SNIBB value” is exactly M) in the range [A,B];
2. What it the k-th number whose “SNIBB value” is exactly M in the range [A,B]; (note that the first one is 1-th but not 0-th)
Here M is given.
Input
For each case, there is one integer Q,which indicates the mode of operation;
If Q=1 then follows four integers X,Y,B,M, indicating the number is between X and Y, the value of base and the “SNIBB value”.
(0<=X,Y<=2000000000,2<=B<=64,0<=M<=300)
If Q=2 then follows five integers X,Y,B,M,K, the first four integer has the same meaning as above, K indicates small HH want to know the k-th number whose “SNIBB value” is exactly M.
(1<=K<=1000000000)
Output
The first line is the case number, the format is exactly “Case x:”, here x stands for the case index (start from 1.).
Then follows the answer.
If Q=2 and there is no such number in the range, just output “Could not find the Number!” (without quote!) in a single line.
Sample Input
1 0 10 10 3
2 0 10 10 1 2
1 0 10 2 1
Sample Output
Case 1:
1
Case 2:
10
Case 3:
4
In case 1, the number in the range [0,10] whose “SNIBB value” is exactly 3 is 3(in Base 10); In case 2, the numbers in the range [0,10] whose “SNIBB value” is exactly 1 are 1 and 10; Of course the 2-th number is 10. In case 3, the number in the range [0,10] whose “SNIBB value” is exactly 1 is 1,10,100,1000(in Base 2);
Source
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
int dp[][],bit[],op,x,y,b,m,k;
int dfs(int len,int sum,bool flag){
if(- == len) return sum == m;
if(!flag && dp[len][sum] != -) return dp[len][sum];
int ret = ,u = flag?bit[len]:(b - );
for(int i = ; i <= u; ++i)
ret += dfs(len - ,sum + i,flag && i == u);
if(!flag) dp[len][sum] = ret;
return ret;
}
int solve(int n){
if(n <= ) return n == m;
int len = ;
while(n){
bit[len++] = n%b;
n /= b;
}
return dfs(len - ,,true);
}
int main(){
int cs = ;
while(~scanf("%d%d%d%d%d",&op,&x,&y,&b,&m)){
memset(dp,-,sizeof dp);
if(x > y) swap(x,y);
int p = solve(x - ),q = solve(y);
printf("Case %d:\n",cs++);
if(op == ) printf("%d\n",q - p);
else{
scanf("%d",&k);
if(q - p < k){
puts("Could not find the Number!");
continue;
}
int low = x,high = y,ans;
while(low <= high){
int mid = (static_cast<LL>(low) + high)>>;
if(solve(mid) - p >= k){
ans = mid;
high = mid - ;
}else low = mid + ;
}
printf("%d\n",ans);
}
}
return ;
}
HDU 3271 SNIBB的更多相关文章
- hdu 3271 SNIBB 数位DP+二分
思路:dp[i][j]:表示第i位在B进制下数字和. 用二分找第k个数! 代码如下: #include<iostream> #include<stdio.h> #include ...
- [数字dp] hdu 3271 SNIBB
意甲冠军:有两个查询: q=1.在[x,y]间隔,兑换b十进制,数字和m多少个月. q=2.在[x,y]间隔,兑换b十进制,数字是m第一k的数目是多少(十进制),没有输出由给定的主题. 思维: 和比特 ...
- HDU 3271 数位dp+二分
SNIBB Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- [DP]数位DP总结
数位DP总结 By Wine93 2013.7 1.学习链接 [数位DP] Step by Step http://blog.csdn.net/dslovemz/article/details/ ...
- 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 ...
- HDU 4006The kth great number(K大数 +小顶堆)
The kth great number Time Limit:1000MS Memory Limit:65768KB 64bit IO Format:%I64d & %I64 ...
随机推荐
- Hibernate:Disjunction&Conjunction构造复杂的查询条件.
Hibernate:Disjunction&Conjunction构造复杂的查询条件 Disjunction和Conjunction是逻辑或和逻辑与,如下: 用来组合一组逻辑或[or]条件的方 ...
- Idea注释参数报错,控制台乱码问题解决方法
idea虽然工具非常好用,但是他的一些解决方法网上非常的少,有些压根没有,解决这些问题非常浪费时间 1.最近在工作中发现一个问题,使用ant打包后,控制台总是报错,提示信息还是乱码的,吓得我赶紧用回了 ...
- 源文件名长度大于系统支持的长度,无法删除,java主方法执行方式删除
import java.io.File; /** * @author 海盗船长 * 2017年2月14日11:24:26 */ public class DeleteFiles { public st ...
- POJ 2449 Remmarguts' Date
Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 30725 Accepted: 8389 Description &quo ...
- COGS 1578. 次小生成树初级练习题
☆ 输入文件:mst2.in 输出文件:mst2.out 简单对比时间限制:1 s 内存限制:256 MB [题目描述] 求严格次小生成树 [输入格式] 第一行包含两个整数N 和M,表 ...
- X11/extensions/XShm.h: No such file or directory
CentOS 编译一些开源项目提示:X11/extensions/XShm.h: No such file or directory. 运行命令:yum install libXext-devel就可 ...
- Fedora CentOS Red Hat中让vim支持语法高亮设置
Fedora / CentOS / Red Hat这三个系统里默认的vi是没有语法高亮显示的,白色的字体看起来很不舒服. 首先用命令行cat /etc/os-release查看当前linux系统的类型 ...
- javascript基本类型和引用类型,作用域和内存问题
基本类型(null.undefined.boolean.number.string)和引用类型(Object 对象) 1 基本类型:只能不存一个值,一种类型:从一个变量向另一个变量复制基本类型的值, ...
- shell补充知识点
一.cut(截取) 1.按字节截取(-b) 例:/etc/passwd文件截取 head -5 passwd | cut -b 1-4 ----->截取1-4的字节 head -5 passwd ...
- javaEE(13)_jdbc框架
一.使用模板方法设计模式简化开发 模板方法设计模式,执行一个程序有很多步骤,将每次都要执行的共有的提取出来放到一个抽象父类中,变化的部分通过让子类传递参数过来或将这部分抽象为抽象方法让子类通过继承的方 ...