标签: 数位DP

[HAOI2010]计数 题解

[HAOI2010]计数 题解

题目地址:洛谷:【P2518】[HAOI2010]计数 – 洛谷、BZOJ:Problem 2425. — [HAOI2010]计数

题目描述

你有一组非零数字(不一定唯一),你可以在其中插入任意个0,这样就可以产生无限个数。比如说给定{1,2},那么可以生成数字12,21,102,120,201,210,1002,1020,等等。
现在给定一个数,问在这个数之前有多少个数。(注意这个数不会有前导0).

输入输出格式

输入格式:
只有1行,为1个整数n.

输出格式:
只有整数,表示N之前出现的数的个数。

输入输出样例

输入样例#1:

1020

输出样例#1:

7

说明

n的长度不超过50,答案不超过2^63-1.

题解

参考资料:题解 P2518 【[HAOI2010]计数】 – noob – 洛谷博客
如果我们不删去前导0,其实就是相当于求对给定数的全排列中,比这个数小的排列个数。考虑一个数位DP的思想,如果一个高位上填的数字已经更小了,后面的位置显然是可以瞎邒排的,直接往答案里加一个全排列就好。但是可重集的全排列是有可能爆LL的,这里我们采用一种折中的办法求:如果集合大小为n,每个数码的个数为xi,考虑从n个位置里先取x0个位置放置0,然后从n-x0个位置放置1,,以此类推。也就是说,上面那个可重集的全排列数量是
\prod_{i=0}^9 \mathrm{C}_{n - \sum_{j=0}^{i-1} x_j}^{x_i}
我们枚举若前面的所有数位都与原数相同且现在这一位填某数码的时候计算答案,加进去就可以了。

代码

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

typedef long long LL;

const int MAXN = 55;

LL C[MAXN][MAXN];

inline void calc() {
    C[0][0] = 1;
    for(int i = 1; i < MAXN; i++) {
        C[i][0] = 1;
        for(int j = 1; j <= i; j++) {
            C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
        }
    }
}

char num[MAXN];
int n, cnt[10];

inline LL cal(int n, int *cnt) {
    LL res = 1;
    for(int i = 0; i <= 9; i++) {
        res *= C[n][cnt[i]];
        n -= cnt[i];
    }
    return res;
}

int main() {
    calc();
    scanf("%s", num + 1); n = strlen(num + 1);
    for(int i = 1; i <= n; i++) {
        cnt[num[i] - '0']++;
    }
    LL ans = 0;
    for(int i = 1; i <= n; i++) {
        for(int j = 0; j < num[i] - '0'; j++) {
            if(cnt[j]) {
                cnt[j]--;
                ans += cal(n - i, cnt);
                cnt[j]++;
            }
        }
        cnt[num[i] - '0']--;
    }
    printf("%lld", ans);
    return 0;
}
[51Nod1684]子集价值 题解

[51Nod1684]子集价值 题解

题目地址:51Nod:子集价值 问题 – 51Nod

题目描述

lyk最近在研究位运算。它发现除了xor,or,and外还有很多运算。它新定义了一种运算符“#”。具体地,可以由4个参数来表示。ai,j表示i#j。其中i,j与a的值均∈[0,1]。当然问题可以扩展为>1的情况,具体地,可以将两个数分解为p位,然后对于每一位执行上述的位运算,再将这个二进制串转化为十进制就可以了。例如当 a0,0=a1,1=0,a0,1=a1,0=1时,3#4在p=3时等于7,2#3在p=4时等于1(实际上就是异或运算)。
现在lyk想知道的是,已知一个数列b。它任意选取一个序列c,满足 c1<c2<…<ck,其中1≤c1且ck≤n ,这个序列的价值为 bc1 # bc2 #…# bck 的平方。这里我们假设k是正整数,因此满足条件的c的序列一定是 2n−1 。lyk想知道所有满足条件的序列的价值总和是多少。例如样例中,7个子集的价值分别为1,1,4,4,9,9,0。总和为28。
由于答案可能很大,只需对1,000,000,007取模即可。

