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(m>0) sum += a[i-1];
13 else if(m==0) sum += n%a[i-1]+1;
14 if(n<a[i]) break;
15 }
16 return sum;
17 }
18
19 int main()
20 {
21 a[0] =1;
22 for(int i=1;i<15;i++)
23 a[i] =a[i-1]*10;
24 long long m,n;
25 while(cin>>m>>n){
26 if(m<0)
27 break;
28 if(n==0){
29 cout<<1<<endl;
30 continue;
31 }
32 long long res1 = solve(n);
33 long long res2 = solve(m-1);
34 //cout<<p<<endl;
35 //cout<<res1<<" "<<res2<<endl;
36 cout<<res1-res2<<endl;
37 }
38 return 0;
39 }
40 /**
41 if(n<0)
42 return 0;
43 long long low;
44 long long high;
45 long long cur;
46 p =1;
47 int tmp=0;
48 long long cnt =0;
49 while(n/p!=0){
50 tmp ++;
51 low = n-(n/p)*p;
52 high = n/(p*10)-1;
53 cur = (n/p)%10;
54 switch (cur){
55 case 0:
56 cnt += high*p+low+1;
57 break;
58 default:
59 cnt += (high+1)*p;
60 break;
61 }
62 p =p*10;
63 }
64 //for(int i=0;i<tmp;i++){
65 // cnt -= a[i];
66 //}
67 return cnt;
68 */

poj 3286 统计0的个数

时间: 2024-08-05 01:13:33

poj 3286 统计0的个数的相关文章

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&

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?(多少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(数位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 - 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

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> #

统计结尾0的个数

给一串数组,算出所有元素乘积的末尾0的个数 f1,f2 = 0, 0 L=[] for i in L: while i != 0 and (i % 2 == 0 or i % 5 == 0): if i % 2 == 0: f1 += 1 i /= 2 if i % 5 == 0: f2 += 1 i /= 5 print min(f1, f2) 求一下每一个之中的2,5成对出现的个数,成对一个就有一个10. 好笨的代码的说.

POJ 1236--Network of Schools【scc缩点构图 &amp;&amp; 求scc入度为0的个数 &amp;&amp; 求最少加几条边使图变成强联通】

Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13325   Accepted: 5328 Description A number of schools are connected to a computer network. Agreements have been developed among those schools: each school maintains a li

[ACM] POJ 3252 Round Numbers (一个区间内二进制中0的个数大于等于1的个数有多少个,组合)

Round Numbers Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8590   Accepted: 3003 Description The cows, as you know, have no fingers or thumbs and thus are unable to play Scissors, Paper, Stone' (also known as 'Rock, Paper, Scissors',