lightoj 1140 - How Many Zeroes?(数位DP)

news/2024/7/7 1:24:56

1140 - How Many Zeroes?

PDF (English)StatisticsForum
Time Limit: 2 second(s)Memory Limit: 32 MB

Jimmy writes down the decimal representations of all natural numbers between and including m and n, (m ≤ n). How many zeroes will he write down?

Input

Input starts with an integer T (≤ 11000), denoting the number of test cases.

Each case contains two unsigned 32-bit integers m and n, (m ≤ n).

Output

For each case, print the case number and the number of zeroes written down by Jimmy.

Sample Input

Output for Sample Input

5

10 11

100 200

0 500

1234567890 2345678901

0 4294967295

Case 1: 1

Case 2: 22

Case 3: 92

Case 4: 987654304

Case 5: 3825876150

 


SPECIAL THANKS: JANE ALAM JAN (DESCRIPTION, SOLUTION, DATASET)

简单数位DP

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[30];
ll dp[30][30];
ll dfs(int pos,int sta,int lead,int limit)
{
	if(pos==-1){
		return sta;
	}
	if(!limit&&!lead&&dp[pos][sta]!=-1) 
	return dp[pos][sta];
	int up=limit?a[pos]:9;
	ll tmp=0;
	for(int i=0;i<=up;i++)
	{

	if(lead==1&&i==0)     //前导0
	{
	tmp+=dfs(pos-1,sta,lead,limit&&i==up);
	}
	else 
	{ 
	if(i==0)                     
	tmp+=dfs(pos-1,sta+1,lead && i==0,limit&&i==up);
	else
	tmp+=dfs(pos-1,sta,lead && i==0,limit&&i==up);
	}
	}
	if(!limit&&!lead) 
	dp[pos][sta]=tmp;
	return tmp; 
}
 
ll solve(ll x)
{
    int pos=0;
    while(x)
    {
        a[pos++]=x%10;
        x/=10;
    }
    return dfs(pos-1,0,true,true);
}
int main()
{
	
    memset(dp,-1,sizeof(dp));
    int T;
    cin>>T;
    for(int i=1;i<=T;i++)
    {
	ll n,m;
	scanf("%lld%lld",&n,&m);
	if(n==0)
	printf("Case %d: %lld\n",i,solve(m)-solve(n-1)+1);//0是一个特殊情况
	else
	printf("Case %d: %lld\n",i,solve(m)-solve(n-1));
    }

    return 0;
}

 


http://www.niftyadmin.cn/n/3628387.html

相关文章

JS对象继承(6种)

原型链(父类的实例)借用构造函数(调用超类构造函数)组合继承(原型链和借用构造函数)原型式继承(借助原型基于已有的对象上创建新对象)寄生式继承(创建一个仅用于封装继承过程的函数)寄生组合式继承(解决父类属性重写)ECMAScript无法实现接口继承&#xff0c;只支持实现继承&…

第15周阅读程序(5)

/* *Copyright (c) 2016,烟台大学计算机学院 *All rights reserved. *文件名称 : *作 者 : 刘云 *完成日期 : 2016年6月7号 *版 本 号 : v6.0 * *问题描述 : 阅读程序 *输入描述 :无 *程序输出 : */#include<iterator> #include<algorithm> #include<functional…

Java的String字符串内容总结

String--字符串 获取字符串的长度 使用Sring类的length()方法可获取字符串对象的长度&#xff0c;例&#xff1a; str.length(); str代表指定的字符串对象;返回值为返回指定字符串的长度。例&#xff1a; 获取字符串中指定字符的索引位置 String类提供了indexOf()和lastIndexOf…

第15周阅读程序(6)

/* *Copyright (c) 2016,烟台大学计算机学院 *All rights reserved. *文件名称 : *作 者 : 刘云 *完成日期 : 2016年6月7号 *版 本 号 : v6.0 * *问题描述 : 阅读程序 *输入描述 :无 *程序输出 : */#include<string> #include<iostream> #include<map> using…

poj 3208 Apocalypse Someday 数位dp+二分答案

Apocalypse Someday Time Limit: 1000MS Memory Limit: 131072KTotal Submissions: 2203 Accepted: 1110Description The number 666 is considered to be the occult “number of the beast” and is a well used number in all major apocalypse themed blockbuster movies. …

emacs使用笔记

C-h t tutorial [移动基本操作]C-f C-b C-p C-n 前后上下 C-v C-a 行首 C-e行尾C-a 和 C-e 可以将光标移动到"一行"的头部和尾部。M-a 和 M-e 则将光标移动到“一句”的头部和尾部。M-f 向后移动一个单词 M-b 向前移动一个单词C-k 删除 C-v 下一页 M-v 上一页M-< …

red hat Linux配置ip

初次接触Linux &#xff0c;最开始的需要就是上网。配置IP成了学习使用Linux的第一个功课。 1.设置IP地址 查看你使用的IP网卡 ifconfig -a 找到你要使用的网卡 vi/etc/sysconfig/network-scripts/ifcfg-eth0 #IntelCorporation82801BA/BAM/CA/CAMEthernetController DEVICEet…

nyist 14 会场安排问题

以结束时间排序&#xff0c;一开始用了先按照开始时间排序&#xff0c;开始时间相同的按结束时间排&#xff0c;很荣幸的WA了&#xff0c;沉思了一会&#xff0c;用了以结束时间来排序&#xff0c;提交 AC了&#xff0c; 纠结了一会儿&#xff0c;这个。。。。。。。。。。 若以…