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 ...
随机推荐
- hdoj薛猫猫杯程序设计网络赛1003 球球大作战
思路: 二分,check函数不是很好写. 实现: 1. #include <bits/stdc++.h> using namespace std; typedef long long ll ...
- HDU4576 Robot(概率)
题意 抄袭自https://www.cnblogs.com/Paul-Guderian/p/7624039.html 多组输入n,m,l,r.表示在一个环上有n个格子.接下来输入m个w表示连续的一段 ...
- MFC命令行及CCommandLineInfo类
获取命令行的方法: 1.GetCommandLine() 获取输入的所有信息,包括程序所在路径及参数 2.AfxGetApp()->m_lpCmdLine 只包含参数 一般情况下,获取到命令行后 ...
- 解决更新到os x10.11后openssl头文件无法找到的问题
os x从10.10更新到10.11后,原有代码编译报错,#include <openssl/ssl.h>等头文件无法找到: "openssl/ssl.h: No such fi ...
- webpack前端构建工具学习总结(四)之自动化生成项目中的html页面
接续上文:webpack前端构建工具学习总结(三)之webpack.config.js配置文件 插件的介绍文档:https://www.npmjs.com/package/html-webpack-p ...
- (一)maven之创建一个maven项目
为什么要使用Maven? 1. maven使用的是本地仓库存储jar,所有项目都会共用仓库中的同一份jar. 2. Spring core.jar必须同时引用版本兼容的common-logging ...
- docker配置国内镜像
1. 配置 root@ros-OptiPlex-3050:~# cat /etc/docker/daemon.json { "graph": "/mnt/docke ...
- ssh复制remote
rsync rsync localdirectory username@10.211.55.4:/home/username/Downloads/localdirectory -r
- htmlunit爬取js异步加载后的页面
直接上代码: 一. index.html 调用后台请求获取content中的内容. <html> <head> <script type="text/javas ...
- C# Process.Start方法
System.Diagnostics.Process.Start(); 主要功能: 1.打开某个链接网址(弹窗). 2.定位打开某个文件目录. 3.打开系统特殊文件夹,如“控制面板”等. Proces ...