LightOJ 1140 How Many Zeroes
题意:写出一个给定区间的每个数,求出一共写了多少个零。
解法:数位DP,定义dp[len][flag][num]:len的定义为数位的长度,flag定义为前导0和没有前导0的两种状态,num定义为写的满足条件的0的个数。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#define inf 0x7fffffff
#define exp 1e-10
#define PI 3.141592654
using namespace std;
typedef long long LL;
LL digit[];
LL dp[][][];
LL dfs(LL len,bool flag,LL p,LL p2,bool fp)
{
if (!len)
{
if (!flag)
return p2-p;
else return ;
}
if (!fp && dp[len][flag][p2-p]!=-) return dp[len][flag][p2-p];
LL ret=;
LL fpmax= fp ? digit[len] : ;
for (LL i= ;i<=fpmax ;i++)
{
bool ok= i== ? true : false;
LL temp;
if (i==) temp= ; else temp= ;
ret += dfs(len-,flag&&ok,flag?p+temp:p ,p2+temp,fp&&i==fpmax);
}
if (!fp) return dp[len][flag][p2-p]=ret;
return ret;
}
LL f(LL n)
{
if (n==-) return -;
if (n==) return ;
LL len=;
while (n)
{
digit[++len]=n%;
n /= ;
}
return dfs(len,true,,,true);
}
int main()
{
int t;
int ncase=;
cin>>t;
while (t--)
{
LL a,b;
scanf("%lld%lld",&a,&b);
memset(dp,-,sizeof(dp));
printf("Case %d: %lld\n",ncase++,f(b)-f(a-));
}
return ;
}
LightOJ 1140 How Many Zeroes的更多相关文章
- lightoj 1140 - How Many Zeroes?(数位dp)
Jimmy writes down the decimal representations of all natural numbers between and including m and n, ...
- LightOJ 1140 How Many Zeroes? (数位DP)
题意:统计在给定区间内0的数量. 析:数位DP,dp[i][j] 表示前 i 位 有 j 个0,注意前导0. 代码如下: #pragma comment(linker, "/STACK:10 ...
- How Many Zeroes? LightOJ - 1140
#include<stdio.h> #include<string.h> #include<math.h> #include<time.h> #incl ...
- 数位dp(D - How Many Zeroes? LightOJ - 1140 )
题目链接:https://cn.vjudge.net/contest/278036#problem/D 题目大意:T组测试数据,每一次输入两个数,求的是在这个区间里面,有多少个0,比如说19203包括 ...
- LightOJ 1140: How Many Zeroes? (数位DP)
当前数位DP还不理解的点: 1:出口用i==0的方式 2:如何省略状态d(就是枚举下一个数的那个状态.当然枚举还是要的,怎么把空间省了) 总结: 1:此类DP,考虑转移的时候,应当同时考虑查询时候的情 ...
- Light oj 1140 How Many Zeroes?
Jimmy writes down the decimal representations of all natural numbers between and including m and n, ...
- light oj 1140 - How Many Zeroes? 数位DP
思路:dp[i][j]:表示第i位数,j表示是否有0. 代码如下: #include<iostream> #include<stdio.h> #include<algor ...
- LightOJ 1140 计数/数位DP 入门
题意: 给出a,b求区间a,b内写下过多少个零 题解:计数问题一般都会牵扯到数位DP,DP我写的少,这道当作入门了,DFS写法有固定的模板可套用 dp[p][count] 代表在p位 且前面出现过co ...
- LightOJ - 1140 统计0的数位 数位DP
注意以下几点: 搜索维度非约束条件的都要记录,否则大概率出错,比如_0 st参数传递和_0的互相影响要分辨清楚 num==-1就要返回0而不是1 #include<iostream> #i ...
随机推荐
- centos6.7下编译安装lnmp
很多步骤不说明了,请参照本人的centos6.7下编译安装lamp,这次的架构是nginx+php-fpm一台服务器,mysql一台服务器 (1)首先编译安装nginx: 操作命令: yum -y g ...
- Qt使用默认浏览器打开网页
#include <QDesktopServices> #include <QUrl> QDesktopServices::openUrl(QUrl("http:// ...
- C++判断对称三位数素数
题目内容:判断一个数是否为对称三位数素数.所谓“对称”是指一个数,倒过来还是该数.例如,375不是对称数,因为倒过来变成了573. 输入描述:输入数据含有不多于50个的正整数(0<n<23 ...
- ContactsContract中涉及数据库中的一些列属性值【Written By KillerLegend】
ContactsContract.Data http://developer.android.com/reference/android/provider/ContactsContract.Dat ...
- openshift云计算平台diy模式安装Python2.7+Flask
主要翻译了链接1)的教程,加上一些个人研究,步骤如下: 1) 在openshift.redhat.com申请账号,安装git for windows,然后安装gem install rhc,这些比较容 ...
- 自学Python三 Python中的屠龙刀(续)
装饰器: 在函数代码功能运行期间动态增加功能的方式叫做装饰器(Decorator).它对一个函数或者类进行再加工. 我们先定义两个函数,一个计算两数和,一个计算两数差. >>> de ...
- Linux源代码情景分析读书笔记 物理页面的分配
函数 alloc_pages流程图
- Python 3.5.2建立与DB2的连接
Python是可以连接数据库,并从数据库获取相应的数据库的,但是怎么连接呢? 这是个问题,以下是我使用Python建立数据库连接的步骤(我使用的工具为:PyCharm) 1.首先下载setuptool ...
- 配置DB2的数据库ODBC连接
打开cmd窗口,输入db2cmd启动db2 clp窗口 输入db2 list node directory查看是否有数据库需要连接的节点 如果不存在,则 节点编目:db2 catalog tcpip ...
- viewPager+Handler+Timer简单实现广告轮播效果
基本思想是在Avtivity中放一个ViewPager,然后通过监听去实现联动效果,代码理由详细的解释,我就不说了. MainActivity.java package com.example.adm ...