输入输出格式

输入格式:
第一行两个整数n(1<=n<=50000),p(1<=p<=30)。
第二行4个数表示a0,0,a0,1,a1,0,a1,1。(这4个数都∈{0,1})
第三行n个数bi(0<=bi<2^p)。

输出格式:
一行表示答案。

输入输出样例

输入样例#1:

3 30
0 1 1 0
1 2 3

输出样例#1:

28

题解

又是一道喜闻乐见的数位DP。
首先这个平方给我们设置了一个障碍,我们如果不求平方只求和,那么我们可以设计状态dp[i][0/1]表示当前处理到第i个数,现在正在处理的这一位经过运算后为0或1的方案总数。我们从大到小枚举每一位,每一位对答案的贡献就是1<<数*dp[n][1]。
但是现在有一个平方,那我们考虑平方的意义。如果把每一位单独拆出来算,那么一个十进制数可以表示成一堆2的幂加起来的结果,又由于我们有下面的式子
(A_1+A_2+A_3+A_4+\cdots+A_n)^2 = A_1A_2 + \cdots + A_1A_n + A_2A_1 + \cdots + A_2A_n + \cdots + A_n^2
我们发现,平方后的结果的每一项只跟原来的两项有关系。换句话说,原来的数中两个位就能对平方后的答案产生一个位的贡献。那我们考虑把上面求和的DP状态扩展为dp[i][0/1][0/1]表示当前处理到第i个数,选定的两位分别为多少的方案总数。我们枚举选定哪两位来组合出答案,每枚举一次就DP一次,由于两位必须得都是1乘起来才非0,最后枚举的两位产生的贡献就是dp[n][1][1]*1<<(i+j),其中i和j是枚举的两位。
这样做的时间复杂度是O(p^2n)的。

代码

// 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;
}

const int MAXN = 50005, MO = 1e9 + 7;

int n, p, op[2][2];
LL dp[MAXN][2][2], a[MAXN];

int main() {
    n = readint();
    p = readint();
    op[0][0] = readint();
    op[0][1] = readint();
    op[1][0] = readint();
    op[1][1] = readint();
    for(int i = 1; i <= n; i++) {
        a[i] = readint();
    }
    LL ans = 0;
    for(int i = 0; i < p; i++) {
        for(int j = 0; j < p; j++) {
            memset(dp, 0, sizeof(dp));
            for(int k = 1; k <= n; k++) {
                int p1 = (a[k] >> i) & 1, p2 = (a[k] >> j) & 1;
                dp[k][p1][p2] = (dp[k][p1][p2] + 1) % MO;
                dp[k][op[0][p1]][op[0][p2]] = (dp[k][op[0][p1]][op[0][p2]] + dp[k - 1][0][0]) % MO;
                dp[k][0][0] = (dp[k][0][0] + dp[k - 1][0][0]) % MO;
                dp[k][op[0][p1]][op[1][p2]] = (dp[k][op[0][p1]][op[1][p2]] + dp[k - 1][0][1]) % MO;
                dp[k][0][1] = (dp[k][0][1] + dp[k - 1][0][1]) % MO;
                dp[k][op[1][p1]][op[0][p2]] = (dp[k][op[1][p1]][op[0][p2]] + dp[k - 1][1][0]) % MO;
                dp[k][1][0] = (dp[k][1][0] + dp[k - 1][1][0]) % MO;
                dp[k][op[1][p1]][op[1][p2]] = (dp[k][op[1][p1]][op[1][p2]] + dp[k - 1][1][1]) % MO;
                dp[k][1][1] = (dp[k][1][1] + dp[k - 1][1][1]) % MO;
            }
            ans = (ans + (1ll << (i + j)) % MO * dp[n][1][1] % MO) % MO;
        }
    }
    printf("%lld", ans);
    return 0;
}
[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;
}