题意:给一个数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 ;
}

HDU 3271-SNIBB(数位dp)的更多相关文章

  1. hdu 3271 SNIBB 数位DP+二分

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

  2. Bomb HDU - 3555 (数位DP)

    Bomb HDU - 3555 (数位DP) The counter-terrorists found a time bomb in the dust. But this time the terro ...

  3. HDU 3271 SNIBB

    SNIBB Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 3271 ...

  4. HDU 2089 简单数位dp

    1.HDU 2089  不要62    简单数位dp 2.总结:看了题解才敲出来的,还是好弱.. #include<iostream> #include<cstring> #i ...

  5. HDU(3555),数位DP

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3555 Bomb Time Limit: 2000/1000 MS (Java/Others ...

  6. HDU(4734),数位DP

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=4734 F(x) Time Limit: 1000/500 MS (Java/Others) ...

  7. HDU 3555 Bomb 数位dp

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3555 Bomb Time Limit: 2000/1000 MS (Java/Others) Mem ...

  8. Hdu 4734 【数位DP】.cpp

    题意: 我们定义十进制数x的权值为f(x) = a(n)*2^(n-1)+a(n-1)*2(n-2)+...a(2)*2+a(1)*1,a(i)表示十进制数x中第i位的数字. 题目给出a,b,求出0~ ...

  9. HDU 6148 (数位DP)

    ### HDU 6148 题目链接 ### 题目大意: 众所周知,度度熊非常喜欢数字. 它最近发明了一种新的数字:Valley Number,像山谷一样的数字. 当一个数字,从左到右依次看过去数字没有 ...

  10. hdu 3652 【数位dp】

    hdu 3652 题意:求1到n中包含'13'('13'不一定连续)且能被13整除的数的个数. 这是我第一道比较了能看懂的数位dp.定义状态dp[pos][res][sta]表示处理到第pos位,模的 ...

随机推荐

  1. myEclipse中的web项目直接引入到eclipse中运行

    首先打开项目属性(Properties),如果动态web项目被作为普通java项目引进去,需要首先修改为web项目,如下图: 确定后即可在eclipse中看到转换为了动态的web项目,然后继续属性(P ...

  2. 选择排序O(n^2)与快速排序O(nlogn)的优越性代码体现

    随机函数生成一个超大数组: [code]: #include <iostream> #include <stdio.h> #include<time.h> #inc ...

  3. 4.3 spring-嵌入式beans标签的解析

    对于嵌入式的beans标签,想信大家很少使用过,或者接触过,起码,我本人就没用过. 它非常类似于Import标签所提供的功能; 使用如下: <?xml version="1.0&quo ...

  4. Why are very few schools involved in deep learning research? Why are they still hooked on to Bayesian methods?

    Why are very few schools involved in deep learning research? Why are they still hooked on to Bayesia ...

  5. 汇编之FS段寄存器

    FS寄存器指向当前活动线程的TEB结构(线程结构) 偏移  说明 000  指向SEH链指针 004  线程堆栈顶部 008  线程堆栈底部 00C  SubSystemTib 010  FiberD ...

  6. 破解之API断点法

    上回给大家做的破解教程,地址是http://www.52pojie.net/thread-52719-1-1.html,用的是“调用堆栈”方法.今天给新手提供另一种方法“API函数断点”,这种方法要求 ...

  7. 分析jQuery中的each方法

    在看jQuery源码是怎么实现each方法之前,我们看一下js的原生实现. ECMAScript 5为数组定义了一个forEach方法,该方法接受两个参数:第一个参数是要在每一个数组项上运行的函数,第 ...

  8. poj 2888 Magic Bracelet

    经典的有限制条件的Burnside计数+矩阵乘法!!! 对于这种限制条件的情况我们可以通过矩阵连乘得到,先初始化矩阵array[i][j]为1.如果颜色a和颜色b不能涂在相邻的珠子, 那么array[ ...

  9. Java 的 Class Path 和 Package

    前言:   由于这两个问题新手问得较多, 且回答比较零散, 很难统一整理, 所以就直接写了一篇, 还请大家见谅. 正文:一, 类路径 (class path)   当你满怀着希望安装好了 java, ...

  10. Qt:无标题栏无边框程序的拖动和改变大小

    From: http://blog.csdn.net/kfbyj/article/details/9284923 最近做项目遇到的问题,总结下. 有时候我们觉得系统的标题栏和按钮太丑太呆板,想做自己的 ...