UVa 1640 The Counting Problem (数学,区间计数)
题意:给定两个数m, n,求从 m 到 n 中0-9数字各出现了多少次。
析:看起来挺简单的,其实并不好做,因为有容易想乱了。主要思路应该是这样的,分区间计数,先从个位进行计,一步一步的计算过来。都从0开始,最后用大数减小数的即可。
举个例子吧,容易理解。比如0-1234。
先计算个位数字,有1-4,然后计算123各出现了5次,注意是这里是5次,不是4次,因为我们要加上那个0,然后就剩下那个1230了,我们想那么现在个位数从开始到这,
重复了123次,然后再进行下一位,依次进行,直到0.
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <functional>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <deque>
#include <map>
#include <cctype>
#include <stack>
#include <sstream>
#include <cstdlib>
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;
#include <ctime> typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 0x3f3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e5 + 5;
const int mod = 1e9 + 7;
const char *mark = "+-*";
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
int n, m;
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
int ans[15]; void dfs(int n, int m, int ok){
if(-1 == n) return ;
int x = n / 10;
int y = n % 10;
for(int i = 1; i <= y; ++i) ans[i] += ok * m;
for(int i = 0; i < 10; ++i) ans[i] += ok * m * x;
int tmp = x;
while(tmp){
ans[tmp%10] += ok * (y+1) * m;
tmp /= 10;
}
dfs(x-1, m*10, ok);
} int main(){
while(scanf("%d %d", &m, &n) == 2){
if(!m && !n) break;
if(m < n) swap(m, n);
--n;
memset(ans, 0, sizeof ans);
dfs(m, 1, 1);
dfs(n, 1, -1); for(int i = 0; i < 10; ++i){
if(i) putchar(' ');
printf("%d", ans[i]);
}
printf("\n");
}
return 0;
}
UVa 1640 The Counting Problem (数学,区间计数)的更多相关文章
- 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] ...
- UVA.1640.The Counting Problem / BZOJ.1833.[ZJOI2010]数字计数(数位DP)
题目链接 \(Description\) 求\([l,r]\)中\(0,1,\cdots,9\)每个数字出现的次数(十进制表示). \(Solution\) 对每位分别DP.注意考虑前导0: 在最后统 ...
- UVa 1640 - The Counting Problem(数论)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVA 1640 The Counting Problem
https://vjudge.net/problem/UVA-1640 题意:统计区间[l,r]中0——9的出现次数 数位DP 注意删除前导0 #include<cmath> #inclu ...
- 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] 对于 ...
- UVA - 1640 The Counting Problem (数位dp)
题意:统计l-r中每种数字出现的次数 很明显的数位dp问题,虽然有更简洁的做法但某人已经习惯了数位dp的风格所以还是选择扬长避短吧(说白了就是菜啊) 从高位向低位走,设状态$(u,lim,ze)$表示 ...
- Codeforces ECR86 C. Yet Another Counting Problem(规律,区间)
题意:给你两个正整数a和b,询问q次,每次给你一个区间[l,r],问[l,r]中有多少数字满足:x%a%b!=a%b%a. 题解:看公式无从下手的题,一般都是要找规律的.首先,我们知道,假如x%a%b ...
- The Counting Problem
The Counting Problem 询问区间\([a,b]\)中\(1\sim 9\)出现的次数,0 < a, b < 100000000. 解 显然为数位递推,考虑试填法,现在关键 ...
- POJ2282 The Counting Problem
题意 Language:DefaultEspañol The Counting Problem Time Limit: 3000MS Memory Limit: 65536K Total Submis ...
随机推荐
- Android仿iPhone晃动撤销输入功能(微信摇一摇功能)
重力传感器微信摇一摇SensorMannager自定义alertdialogSensorEventListener 很多程序中我们可能会输入长文本内容,比如短信,写便笺等,如果想一次性撤销所有的键入内 ...
- parseInt和valueOf
.parseInt和valueOf.split static int parseInt(String s) 将字符串参数作为有符号的十进制整数进行分析. static Integer valueOf( ...
- js风格技巧
1.一个页面的所有js都可以写成这样,比如: var index ={}; index.User = ****; index.Init = function(){ $("$tes ...
- hMailserver设置外部反病毒扫描程序
刚在5dmail上发现有人提出一个问题,他在hmailserver的外部病毒扫描程序中使用了瑞星那个娱乐货,结果呢,说瑞星太勇猛了,所有附件都认为病毒了,这是怎么个情况呢? 先从hmailadmin里 ...
- bzoj1054: [HAOI2008]移动玩具
hash+bfs:要注意特殊情况.(似乎连sort.lower_bound都不用数据小直接判重了... #include<cstdio> #include<cstring> # ...
- UrlRewriter.dll伪静态实现二级域名泛解析
大家应该知道,微软的URLRewrite能够对URL进行重写,但是也只能对域名之后的部分进行重写,而不能对域名进行重写, 如:可将 http://http://www.115sou.com/qq/ ...
- 一天一个Java基础——泛型
这学期的新课——设计模式,由我仰慕已久的老师传授,可惜思维过快,第一节就被老师挑中上去敲代码,自此在心里烙下了阴影,都是Java基础欠下的债 这学期的新课——算法设计与分析,虽老师不爱与同学互动式的讲 ...
- 【转】【Android】对话框 AlertDialog -- 不错不错
原文网址:http://blog.csdn.net/feng88724/article/details/6171450 本讲介绍一下Android基本组件:对话框AlertDialog. API: j ...
- 常用UI布局
1.LinearLayout(线性布局):将布局所包含的控件在线性方向上依次排列. <1>android:orientation 指定了排列方向(垂直方向(vertical).水平方向(h ...
- iOS - GIF图的完美拆解、合成、显示
转:http://blog.csdn.net/marujunyy/article/details/14455699 最近由于项目需要,需要先把gif图拆解开,然后在每一张图片上添加一些图片和文字,最后 ...