标签: 记忆化搜索

[BZOJ3208]花神的秒题计划Ⅰ 题解

[BZOJ3208]花神的秒题计划Ⅰ 题解

题目地址:BZOJ:Problem 3208. — 花神的秒题计划Ⅰ 题目描述 

[ZJOI2010]数字计数 题解

[ZJOI2010]数字计数 题解

题目地址:洛谷:【P2602】[ZJOI2010]数字计数 – 洛谷、BZOJ 

[HDU4352]XHXJ’s LIS 题解

[HDU4352]XHXJ’s LIS 题解

题目地址:HDUOJ:Problem – 4352

题目描述

#define xhxj (Xin Hang senior sister(学姐))
If you do not know xhxj, then carefully reading the entire description is very important.
As the strongest fighting force in UESTC, xhxj grew up in Jintang, a border town of Chengdu.
Like many god cattles, xhxj has a legendary life:
2010.04, had not yet begun to learn the algorithm, xhxj won the second prize in the university contest. And in this fall, xhxj got one gold medal and one silver medal of regional contest. In the next year’s summer, xhxj was invited to Beijing to attend the astar onsite. A few months later, xhxj got two gold medals and was also qualified for world’s final. However, xhxj was defeated by zhymaoiing in the competition that determined who would go to the world’s final(there is only one team for every university to send to the world’s final) .Now, xhxj is much more stronger than ever,and she will go to the dreaming country to compete in TCO final.
As you see, xhxj always keeps a short hair(reasons unknown), so she looks like a boy( I will not tell you she is actually a lovely girl), wearing yellow T-shirt. When she is not talking, her round face feels very lovely, attracting others to touch her face gently。Unlike God Luo’s, another UESTC god cattle who has cool and noble charm, xhxj is quite approachable, lively, clever. On the other hand,xhxj is very sensitive to the beautiful properties, “this problem has a very good properties”,she always said that after ACing a very hard problem. She often helps in finding solutions, even though she is not good at the problems of that type.
Xhxj loves many games such as,Dota, ocg, mahjong, Starcraft 2, Diablo 3.etc,if you can beat her in any game above, you will get her admire and become a god cattle. She is very concerned with her younger schoolfellows, if she saw someone on a DOTA platform, she would say: “Why do not you go to improve your programming skill”. When she receives sincere compliments from others, she would say modestly: “Please don’t flatter at me.(Please don’t black).”As she will graduate after no more than one year, xhxj also wants to fall in love. However, the man in her dreams has not yet appeared, so she now prefers girls.
Another hobby of xhxj is yy(speculation) some magical problems to discover the special properties. For example, when she see a number, she would think whether the digits of a number are strictly increasing. If you consider the number as a string and can get a longest strictly increasing subsequence the length of which is equal to k, the power of this number is k.. It is very simple to determine a single number’s power, but is it also easy to solve this problem with the numbers within an interval? xhxj has a little tired,she want a god cattle to help her solve this problem,the problem is: Determine how many numbers have the power value k in [L,R] in O(1)time.
For the first one to solve this problem,xhxj will upgrade 20 favorability rate。
定义一个数的内部LIS长度为这个数拆成一个个数字这个序列的LIS长度,给出区间[l, r],求区间内内部LIS长度为k的数的个数。

输入输出格式

输入格式:
First a integer T(T<=10000),then T lines follow, every line has three positive integer L,R,K.(0<L<=R<2^63-1 and 1<=K<=10).

输出格式:
For each query, print “Case #t: ans” in a line, in which t is the number of the test case starting from 1 and ans is the answer.

输入输出样例

输入样例#1:

1
123 321 2

输出样例#1:

Case #1: 139 

题解

思路参考HDU 4352 XHXJ’s LIS(数位dp&状态压缩) – CSDN博客,感谢原作者。
首先这个题我们可以想到数位DP(人生第一个数位DP题),但是数位DP怎么来处理LIS是个很棘手的问题。
我们考虑O(n \log n)求LIS的方法,是在外部维护了一个当前LIS的数组,我们如果在这里用这个数组,会发现LIS的长度不会大于10,LIS的数组显然是可以状态压缩做的。这样,如果进行一些预处理,求LIS的复杂度可以是O(1)的。
设计状态dp[len][S][k]为枚举到长度为len,S为目前LIS数组里的元素情况,要求的LIS长度为k的数字数量。考虑采取记忆化搜索,我们可以向后枚举当前位置可以填充哪个数字。当填充完毕后,检查LIS是否等于k,并返回,这是整个搜索的终点。由于我们询问区间,势必会对答案产生一定的限制,如果当前搜索受到限制,那么搜索的结果不应该被保存。还要特别处理一下前导0之类的。
实现细节参考代码注释吧。

代码

// Code by KSkun, 2018/3
#include <cstdio>
#include <cstring>

typedef long long LL;

inline char fgc() {
    static char buf[100000], *p1 = buf, *p2 = buf;
    return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF 
        : *p1++;
}

inline LL readint() {
    register LL res = 0, neg = 1;
    char c = fgc();
    while(c < '0' || c > '9') {
        if(c == '-') neg = -1;
        c = fgc();
    }
    while(c >= '0' && c <= '9') {
        res = res * 10 + c - '0';
        c = fgc();
    }
    return res * neg;
}

LL dp[20][1 << 10][11];
int dis[20], cnt[1 << 10], nxt[10][1 << 10];

// 这里是计算每个状态后插入一个数的状态
inline int calnxt(int s, int num) {
    for(int i = num; i <= 9; i++) {
        if(s & (1 << i)) {
            return (s ^ (1 << i)) | (1 << num);
        }
    }
    return s | (1 << num);
}

inline void init() {
    memset(dp, -1, sizeof(dp));
    // 把每个状态的LIS长度处理出来
    for(int i = 0; i < (1 << 10); i++) {
        cnt[i] = 0;
        for(int j = 0; j < 10; j++) {
            if(i & (1 << j)) cnt[i]++;
            nxt[j][i] = calnxt(i, j);
        }
    }
}

// s表示状态,lim表示当前是否受限,zero表示当前是否在前导0里
inline LL dfs(int k, int len, int s, bool lim, bool zero) {
    if(len < 0) return cnt[s] == k;
    if(!lim && dp[len][s][k] != -1) return dp[len][s][k];
    LL res = 0;
    int limm = lim ? dis[len] : 9;
    for(int i = 0; i <= limm; i++) {
        // zero产生的影响是状态不会发生改变
        res += dfs(k, len - 1, (zero && i == 0) ? s : nxt[i][s], lim && i == limm, 
            zero && i == 0);
    }
    if(!lim) dp[len][s][k] = res;
    return res;
}

inline LL work(LL n, LL k) {
    // 先处理出来限制条件
    int pos = 0;
    while(n) {
        dis[pos++] = n % 10;
        n /= 10;
    }
    return dfs(k, pos - 1, 0, true, true);
}

int T;
LL l, r, k;

int main() {
    T = readint();
    init();
    for(int ii = 1; ii <= T; ii++) {
        l = readint();
        r = readint();
        k = readint();
        // 这里我们选择把0~l-1和0~r的都求出来然后减掉
        printf("Case #%d: %lld\n", ii, work(r, k) - work(l - 1, k));
    }
    return 0;
}