POJ - 2282 The Counting Problem(数位DP 计数问题)

这篇具有很好参考价值的文章主要介绍了POJ - 2282 The Counting Problem(数位DP 计数问题)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

题目如下:

Given two integers a and b, we write the numbers between a and b, inclusive, in a list. Your task is to calculate the number of occurrences of each digit. For example, if a = 1024 a = 1024 a=1024 and b = 1032 b = 1032 b=1032, the list will be
1024 1024 1024 1025 1025 1025 1026 1026 1026 1027 1027 1027 1028 1028 1028 1029 1029 1029 1030 1030 1030 1031 1031 1031 1032 1032 1032

there are ten 0’s in the list, ten 1’s, seven 2’s, three 3’s, and etc.

Input

The input consists of up to 500 500 500 lines. Each line contains two numbers a and b where 0 < a , b < 100000000 0 < a, b < 100000000 0<a,b<100000000. The input is terminated by a line `0 0’, which is not considered as part of the input.
Output
For each pair of input, output a line containing ten numbers separated by single spaces. The first number is the number of occurrences of the digit 0 0 0, the second is the number of occurrences of the digit 1 1 1, etc.

Sample

Input

1 10
44 497
346 542
1199 1748
1496 1403
1004 503
1714 190
1317 854
1976 494
1001 1960
0 0

Output

1 2 1 1 1 1 1 1 1 1
85 185 185 185 190 96 96 96 95 93
40 40 40 93 136 82 40 40 40 40
115 666 215 215 214 205 205 154 105 106
16 113 19 20 114 20 20 19 19 16
107 105 100 101 101 197 200 200 200 200
413 1133 503 503 503 502 502 417 402 412
196 512 186 104 87 93 97 97 142 196
398 1375 398 398 405 499 499 495 488 471
294 1256 296 296 296 296 287 286 286 247

题意简述:

计算 a a a ~ b b b, 0 ~ 9 数字的出现次数
注意:输入的时候可以 a > b a > b a>b 计算的时候应使 a ≤ b a \le b ab

思路 or 题意:

数位DP – 计数问题文章来源地址https://www.toymoban.com/news/detail-415963.html

AC 代码如下:

/*
Make it simple and keep self stupid
author:Joanh_Lan
*/
#pragma GCC optimize(3)
#pragma GCC optimize("inline") // 如果比赛允许开编译器优化的话,可以默写这两段
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <numeric>
#include <cstring>
#include <cmath>
#include <map>
#include <bitset>
#include <set>
#include <ctime>
#include <queue>
#include <stack>
#include <climits>
#define buff                     \
    ios::sync_with_stdio(false); \
    cin.tie(0);
#define int long long
#define ll long long
#define PII pair<int, int>
#define px first
#define py second
using namespace std;
const int mod = 1e9 + 7;
const int inf = 2147483647;
const int N = 100009;
//int Mod(int a,int mod){return (a%mod+mod)%mod;}
//int lowbit(int x){return x&-x;}//最低位1及其后面的0构成的数值
//int qmi(int a, int k, int p){int res = 1 % p;while (k){if (k & 1) res = Mod(res * a , p);a = Mod(a * a , p);k >>= 1;}return res;}
//int inv(int a,int mod){return qmi(a,mod-2,mod);}
//int lcm(int a,int b){return a*b/__gcd(a,b);}
int a, b, f[30][10][109], ans[10][2], dig[30];
int dfs(int pos, int idx, int cnt, bool lead, bool lim)
{
	if (!pos)	return cnt;
	if (!lim && !lead && f[pos][idx][cnt] != -1)	return f[pos][idx][cnt];


	int ans = 0, t = 0;
	int len = lim ? dig[pos] : 9;
	for (int i = 0; i <= len; i++)
	{
		if (i != idx)
			t = cnt;
		else 
		{
			if (lead && idx == 0)
				t = 0;
			else
				t = cnt + 1;
		}
		ans += dfs(pos - 1, idx, t, lead && i == 0, lim && i == len);
	}

	if (!lim && !lead)
		f[pos][idx][cnt] = ans;
	return ans;
}
void work(int x, int id)
{
	int idx = 0;
	while (x)
		dig[++idx] = x % 10, x /= 10;
	for (int i = 0; i <= 9; i++)
		ans[i][id] = dfs(idx, i, 0, 1, 1);
}
void solve()
{
	if (a > b)
		swap(a, b);
	work(a - 1, 0), work(b, 1);
	for (int i = 0; i <= 9; i++)
		cout << ans[i][1] - ans[i][0] << " \n"[i == 9];
}
signed main()
{
	buff;
	memset(f, -1, sizeof f);
	int _ = 1;
	// cin >> _;
	while (cin >> a >> b, a, b)
		solve();
}

到了这里,关于POJ - 2282 The Counting Problem(数位DP 计数问题)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • NC200204 数位 DP

    题意 传送门 NC200204 dh的帽子 题解 可以独立考虑每一位是否合法。维护数字上下界的状态,枚举三个数字,仅对合法的状态进行转移。数位 DP 求解即可,时间复杂度 O ( l o g R ) O(logR) O ( l o g R ) 。

    2024年02月16日
    浏览(28)
  • 「学习笔记」数位 DP

    意义不大的题不写了。 点击查看目录 目录 「学习笔记」数位 DP 概述 例题 P2657 [SCOI2009] windy 数 思路 代码 P4317 花神的数论题 思路 P4124 [CQOI2016]手机号码 思路 代码 haha数 题意 思路 代码 0和1的熟练 题意 思路 代码 苍与红的试炼 题意 思路 代码 数位 DP 一般用来解决「在一个较

    2023年04月09日
    浏览(26)
  • 数位dp。

    在处理1e9甚至1e18,1e100的问题时,因为在统计情况下有很多重复的计算,数位dp实现了相同状态只计算一次,从而大幅减少运算时间,思想就是对每一位进行dp,计算时记忆化每一位可以有的状态。 如我们在统计1234的状态时,可以拆成统计0~10000,0~2000,0~300,0~40数位统计 我们

    2024年02月01日
    浏览(22)
  • 数位DP万能模板

    ☆* o(≧▽≦)o *☆嗨~我是小奥🍹 📄📄📄个人博客:小奥的博客 📄📄📄CSDN:个人CSDN 📙📙📙Github:传送门 📅📅📅面经分享(牛客主页):传送门 🍹文章作者技术和水平有限,如果文中出现错误,希望大家多多指正! 📜 如果觉得内容还不错,欢迎点赞收藏关注哟!

    2024年01月17日
    浏览(27)
  • 动态规划——数位DP 学习笔记

    引入 数位 DP 往往都是这样的题型:给定一个区间 ([l, r]) ,求这个区间中满足某种条件的数的总数。 简单的暴力代码如下: 而当数据规模过大,暴力枚举就 (mathbb T) 飞了,因此引入数位 DP: 概念 数位(digit):对于十进制,即把一个数字按照个位、十位、百位等,一位

    2024年02月08日
    浏览(38)
  • LeetCode---121双周赛---数位dp

    2996. 大于等于顺序前缀和的最小缺失整数 2997. 使数组异或和等于 K 的最少操作次数 2998. 使 X 和 Y 相等的最少操作次数 2999. 统计强大整数的数目 简单的模拟题,只要按照题目的要求去写代码即可,代码如下 这题考异或的性质---相同为0,相异为1,我们只要关心nums的异或和与

    2024年01月22日
    浏览(38)
  • 周赛348(模拟、反向思维、数位DP)

    难度中等3 给你一个下标从 0 开始的字符串 s ,重复执行下述操作 任意 次: 在字符串中选出一个下标 i ,并使 c 为字符串下标 i 处的字符。并在 i 左侧 (如果有)和 右侧 (如果有)各 删除 一个距离 i 最近 的字符 c 。 请你通过执行上述操作任意次,使 s 的长度 最小化 。

    2024年02月07日
    浏览(61)
  • 异或三角形(数位dp)

    [Link](异或三角 - 蓝桥云课 (lanqiao.cn)) 参考:2021蓝桥杯国赛-J异或三角形-数位dp_塔子哥来了的博客-CSDN博客_蓝桥杯数位dp 给定 T T T 个数 n 1 , n 2 , . . . , n T n_1,n_2,...,n_T n 1 ​ , n 2 ​ , . . . , n T ​ ,对每个 n i n_i n i ​ 请求出有多少组 a , b , c a,b,c a , b , c 满足: 1 ≤ a , b , c ≤

    2024年02月09日
    浏览(30)
  • 「数位dp」统计整数数目(力扣第2719题)

    本题为1月16日力扣每日一题 题目来源:力扣第2719题 题目tag: 数位dp 动态规划 给你两个数字字符串num1和num2,以及两个整数max_sum和min_sum。如果一个整数x满足以下条件,我们称它是一个好整数: (num1 leq x leq num2) (min_sum leq digit_sum(x) leq max_sum) 请你返回好整数的数目。答案

    2024年01月16日
    浏览(30)
  • 算法竞赛备赛进阶之数位DP训练

    数位DP的思想就是对每一位进行DP,计算时记忆化每一位可以有的状态,其作用是减少运算时间,避免重复计算。 数位DP是一种计数用的DP,一般就是要统计一个区间[A,B]内满足一些条件数的个数。 以1e9甚至1e18、1e100的问题为例,因为在统计情况下有很多重复的计算,数位DP实

    2024年01月16日
    浏览(32)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包