题意:给一个数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. validate[.unobtrusive]和Bootstrap实现tooltip错误提示

    validate[.unobtrusive]和Bootstrap实现tooltip错误提示 类似的文章园子里已有,请看这里,个人感觉稍显复杂,日前也打算写一个简单的给项目用,一些关键点记录于此.最终效 ...

  2. python学习笔记5(元组)

    一.元组特性 1.类似列表,但不可变类型,正因如此,它可以做一个字典的key2.当处理一组对象时,这个组默认是元组类型3.所有的多对象,逗号分隔的,没有明确用符号定义的这些都默认为元组类型 >& ...

  3. hdu 4707 Pet(DFS水过)

    http://acm.hdu.edu.cn/showproblem.php?pid=4707 [题目大意]: Lin Ji 的宠物鼠丢了,在校园里寻找,已知Lin Ji 在0的位置,输入N D,N表示 ...

  4. Automotive Security的一些资料和心得(1):Security Engineering

    陆续更新一些最近在Automotive Security方面的资料和心得. 1. Overview 1.1. Software Engineering Process PLC-Phases: Intr ...

  5. REST Design Concerns

    Less Requests, More data; one of the core RESTful API design paradigms is the concept of less API re ...

  6. spoj 39

    DP  完全背包问题 的   d[i] = min(d[i], d[i-w]+p)   d[i]表示当总重量为i时可装的最小价值 #include <cstdio> #include &l ...

  7. Tiny6410 设备驱动之helloworld

    在自己的工作目录下建立helloworld_driver.c #include <linux/init.h> #include <linux/module.h> //代码遵守的 ...

  8. EL表达式对数组、集合操作

    el表达式是通过${key}的方式获取对象中的值.在el表达式中有如下几个隐含的对象,pageScope,requestSope,sessionScope,applicationScope,如果要取$ ...

  9. java版AC自动机

    class Trie { int [][]Next=new int[500005][128]; int []fail=new int[500005]; int []end=new int[500005 ...

  10. Win7 默认hosts文件

    # Copyright (c) 1993-2009 Microsoft Corp. # # This is a sample HOSTS file used by Microsoft TCP/IP f ...