基准时间限制: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. [LeetCode] 851. Loud and Rich_ Medium tag: DFS

    In a group of N people (labelled 0, 1, 2, ..., N-1), each person has different amounts of money, and ...

  2. 一些ios牛人的博客

    王巍的博客:王巍目前在日本横滨任职于LINE.工作内容主要进行Unity3D开发,8小时之外经常进行iOS/Mac开发.他的陈列柜中已有多款应用,其中番茄工作法工具非常棒.http://onevcat ...

  3. iOS设计规范HIG

    点击图标大小至少为这么大: Make it easy for people to interact with content and controls by giving each interacti ...

  4. unp第七章补充之TCP半开连接与半闭连接

    半打开(Half-Open)连接和半关闭(Half-Close)连接.TCP是一个全双工(Full-Duplex)协议,因此这里的半连接"半"字就是相对于全双工的"全&q ...

  5. time使用方法

  6. dedecms开启多站点

    dedecms开启多站点后,填写域名才能正确的地址 if ( ! function_exists('GetFileUrl')) { function GetFileUrl($aid,$typeid,$ ...

  7. centos添加epel源

    1. rpm -Uvh http://dl.fedoraproject.org/pub/epel/5/x86_64/epel-release-5-4.noarch.rpm    粗体部分需要根据自己的 ...

  8. #C++初学记录(算法考试1)

    B - Maximal Continuous Rest Each day in Berland consists of n hours. Polycarp likes time management. ...

  9. LR和SVM的相同和不同

    之前一篇博客中介绍了Logistics Regression的理论原理:http://www.cnblogs.com/bentuwuying/p/6616680.html. 在大大小小的面试过程中,经 ...

  10. Intermediate Python for Data Science learning 2 - Histograms

    Histograms from:https://campus.datacamp.com/courses/intermediate-python-for-data-science/matplotlib? ...