题意:给一个数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. 子查询注意这几点, 就可以写出好的sql语句

    执行sql时子查询的语句一般优先执行 理论上多表查询效率是高于子查询(根据子查询不值一个查询语句可能会有多个from但是多表查询只产生一个from), 但是在oracle中子查询效率一般会高于多表查询

  2. C# Windows - TabControl

    TabControl控件的属性 - 一般用于控制TabPages对象容器的外观,特别是显示的选项卡的外观 属性 说明 Alignment 控制选项卡在选项卡控件的什么位置显示 Appearance 控 ...

  3. 1003: [ZJOI2006]物流运输trans - BZOJ

    Description 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治者整个星系.某一天,凭着一个偶然的机遇,一支反抗军摧毁了帝国的超级武器,并攻下了星系中几乎所有的星球.这些星球通过 ...

  4. Objective C 四舍五入,float处理

    NSLog(@"平方:%.f", pow(3,2) ); //result 9 NSLog(@"上舍入:%.f", ceil(3.000000000001)); ...

  5. string.Equals 比较2个字符串是否相同忽略大小写

    bool res = string.Equals(str1, str2, StringComparison.CurrentCultureIgnoreCase)

  6. PAT-乙级-1037. 在霍格沃茨找零钱(20)

    1037. 在霍格沃茨找零钱(20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 如果你是哈利·波特迷,你会知 ...

  7. Hibernate 二级缓存 总结整理(转)

    和<Hibernate 关系映射 收集.总结整理> 一样,本篇文章也是我很早之前收集.总结整理的,在此也发上来 希望对大家有用.因为是很早之前写的,不当之处请指正. 1.缓存:缓存是什么, ...

  8. Mesh Baker的基本操作与功能演示

    原地址:http://www.narkii.com/club/thread-301789-1.html 如何降低游戏在系统中的消耗并带给用户最佳的体验是开发者一直追求的目标,在Unity里面对于模型与 ...

  9. FiddlerScript开发

    1.为Fiddler会话列表添加自定义列 只需要为你的方法(方法名任意)添加BindUIColumn Attribute 就可以添加自定义列到Session List,下面的代码添加Method列到会 ...

  10. vc2008程序发布指南

    vc2008程序发布指南 2008-05-03 17:46 vc2008开发的程序的发布方式可以有5种方式: 1. 采用静态链接到crt和MFC. 只要你拥有组成程序的所有源代码,你就可以采用这种方式 ...