题意:给一个数q,
q=1时求给定区间,给定进制,各数位和等于m的数字的个数
q=2时求给定区间,给定进制,各数位和等于m的数字中的第k大的数字
分析:dp[i][sum][j],表示长度为i当前数位和是sum,进制是j的个数,q=2时用二分求出k大数
题意给的区间[x,y],x不一定小于y,给定区间没k大数,则输出 Could not find the Number!
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <cctype>
#include <complex>
#include <cassert>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
#define lson l,m,rt<<1
#define pi acos(-1.0)
#define rson m+1,r,rt<<11
#define All 1,N,1
#define read freopen("in.txt", "r", stdin)
const ll INFll = 0x3f3f3f3f3f3f3f3fLL;
const int INF= 0x7ffffff;
const int mod = ;
ll dp[][][],x,y,k;
int b,m,bit[];
ll dfs(int i,int s,int j,int e){
if(i==)return (s==m);
if(!e&&dp[i][s][j]!=-)return dp[i][s][j];
int u=e?bit[i]:b-;
ll num=;
for(int v=;v<=u;++v){
num+=dfs(i-,s+v,j,e&&(v==u));
}
return e?num:dp[i][s][j]=num;
}
ll solve1(ll a){
int len=;
if(a<)return ;
while(a){
bit[++len]=a%b;
a/=b;
}
return dfs(len,,b,);
}
ll solve2(){
ll l=x,r=y;
ll num=solve1(x-);
if(solve1(y)-num<k)return -;
while(l<=r){
ll mid=(l+r)>>;
if(solve1(mid)-num<k)l=mid+;
else r=mid-;
}
return l;
}
int main()
{
int q,cas=;
while(~scanf("%d",&q)){
memset(dp,-,sizeof(dp));
printf("Case %d:\n",++cas);
if(q==){
scanf("%I64d%I64d%d%d",&x,&y,&b,&m);
if(x>y)swap(x,y);
printf("%I64d\n",solve1(y)-solve1(x-));
}
else{
scanf("%I64d%I64d%d%d%I64d",&x,&y,&b,&m,&k);
if(x>y)swap(x,y);
ll tmp=solve2();
if(tmp!=-)
printf("%I64d\n",tmp);
else
printf("Could not find the Number!\n");
}
}
return ;
}