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 ...
随机推荐
- C#入门笔记3 表达式及运算符
C#表达式 表达式,把变量.字面值与运算符组合起来,就创建表达式,是计算的基本构件.字面值:是源代码中键入的数字.字符串,表示指定类型的值.常用有几种:整数.实数.字符.字符串. 整数字面量[也能使用 ...
- 在Window上用cmd创建.htaccess文件
Windows 图形下不能直接建立空名字的文件,所以没法直接创建.htaccess文件,不过可以通过命令行创建: cd /path/to/your/dir/ type nul>.htaccess ...
- hihocoder1777 彩球
思路: 记录一下快速幂计算过程中爆long long的两种解决方法: 1. 使用__int128,这玩意本地编译不通过,提交OJ能AC. 实现: #include <bits/stdc++.h& ...
- this/super/static/final/匿名对象/继承/抽象类/访问权限修饰符
1.this关键字的作用 1)调用本类中的属性; 2)调用本类中的构造方法;且只能放首行,且必须留一个构造方法作为出口,即不能递归调用 3)表示当前对象; 2.匿名对象 ...
- github上ReadMe语法
大标题 =================================== 大标题一般显示工程名,类似html的\<h1\><br /> 你只要在标题下面跟上=====即可 ...
- UVA 427 The Tower of Babylon 巴比伦塔(dp)
据说是DAG的dp,可用spfa来做,松弛操作改成变长.注意状态的表示. 影响决策的只有顶部的尺寸,因为尺寸可能很大,所以用立方体的编号和高的编号来表示,然后向尺寸更小的转移就行了. #include ...
- 集成iAd广告
在iPhone程序中集成广告,管他能不能赚钱,不放上一个iAd就心有不甘. 参考了下面这篇文章: http://bees4honey.com/blog/tutorial/how-to-add-iad- ...
- jsc 解码窥探
先使用 JS_DecodeScript反编译jsc 得到AST树 AST树词法解析 http://esprima.org/ AST还原成源码: npm install escodegen AST树遍 ...
- 测试框架 Mocha 实例教程(转载:来自阮一峰的一篇文章)
Mocha(发音"摩卡")诞生于2011年,是现在最流行的JavaScript测试框架之一,在浏览器和Node环境都可以使用. 所谓"测试框架",就是运行测试的 ...
- OpenCV2:第十一章 图像转换
一.简介 二.例子 #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #inclu ...