SNIBB

Time Limit: 1000ms
Memory Limit: 32768KB

This problem will be judged on HDU. Original ID: 3271
64-bit integer IO format: %I64d      Java class name: Main

 
  As we know, some numbers have interesting property. For example, any even number has the property that could be divided by 2. However, this is too simple. 
  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

  There are no more than 30 cases.
  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

  Output contains two lines for each cases.
  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
Hint

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

 
解题:数位dp + 二分
 #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的更多相关文章

  1. hdu 3271 SNIBB 数位DP+二分

    思路:dp[i][j]:表示第i位在B进制下数字和. 用二分找第k个数! 代码如下: #include<iostream> #include<stdio.h> #include ...

  2. [数字dp] hdu 3271 SNIBB

    意甲冠军:有两个查询: q=1.在[x,y]间隔,兑换b十进制,数字和m多少个月. q=2.在[x,y]间隔,兑换b十进制,数字是m第一k的数目是多少(十进制),没有输出由给定的主题. 思维: 和比特 ...

  3. HDU 3271 数位dp+二分

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

  4. [DP]数位DP总结

     数位DP总结 By Wine93 2013.7 1.学习链接 [数位DP] Step by Step   http://blog.csdn.net/dslovemz/article/details/ ...

  5. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  6. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  7. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

  8. HDU 4569 Special equations(取模)

    Special equations Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  9. HDU 4006The kth great number(K大数 +小顶堆)

    The kth great number Time Limit:1000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64 ...

随机推荐

  1. cookie和session基础以及在Django中应用

    看了会视频,终于搞懂了~ 1.cookie cookie:保存状态 cookie的工作原理是:由服务器产生内容,浏览器收到请求后保存在本地:当浏览器再次访问时,浏览器会自动带上cookie,这样服务器 ...

  2. LPCTSTR和CString的关系

    类型理解 LPCTSTR类型: L表示long指针 这是为了兼容Windows 3.1等16位操作系统遗留下来的,在win32中以及其他的32位操作系统中, long指针和near指针及far修饰符都 ...

  3. 深入浅出Android动态加载jar包技术

    在实际项目中,由于某些业务频繁变更而导致频繁升级客户端的弊病会造成较差的用户体验,而这也恰是Web App的优势,于是便衍生了一种思路,将核心的易于变更的业务封装在jar包里然后通过网络下载下来,再由 ...

  4. mysql> set sql_mode='no_auto_value_on_zero';

    mysql> set sql_mode='no_auto_value_on_zero';

  5. Spring下读取properties文件

    由于在spring的xml文件中配置了 <bean id="validator" class="org.springframework.validation.bea ...

  6. 使用javap深入理解Java整型常量和整型变量的区别

    我下图代码第五行和第九行分别定义了一个整型变量和一个整型常量: static final int number1 = 512; static int number3 = 545; Java程序员都知道 ...

  7. 关于自动化测试环境的集成(Jenkins+RobotFramework+TestLink+SVN)

    本人主要从事网络安全产品的测试,由于一些产品功能在后期稳定后每个版本的迭代仍需要投入大量的时间和精力去测试,所以近期计划逐步的去了解自动化测试的一些内容来节省和解放一些资源.由于自己并没有什么编码基础 ...

  8. Oracle错误(包括PL/SQL)集合与修复

    +-----------------------------------------------------------------------+ |   在本篇随笔中,仅根据个人经验累积错误进行描述 ...

  9. (转)SpringMVC学习(四)——Spring、MyBatis和SpringMVC的整合

    http://blog.csdn.net/yerenyuan_pku/article/details/72231763 之前我整合了Spring和MyBatis这两个框架,不会的可以看我的文章MyBa ...

  10. javaEE(16)_Servlet监听器

    一.监听器原理 1.监听器就是一个实现特定接口的普通java程序,这个程序专门用于监听一个java对象的方法调用或属性改变,当被监听对象发生上述事件后,监听器某个方法将立即被执行. 2.监听器典型案例 ...