链接:

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4515

题意:

给出整数a、b,统计a和b(包含a和b)之间的整数中,数字0,1,2,3,4,5,6,7,8,9分别出现了多少次。1≤a,b≤1e8。

分析:

解决这类题目的第一步一般都是:令f(n,d)表示0~n中数字d出现的次数,则所求的就是f(b,d)-f(a-1,d)。
例如,要统计0~234中4的个数,可以分成几个区间:

范围                    模板集
0~9                    *
10~99                **
100~199            1**
200~229            20*,21*,22*
230~234            230,231,232,233,234

上表中的“模板”指的是一些整数的集合,其中字符“*”表示“任意字符”。例如,1**表示以1开头的任意3位数。
因为后两个数字完全任意,所以“个位和十位”中每个数字出现的次数是均等的。
换句话说,在模板1**所对应的100个整数的200个“个位和十位”数字中,0~9各有20个。
而这些数的百位总是1,因此得到:模板1**对应的100个整数包含数字0,2~9各20个,数字1有120个。
这样,只需把0~n分成若干个区间,算出每个区间中各个模板所对应的整数包含每个数字各多少次,就能解决原问题了。

代码:

 #include <cstdio>
#include <cstring> const int UP = ;
int pow10[UP], amt[UP]; int f(int n, int d) {
int res = ;
char s[];
sprintf(s, "%d", n);
int len = strlen(s); for(int i = ; i < len; i++) {
if(i == ) res++;
else {
res += * amt[i-];
if(d > ) res += pow10[i-];
}
} int pre = ;
for(int i = ; i < len; i++) {
int L = , R = s[i]-'';
if(i == && len > ) L = ;
for(int digit = L; digit < R; digit++) {
res += amt[len--i] + pre * pow10[len--i];
if(digit == d) res += pow10[len--i];
}
if(s[i]-'' == d) pre++;
}
return res + pre;
} int main() {
pow10[] = ;
for(int i = ; i < UP; i++) {
pow10[i] = pow10[i-] * ;
amt[i] = pow10[i] * i / ;
}
int a, b;
while(scanf("%d%d", &a, &b) && a) {
if(a > b) b += a, a = b-a, b -= a;
printf("%d", f(b,) - f(a-,));
for(int i = ; i < ; i++) printf(" %d", f(b,i) - f(a-,i));
printf("\n");
}
return ;
}

UVa 1640 - The Counting Problem(数论)的更多相关文章

  1. UVA 1640 The Counting Problem UVA1640 求[a,b]或者[b,a]区间内0~9在里面各个数的数位上出现的总次数。

    /** 题目:UVA 1640 The Counting Problem UVA1640 链接:https://vjudge.net/problem/UVA-1640 题意:求[a,b]或者[b,a] ...

  2. UVA 1640 The Counting Problem

    https://vjudge.net/problem/UVA-1640 题意:统计区间[l,r]中0——9的出现次数 数位DP 注意删除前导0 #include<cmath> #inclu ...

  3. UVa 1640 The Counting Problem (数学,区间计数)

    题意:给定两个数m, n,求从 m 到 n 中0-9数字各出现了多少次. 析:看起来挺简单的,其实并不好做,因为有容易想乱了.主要思路应该是这样的,分区间计数,先从个位进行计,一步一步的计算过来.都从 ...

  4. UVA.1640.The Counting Problem / BZOJ.1833.[ZJOI2010]数字计数(数位DP)

    题目链接 \(Description\) 求\([l,r]\)中\(0,1,\cdots,9\)每个数字出现的次数(十进制表示). \(Solution\) 对每位分别DP.注意考虑前导0: 在最后统 ...

  5. UVA 1640 The Counting Problem(按位dp)

    题意:给你整数a.b,问你[a,b]间每个数字分解成单个数字后,0.1.2.3.4.5.6.7.8.9,分别有多少个 题解:首先找到[0,b]与[0,a-1]进行区间减法,接着就只是求[0,x] 对于 ...

  6. UVA - 1640 The Counting Problem (数位dp)

    题意:统计l-r中每种数字出现的次数 很明显的数位dp问题,虽然有更简洁的做法但某人已经习惯了数位dp的风格所以还是选择扬长避短吧(说白了就是菜啊) 从高位向低位走,设状态$(u,lim,ze)$表示 ...

  7. UVA 11490 - Just Another Problem(数论)

    11490 - Just Another Problem option=com_onlinejudge&Itemid=8&page=show_problem&category= ...

  8. UVa 1363 Joseph's Problem (数论)

    题意:给定 n,k,求 while(i <=n) k % i的和. 析:很明显是一个数论题,写几个样例你会发现规律,假设 p = k / i.那么k  mod i = k - p*i,如果 k ...

  9. uva 11401 Triangle Counting

    // uva 11401 Triangle Counting // // 题目大意: // // 求n范围内,任意选三个不同的数,能组成三角形的个数 // // 解题方法: // // 我们设三角巷的 ...

随机推荐

  1. 时间格式转换成JUN.13,2017

    SimpleDateFormat sdf = new SimpleDateFormat("MMM.dd,yyyy", Locale.ENGLISH); String negotia ...

  2. 禁止选中页面内容-兼容ie、firefox、chrome

    使用js禁止用户选中网页上的内容,IE及Chrome下的方法一样.使用onselectstart, 比如: 在body中加入<body onselectstart="return fa ...

  3. golang数组与切片

    golang中坑的慢慢踩! golang中的数组是值类型,函数调用是传入的是数组的拷贝,如果想改变数组的值,可考虑使用指针数组,即函数调用时传入数组的地址 golang中的切片是引用类型,但是在函数中 ...

  4. swagger api文档添加jwt授权配置

    最近写的swagger文档,要加jwt授权,所以几经google终于搞定了,简简单单几行配置如下: securityDefinitions: APIKey: type: apiKey name: Au ...

  5. layui使用 ——父,子页面传值

    页面传值是非常常用的,layui自带弹窗功能,但是内置使用的是location.href 暂时没找到方法条件请求头,所以在后台需要放开拦截器, layer.open({ type : 2, title ...

  6. mongdb启动报错

    2018-08-19T12:25:31.707+0800 I CONTROL [main] Automatically disabling TLS 1.0, to force-enable TLS 1 ...

  7. tomcat-虚拟目录的映射

    虚拟目录的映射 1.新建 ${tomcat安装目录}\conf\Catalina\localhost\xxx.xml 文件. 文件内容: <Context  path="/xxx&qu ...

  8. mysql中LOCATE和CASE WHEN...THEN...ELSE...END结合用法

    之前项目中需要写一个sql,就是查出某个调研详情中,选A答案,B答案,C答案...F答案的人各有多少人,这个sql也是费了很大的力气才写出来,故记下来,方便以后使用. 其中tbl_research_i ...

  9. C# 后台解析json,简单方法 字符串序列化为对象,取值

    如果后台是一个JSON的字符串格式如下: string str = "{\"Success\":true,\"Msg\":\"成功!\&qu ...

  10. EventTarge Node Docuement Element HTMLElement 关系

    综述: 可以将其看做是依次继承的关系: Node Node A Node is an interface from which a number of DOM types inherit, and a ...