HDU 3271 数位dp+二分
SNIBB
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1485 Accepted Submission(s): 435
Problem Description
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.
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)
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.
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);
Author
AekdyCoin
Source
题意:
求区间内的数在b进制下个位数的和是m的数的个数
求区间内第k个在b进制下个位数的和是m的数
代码:
#include<iostream> #include<cstdio> #include<cstring> using namespace std; typedef long long ll; int bit[40]; ll f[40][309]; ll dfs(int pos,int m,bool limit,int b) { if(pos==0) return m==0; if(m<0) return 0; if(!limit&&f[pos][m]!=-1) return f[pos][m]; int max_b=(limit?bit[pos]:b-1); ll ans=0; for(int i=0;i<=max_b;i++){ ans+=dfs(pos-1,m-i,limit&&(i==max_b),b); } if(!limit) f[pos][m]=ans; return ans; } ll solve(ll x,int b,int m) { if(x<0) return 0; int pos=0; while(x){ bit[++pos]=x%b; x/=b; } return dfs(pos,m,1,b); } int main() { int p,b,m,cas=0; ll X,Y,k; while(scanf("%d",&p)==1){ printf("Case %d:\n",++cas); memset(f,-1,sizeof(f)); if(p==1){ scanf("%lld%lld%d%d",&X,&Y,&b,&m); if(X>Y) swap(X,Y); printf("%lld\n",solve(Y,b,m)-solve(X-1,b,m)); }else{ scanf("%lld%lld%d%d%lld",&X,&Y,&b,&m,&k); if(X>Y) swap(X,Y); ll tmp1=solve(Y,b,m),tmp2=solve(X-1,b,m); if(k>tmp1-tmp2) puts("Could not find the Number!"); else{ ll l=X,r=Y,ans; while(l<=r){ ll mid=(l+r)>>1; ll tmp=solve(mid,b,m); if(tmp-tmp2>=k) { ans=mid;r=mid-1; } else l=mid+1; } printf("%lld\n",ans); } } } return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 【杂谈】分布式事务——高大上的无用知识?