• [洛谷P2602][ZJOI2010]数字计数


    题目大意:求区间$[l,r]$中数字$0sim9$出现个数

    题解:数位$DP$

    卡点:

    C++ Code:

    #include <cstdio>
    #include <iostream>
    struct node {
    	long long s[10], sum;
    	inline node() {for (register int i = 0; i < 10; i++) s[i] = 0; sum = 0;}
    	inline node(int x) {for (register int i = 0; i < 10; i++) s[i] = 0; sum = x;}
    	inline friend node operator + (const node &lhs, const node &rhs) {
    		node res;
    		res.sum = lhs.sum + rhs.sum;
    		for (register int i = 0; i < 10; i++) res.s[i] = lhs.s[i] + rhs.s[i];
    		return res;
    	}
    	inline friend node operator - (const node &lhs, const node &rhs) {
    		node res;
    		res.sum = lhs.sum - rhs.sum;
    		for (register int i = 0; i < 10; i++) res.s[i] = lhs.s[i] - rhs.s[i];
    		return res;
    	}
    	inline friend std::ostream & operator << (std::ostream &Fout, const node __node) {
    		for (register int i = 0; i < 10; i++) {
    			Fout << __node.s[i];
    			Fout << (i == 9 ? '
    ' : ' ');
    		}
    		return Fout;
    	}
    };
    
    int num[20], tot;
    node f[20];
    bool vis[20];
    node calc(int x, int lim, int lead) {
    	if (!x) return node(1);
    	if (!lim && lead && vis[x]) return f[x];
    	node F;
    	for (int i = lim ? num[x] : 9, op = 1; ~i; i--, op = 0) {
    		node tmp = calc(x - 1, lim && op, lead || i);
    		F = F + tmp;
    		if (i || lead) F.s[i] += tmp.sum;
    	}
    	if (!lim && lead) f[x] = F, vis[x] = true;
    	return F;
    }
    node solve(long long x) {
    	if (x < 0) return node();
    	tot = 0;
    	while (x) {
    		num[++tot] = x % 10;
    		x /= 10;
    	}
    	return calc(tot, 1, 0);
    }
    long long l, r;
    int main() {
    	std::ios::sync_with_stdio(false), std::cin.tie(0), std::cout.tie(0);
    	std::cin >> l >> r;
    	std::cout << solve(r) - solve(l - 1) << std::endl;
    	return 0;
    }
    

      

  • 相关阅读:
    hdu 2196 树形dp
    codeforces 1A
    [日常摸鱼]bzoj1218[HNOI2003]激光炸弹-二维前缀
    [日常摸鱼]bzoj2724蒲公英-分块
    [日常摸鱼]关于离散化
    [OI笔记]后缀自动机
    [日常摸鱼]poj1509Glass Beads-SAM
    [日常摸鱼]bzoj1083[SCOI2005]繁忙的都市-最小生成树
    [日常摸鱼]bzoj2038[2009国家集训队]小Z的袜子-莫队算法
    [日常摸鱼]三分法
  • 原文地址:https://www.cnblogs.com/Memory-of-winter/p/9888551.html
Copyright © 2020-2023  润新知