hdu4352 (数位dp+状态压缩)

HDU 4352 XHXJ’s LIS

传送门
题目描述:
给定一个区间中,将区间的每一个数看成一个字符串,求这个区间内每个字符串的最大上升
子序列等于k的个数。
思路:
dp[pos][sta][k]
pos:搜索到第几位
sta:之前的最长上升子序列的状态。
k:就是输入的那个k
牛逼的地方:
sta储存为0-9每个数字的状态 类似最长上升子序列更新方式。sta二进制下1的个数就是当前最长上升子序列的长度
比如:1245->12453 LIS都为3 状态sta变化为 00000110110->00000101110
即寻找3在原数组lower_bound的位置为4 把4位置0,3位置1

#pragma GCC optimize(3,"Ofast","inline")  	//G++
#include<bits/stdc++.h>
#define mem(a,x) memset(a,x,sizeof(a))
#define debug(x) cout << #x << ": " << x << endl;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fcout cout<<setprecision(4)<<fixed
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pii;

const ll inf=0x3f3f3f3f;
const ll mod=1e9+7;
const ll maxn = 1e6+5;

template<typename T> void read(T &x){
    x = 0;char ch = getchar();ll f = 1;
    while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
    while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
template<typename T, typename... Args> void read(T &first, Args& ... args) {
    read(first);
    read(args...);
}
ll dp[20][1<<10][11];
ll a[20];

ll get_sta(ll sta,ll k){
    for(ll i=k;i<=9;i++){
        if(sta&(1<<i)) return (sta^(1<<i))|(1<<k);
    }
    return sta|(1<<k);
}
ll dfs(ll pos,ll sta,ll k,bool limit,bool lead){
    if(!limit&&dp[pos][sta][k]!=-1)  return dp[pos][sta][k];
    if(pos==0) return __builtin_popcount(sta)==k;
    ll up=limit?a[pos]:9;
    ll res=0;
    for(ll i=0;i<=up;i++){
        ll t=sta;
        if(!(lead&&i==0)){
            t=get_sta(sta,i);
        }
        res+=dfs(pos-1,t,k,limit&&a[pos]==i,lead&&i==0);
    }
    if(!limit) dp[pos][sta][k]=res;
    return res;
}

ll solve(ll n,ll k){
    ll pos=0;
    while(n){
        a[++pos]=n%10;
        n/=10;
    }
    return dfs(pos,0,k,1,1);
}
 main()
{
    int Tim=0;
    mem(dp,-1);
    ll T;
    read(T);
    while(T--){
        ll l,r,k;
        read(l,r,k);
        printf("Case #%d: ",++Tim);
        cout<<solve(r,k)-solve(l-1,k)<<"\n";
    }
}

__builtin_popcount 返回值为unsigned int

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值