基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题
 收藏
 关注
给出一段区间a-b,统计这个区间内0-9出现的次数。

 
比如 10-19,1出现11次(10,11,12,13,14,15,16,17,18,19,其中11包括2个1),其余数字各出现1次。
Input
两个数a,b(1 <= a <= b <= 10^18)
Output
输出共10行,分别是0-9出现的次数
Input示例
10 19
Output示例
1
11
1
1
1
1
1
1
1
1

题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1042

0的情况比较特殊,容易坑;

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-4
#define bug(x) cout<<"bug"<<x<<endl;
const int N=3e3+,M=1e6+,inf=;
const ll INF=1e18+,mod=;
ll f[][][][],bit[];
int k;
ll dp(int pos,int pre,int sum,int xx,int kk,int flag,int q)
{
if(pos==)return (sum==xx);
if(flag&&f[pos][sum][xx][pre]!=-)return f[pos][sum][xx][pre];
int x=flag?:bit[pos];
ll ans=;
for(int i=;i<=x;i++)
{
if(!q)
{
///cout<<pos<<" "<<i<<endl;
if(!i)ans+=dp(pos-1,i,sum,xx,kk,flag||i<x,q);
else if(i==kk)ans+=dp(pos-1,i,sum+1,xx,kk,flag||i<x,1);
else ans+=dp(pos-1,i,sum,xx,kk,flag||i<x,1);
}
else
{
///cout<<"xxxx "<<i<<endl;
if(i==kk)
ans+=dp(pos-,i,sum+,xx,kk,flag||i<x,);
else
ans+=dp(pos-,i,sum,xx,kk,flag||i<x,);
}
}
if(flag)f[pos][sum][xx][pre]=ans;
return ans;
}
ll getans(ll x,int k)
{
int len=;
while(x)
{
bit[++len]=x%;
x/=;
}
ll ans=;
for(int i=;i<=;i++)
ans+=dp(len,,,i,k,,)*i;
return ans;
}
int main()
{ ll l,r;
scanf("%lld%lld",&l,&r);
for(k=;k<=;k++)
{
memset(f,-,sizeof(f));
printf("%lld\n",getans(r,k)-getans(l-,k));
}
return ;
}
基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题
 收藏
 关注
给出一段区间a-b,统计这个区间内0-9出现的次数。

 
比如 10-19,1出现11次(10,11,12,13,14,15,16,17,18,19,其中11包括2个1),其余数字各出现1次。
Input
两个数a,b(1 <= a <= b <= 10^18)
Output
输出共10行,分别是0-9出现的次数
Input示例
10 19
Output示例
1
11
1
1
1
1
1
1
1
1

51nod 1042 数字0-9的数量 数位dp的更多相关文章

  1. 51nod 1042数字0-9的数量

    1042 数字0-9的数量  基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题  收藏  关注 给出一段区间a-b,统计这个区间内0-9出现的次数. 比如 10-19 ...

  2. 51nod 1009 - 数字1的数量 - [数位DP][模板的应用以及解释]

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1009 基准时间限制:1 秒 空间限制:131072 KB 给 ...

  3. 51nod 1042 数字0-9的数量

    给出一段区间a-b,统计这个区间内0-9出现的次数.   比如 10-19,1出现11次(10,11,12,13,14,15,16,17,18,19,其中11包括2个1),其余数字各出现1次. Inp ...

  4. 1009 数字1的数量 数位dp

    1级算法题就这样了,前途渺茫啊... 更新一下博客,我刚刚想套用数位dp的模板,发现用那个模板也是可以做到,而且比第二种方法简单很多 第一种方法:我现在用dp[pos][now]来表示第pos位数字为 ...

  5. BZOJ1833 [ZJOI2010]count 数字计数 【数学 Or 数位dp】

    题目 给定两个正整数a和b,求在[a,b]中的所有整数中,每个数码(digit)各出现了多少次. 输入格式 输入文件中仅包含一行两个整数a.b,含义如上所述. 输出格式 输出文件中包含一行10个整数, ...

  6. (数位DP)51NOD 1042 数字0-9的数量

    给出一段区间a-b,统计这个区间内0-9出现的次数. 比如 10-19,1出现11次(10,11,12,13,14,15,16,17,18,19,其中11包括2个1),其余数字各出现1次.   输入 ...

  7. 51nod 1009 数字1的数量 数位dp

    1009 数字1的数量 基准时间限制:1 秒 空间限制:131072 KB   给定一个十进制正整数N,写下从1开始,到N的所有正数,计算出其中出现所有1的个数.   例如:n = 12,包含了5个1 ...

  8. Poj-3286- How many 0's? - 【基础数位DP】

    How many 0's? Description A Benedict monk No.16 writes down the decimal representations of all natur ...

  9. 2018.07.26NOIP模拟 魔法数字(数位dp)

    魔法数字 题目背景 ASDFZ-NOIP2016模拟 题目描述 在数论领域中,人们研究的基础莫过于数字的整除关系.一般情况下,我们说整除总在两个数字间进行,例如 a | b(a能整除b)表示 b 除以 ...

随机推荐

  1. POJ:2049Finding Nemo(bfs+优先队列)

    http://poj.org/problem?id=2049 Description Nemo is a naughty boy. One day he went into the deep sea ...

  2. spring用注解简化bean配置

    组件扫描: <context:component-scan base-package="com"/> 容器启动后如果发现配置文件有上面的标签会自动扫描对应的包及子包,如 ...

  3. 多张图片合成一个tif

    可以利用ACDSEE6.0打开你要合成的多张图片,CTRL全部选中,打开工具--转化文件格式-选择格式tif---所有页----合并---

  4. Java jdbc 操作数据库详解

    原文地址https://www.cnblogs.com/huguodong/p/5910859.html JDBC(Java Data Base Connectivity,java数据库连接)是一种用 ...

  5. 实现对多维数组按照某个键值排序的两种方法(array_multisort和array_sort)

    实现对多维数组按照某个键值排序的两种解决方法(array_multisort和array_sort): 第一种:array_multisort()函数对多个数组或多维数组进行排序.    //对数组$ ...

  6. VMware Coding Challenge: The Heist

    类似BackpackII问题 static int maximize_loot(int[] gold, int[] silver) { int[][] res = new int[gold.lengt ...

  7. ts实战项目启动中遇到的问题

    项目链接:https://github.com/Jiasm/typescript-example 储备知识须知: sequelize入门篇 : 依照README执行以下操作: npm i brew s ...

  8. animation-play-state

    animation-play-state: css: .play-state{ -webkit-animation:f1 2s 0.5s infinite linear forwards altern ...

  9. JSON实例(单对象)

    实例一: using System; using System.Web.Script.Serialization; namespace JSonDemo { //实体类 public class Pe ...

  10. 代码静态检查Eclipse插件:SonarLint插件离线安装

    Eclipse Version: Oxygen.3a Release (4.7.3a)Myeclipse版本: 10.7 SonarLint 插件离线安装包:org.sonarlint.eclipse ...