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. Java基础50题test1—不死神兔

    [不死神兔] 题目:古典问题:有一对兔子,从出生后第 3 个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子对数为多少?  程序分析: 兔子的规律为数列 ...

  2. map中使用await 异步函数

    let result=await Promise.all(dataComments.map(async (ele)=>{ return (async ()=>{ let resData= ...

  3. 学习笔记——Paint 1(MaskFilter)

    对于Paint没有很好的深入的学习过,在工作之余再巩固巩固. 1.Paint的BlurMaskFilter(模糊效果) 自定义一个View继承View 重写里面的onDraw方法.这里直接上代码了: ...

  4. moment.js获取当前日期是当年的第几周

    /** * 实现当前日期是当年的第几周,再向前和向后推几周 * js数组保存当前日期的前后两周(共五周的数据) * */ var initSearchMajorChanges = function() ...

  5. COGS 615. 韩国明星

    [问题描述] 在LazyCat同学的影响下,Roby同学开始听韩国的音乐,并且越来越喜欢H.o.T,尤其喜欢安七炫和Tony,可是,爱学习爱思考的Roby同学想,如果以后喜欢的韩星越来越多怎么办呢?R ...

  6. iPhone开发小工具

    1.AppIcon: 可以瞬间把图片转换为应用所需要的Icon(Icon-72.png,Icon-72@2x.png,......iTunesArtwork@2x)   2.Resizer: 方便把- ...

  7. 找出指定文件夹中的所有以txt结尾的文件,包括所有嵌套的子文件夹

    # coding:utf-8 import os, re for i in os.walk('d:'+os.sep):     for txt in i[2]:         try:        ...

  8. wxwidgets编译及环境配置

    wxwidgets编译及环境配置 安装步骤: 到www.CodeBlocks.org下载并安装CodeBlocks,最好下载MinGW版本的,可以省掉安装和配置GCC的麻烦. 到www.wxWidge ...

  9. eclipse中Lombok注解无效

    问题现象:eclipse中使用lombok的@Date,引用get方法时,报错. 解决方案: 在lombok官网(https://www.projectlombok.org/download)下载,或 ...

  10. CPP-基础:char、BYTE、byte

    一,C++语言的内建类型中没“BYTE”这么个类型.BYTE是WINDOWS Platform SDK中windef.h里面定义的:typedef unsigned char BYTE; 二,char ...