首页 技术 正文
技术 2022年11月18日
0 收藏 521 点赞 3,567 浏览 927 个字

题目链接:

  Hdu 2089 不要62

题目描述:

  给一个区间 [L, R] ,问区间内不含有4和62的数字有多少个?

解题思路:

  以前也做过这个题目,但是空间复杂度是n。如果数据范围太大就GG了。今天看了一下数位DP,的确有时间和空间上的优越性。
  用数位dp做这个题目的时候,首先要预处理出dp[x][y],代表以y开头的x位数中不含有62和4的数有多少个,在满足条件的情况下状态转移为:dp[x][y] += dp[x-1][k]。又因为对于一个数字x,如果和它位数相同的数字y小于x,那么只需要y数字其中任意一位小于x即可。然后问题就变为求ans([1, r+1]) – ans([1, l])。

 #include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; const int maxn = ;
int dp[maxn][maxn];
//dp[x][y] 以y开头的x位数满足题意的个数 void init ()
{
memset(dp, , sizeof(dp));
dp[][] = ;
for (int i=; i<=; i++)//i位数
for (int j=; j<; j++)//第i位数
for (int k=; k<; k++)//第i-1位数
{
if(j== || j==&&k==)
continue ;
dp[i][j] += dp[i-][k];
}
} int solve (int n)
{
int a[maxn], len = , ans = ;
while (n)
{
a[++len] = n % ;
n /= ;
}
a[len+] = ; for (int i=len; i>; i--)
{//枚举后len位的策略
for (int j=; j<a[i]; j++)
{
if (j== || a[i+]==&&j==)
continue ;
ans += dp[i][j];
} if (a[i]== || a[i+]== && a[i]==)
break;
}
return ans;
} int main ()
{
int n, m;
init (); while (scanf ("%d %d", &n, &m), n + m)
printf ("%d\n", solve(m+) - solve(n));
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,104
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,580
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,428
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,835
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,918