POJ 3286- How many 0's?(组合数学_区间计数)

How many 0‘s?

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d
& %I64u

Submit Status Practice POJ
3286

Appoint description: 
System Crawler  (2015-04-18)

Description

A Benedict monk No.16 writes down the decimal representations of all natural numbers between and including m and nm ≤ n. How many 0‘s will he write down?

Input

Input consists of a sequence of lines. Each line contains two unsigned 32-bit integers m and nm ≤ n. The last line of input has the value of m negative and this line should not be processed.

Output

For each line of input print one line of output with one integer number giving the number of 0‘s written down by the monk.

Sample Input

10 11
100 200
0 500
1234567890 2345678901
0 4294967295
-1 -1

Sample Output

1
22
92
987654304
3825876150

题意:求区间内出现多少个零。和POJ 2282差不多。

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <set>
#include <queue>
#include <stack>
#include <map>
using namespace std;
typedef long long LL;
const int inf=0x3f3f3f3f;
const double pi= acos(-1.0);
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
LL b[12]={1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000,10000000000,100000000000};
LL get_res(LL n)
{
    int i;
    LL left,m;
    LL cnt=0;
    for(i=1;i<12;i++){
            left=n/b[i]-1;
        cnt+=left*b[i-1];
        m=(n%b[i]-n%b[i-1])/b[i-1];
        if(m>0)
            cnt+=b[i-1];
        else if(m==0)
            cnt+=n%b[i-1]+1;
        if(n<b[i])
            break;
    }
    return cnt;
}

int main()
{
    LL n,m;
    while(~scanf("%lld %lld",&n,&m)){
        if(n==-1&&m==-1) break;
        if(n>m) swap(n,m);
        printf("%lld\n",get_res(m)-get_res(n-1));
    }
    return 0;
}

POJ 3286- How many 0's?(组合数学_区间计数)

时间: 2024-10-22 04:17:18

POJ 3286- How many 0's?(组合数学_区间计数)的相关文章

POJ 2282-The Counting Problem(组合数学_区间计数)

The Counting Problem Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2282 Appoint description:  System Crawler  (2015-04-15) Description Given two integers a and b, we write the numbers between

POJ - 3286 - How many 0&#39;s? 【数位DP】

How many 0's? Time Limit: 1000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Description A Benedict monk No.16 writes down the decimal representations of all natural numbers between and including m and n, m ≤ n. How many 0's will he writ

TOJ 2294 POJ 3286 How many 0&#39;s? 数位dp

http://acm.tju.edu.cn/toj/showp2294.html http://poj.org/problem?id=3284 题外话:集训结束,回学校了.在宿舍看了这题,没什么好想法,去洗澡了.转了两个澡堂都特么没开..倒是在路上把这题想了.用回自己的电脑,不得不说苹果的字体渲染,真心高一个等级. 题意:给定两个数a和b,从a写到b,问一共写了多少个0. 分析:当然先转化为求0..a写多少个0.网上有更简单的做法,就是枚举每位作为0,则如果这一位本来是0,左边取1..a-1(不

poj 3286 How many 0&#39;s? 按位统计

题意: 给m<=n,求从m写到n,一共写多少个0. 分析: 按位算当某位是0时左边有多少种情况,右边有多少种情况,注意左边的情况数为-1时(这时遍历到最高位)是为了把右边多加的情况减去,也就是把0作为开头时的情况减去. 代码: //poj 3286 //sep9 #include<iostream> using namespace std; typedef __int64 ll; ll b[16]; ll f(ll n) { ll left,m,ans=0; for(int i=1;i&

POJ 3286 How many 0&#39;s?(多少0?)

POJ 3286 How many 0's?(多少0?) Time Limit: 1000MS   Memory Limit: 65536K [Description] [题目描述] A Benedict monk No.16 writes down the decimal representations of all natural numbers between and including m and n, m ≤ n. How many 0's will he write down? 一个

POJ 3286 How many 0&#39;s?

题目大意: 计算[m,n]之间所有数字有多少个零. 解题思路: 可以用[0,m)之间和[0,n]之间有多少个零然后作差. 规律是计算所有位置在到当前数时有多少个零. 下面是代码:   #include <set> #include <map> #include <queue> #include <math.h> #include <vector> #include <string> #include <stdio.h> #

POJ 3286 How many 0&#39;s(数位DP模板)

题目链接:http://poj.org/problem?id=3286 题目大意: 输入n,m,求[n,m]的所有数字中,0出现的总数是多少,前导零不算. 解题思路: 模板题,设dp[pos][num],pos为数位,num为当前0的数目,然后套数位DP模板即可. 还有之前的一些思考: 关于数位DP求0时,dp下标记录num有什么作用,num不是与后面的0的个数无关吗?是的,在(!limit&&!lead)的情况下,前面有多少0是不影响后面可以出现多少0的.但是,比如说dp[pos][nu

poj 3286 统计0的个数

1 #include <iostream> 2 3 using namespace std; 4 long long p; 5 long long a[20]; 6 long long solve(long long n){ 7 long long left,m,sum =0; 8 for(int i=1;i<12;i++){ 9 left = n/a[i]-1; 10 sum += left*a[i-1]; 11 m = (n%a[i]-n%a[i-1])/a[i-1]; 12 if(

POJ 2282 The Counting Problem,组合数学

POJ 2282 The Counting Problem,组合数学 ACM 题目地址:POJ 2282 题意: 给出俩数n,m,求从n~m中0~9分别出现的次数. 分析: 组合数学. 只要能快速算出0~n中各个数的出现次数就能解决问题了. 要把数拆开来看,比如3456=3000+400+50+6. 然后就只要考虑后面都是0的数就行了. 0~3000中,我们要分为两部分来考虑: 在第一位中,0\1\2都出现了1000次. 假设不管第一位,后面那些位数出现0~9的几率是均等的(先不考虑前导0).那