HDU 3271-SNIBB(数位dp)

题意:给一个数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 =  1000000007;
ll dp[50][500][65],x,y,k;
int b,m,bit[50];
ll dfs(int i,int s,int j,int e){
    if(i==0)return (s==m);
    if(!e&&dp[i][s][j]!=-1)return dp[i][s][j];
    int u=e?bit[i]:b-1;
    ll num=0;
    for(int v=0;v<=u;++v){
        num+=dfs(i-1,s+v,j,e&&(v==u));
    }
    return e?num:dp[i][s][j]=num;
}
ll solve1(ll a){
    int len=0;
    if(a<0)return 0;
    while(a){
        bit[++len]=a%b;
        a/=b;
    }
    return dfs(len,0,b,1);
}
ll solve2(){
    ll l=x,r=y;
    ll num=solve1(x-1);
    if(solve1(y)-num<k)return -1;
    while(l<=r){
        ll mid=(l+r)>>1;
        if(solve1(mid)-num<k)l=mid+1;
        else r=mid-1;
    }
    return l;
}
int main()
{
    int q,cas=0;
    while(~scanf("%d",&q)){
        memset(dp,-1,sizeof(dp));
        printf("Case %d:\n",++cas);
        if(q==1){
            scanf("%I64d%I64d%d%d",&x,&y,&b,&m);
            if(x>y)swap(x,y);
            printf("%I64d\n",solve1(y)-solve1(x-1));
        }
        else{
            scanf("%I64d%I64d%d%d%I64d",&x,&y,&b,&m,&k);
            if(x>y)swap(x,y);
            ll tmp=solve2();
            if(tmp!=-1)
            printf("%I64d\n",tmp);
            else
            printf("Could not find the Number!\n");
        }
    }
return 0;
}
时间: 2024-10-07 05:27:05

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

[ACM] hdu 3555 Bomb (数位DP,统计1-N中含有“49”的总数)

Bomb Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 7187 Accepted Submission(s): 2512 Problem Description The counter-terrorists found a time bomb in the dust. But this time the terrorists impro

HDU——B-number(数位DP)

题目大意: 要找出1到n之间有多少个数含13,并且能被13整除 记忆化搜索: dp[pos][pre][mod][statu],pos位数,pre前一位,mod余数,statu状态 有2个状态:含13,不含13 <span style="font-size:18px;"><strong>#include<iostream> #include<cstring> #include<cstdio> #include<algor

[数位dp] hdu 3271 SNIBB

题意:有两种询问: q=1,在[x,y]区间内,转换成b进制数,数位和为m的有多少个. q=2,在[x,y]区间内,转换成b进制数,数位和是m的第k个数是多少(十进制),不存在按题目给出输出. 思路: 和普通数位dp一样,第几个数的话就是二分判断. 然后就是按常理要开4维,就是dp[i][sum][b][m] i位,和为sum,b进制,最后和为m 但是开不下,所以开3维每次初始化. 注意一下: 1.每次都要初始化 2.x不一定小于y 3.是[x,y]不是(x,y] 代码: #include"cs

HDU 3271 SNIBB

SNIBB Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 327164-bit integer IO format: %I64d      Java class name: Main As we know, some numbers have interesting property. For example, any even number has the pr

HDU 3555 Bomb (数位DP)

数位dp,主要用来解决统计满足某类特殊关系或有某些特点的区间内的数的个数,它是按位来进行计数统计的,可以保存子状态,速度较快.数位dp做多了后,套路基本上都差不多,关键把要保存的状态给抽象出来,保存下来. 简介: 顾名思义,所谓的数位DP就是按照数字的个,十,百,千--位数进行的DP.数位DP的题目有着非常明显的性质: 询问[l,r]的区间内,有多少的数字满足某个性质 做法根据前缀和的思想,求出[0,l-1]和[0,r]中满足性质的数的个数,然后相减即可. 算法核心: 关于数位DP,貌似写法还是

HDU 5898 (数位DP)

HDU 5898 odd-even number 题意:如果一个数连续的奇数之和为偶数,连续的偶数之和为奇数则满足条件,问某一区间内满足条件的数字的个数. 思路:数位DP,dp[i][j][k][u]表示计算到底i位,是否为临界值,上一位是奇还是偶,连续奇或偶有u个,在dfs是多记录一下该数前面是不是全为0以及该数是不是已近不满足条件. #include<cstdio> #include<cstring> #include<iostream> #include<a

HDU 2089 简单数位dp

1.HDU 2089  不要62    简单数位dp 2.总结:看了题解才敲出来的,还是好弱.. #include<iostream> #include<cstring> #include<cstdio> using namespace std; int dp[10][11]; //dp[i][j]表示以j为首位符合条件的i位数的个数 void init() //预处理 { memset(dp,0,sizeof(dp)); dp[0][0]=1; for(int i=1

HDU(3555),数位DP

题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3555 Bomb Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 15372    Accepted Submission(s): 5563 Problem Description The counter-terrorists f

hdu 3555(数位dp 入门)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3555 Bomb Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 7716    Accepted Submission(s): 2702 Problem Description The counter-terrorists found a

HDU 3555 Bomb(数位DP)

Description The cows, as you know, have no fingers or thumbs and thus are unable to play Scissors, Paper, Stone' (also known as 'Rock, Paper, Scissors', 'Ro, Sham, Bo', and a host of other names) in order to make arbitrary decisions such as who gets