hdu4352---XHXJ's LIS(状态压缩数位dp)

194 篇文章 0 订阅

一开始我设计的状态是dp[i][j][sta],表示第i位为j,然后状态为sta,后来发现这样会导致后面的计算直接return,得不到正确答案
重新设计状态dp[i][k][sta]表示i位数,lis=k,状态为sta的个数,这里求LIS用的是O(nlogn)求法的思想

/*************************************************************************
    > File Name: hdu4352.cpp
    > Author: ALex
    > Mail: zchao1995@gmail.com 
    > Created Time: 2015年02月24日 星期二 10时55分05秒
 ************************************************************************/

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

LL dp[40][15][(1 << 10) + 10];
int bit[40];
int k;

int get_one (int sta)
{
    int cnt = 0;
    while (sta)
    {
        cnt += (sta & 1);
        sta >>= 1;
    }
    return cnt;
}

int get_sta (int sta, int d)
{
    for (int i = d; i <= 9; ++i)
    {
        if (sta & (1 << i))
        {
            sta &= ~(1 << i);
            break;
        }
    }
    sta |= (1 << d);
    return sta;
}

LL dfs (int cur, int sta, bool flag, bool zero)
{
    if (cur == -1)
    {
        if (zero)
        {
            return 0;
        }
        return get_one (sta) == k;
    }
    if (!flag && ~dp[cur][k][sta])
    {
        return dp[cur][k][sta];
    }
    int end = flag ? bit[cur] : 9;
    LL ans = 0;
    for (int i = 0; i <= end; ++i)
    {
        if (zero && !i)
        {
            ans += dfs (cur - 1, 0, flag && (i == end), 1);
        }
        else if (zero && i)
        {
            ans += dfs (cur - 1, (1 << i), flag && (i == end), 0);
        }
        else
        {
            int newsta = get_sta(sta, i);
            ans += dfs (cur - 1, newsta, flag && (i == end), 0);
        }
    }
    if (!flag)
    {
        dp[cur][k][sta] = ans;
    }
    return ans;
}

LL calc (LL n)
{
    int ret = 0;
    memset (bit, 0, sizeof(bit));
    while (n)
    {
        bit[ret++] = n % 10;
        n /= 10;
    }
    return dfs (ret - 1, 0, 1, 1);
}

int main ()
{
    int t;
    int icase = 1;
    scanf("%d", &t);
    memset (dp, -1, sizeof(dp));
    while (t--)
    {
        LL l, r;
        scanf("%lld%lld%d", &l, &r, &k);
        printf("Case #%d: ", icase++);
        printf("%lld\n", calc (r) - calc (l - 1));
    }
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值