HDU3652 B-number —— 数位DP
题目链接:https://vjudge.net/problem/HDU-3652
B-number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7415 Accepted Submission(s): 4346
100
200
1000
1
2
2
题解:
求一段区间内既含有“13”也能被13整除的数的个数,数位DP。
1.dp[pos][status][num]:第pos位,状态为status(2为含有“13”, 1为不含有“13”但末尾为“1”, 0为什么都不满足),num为当前的数除以13的余数。
2.题目的其中一个要求是:该数能够被13整除,那么为什么在dp的数组中,只用记录:到当前位所形成的数除以13的余数呢?答:
比如 “14XXX” :
即当前位为4,上一位为1,后面位的数待定。所以到当前位为止,数字为14,那么14%13 = 1 。
由于当前位为第四位,即最小单元为1000,所以上面的 14%13 = 1 实际是: 14000 % 13000 = 1000。由于13000是13的倍数,故14000 除以 13, 至少能减少13000,剩下1000,这个1000只能到下一位才能继续处理了。也就是说: “14XXX” 和 “01XXX” 对于模13来说是等价的,因为“14XXX”可以减掉13000得到“01XXX”。
故:对于位置相同,且到当前位所形成的数字%13相等的状态,实际上可以归为一类。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int maxn = +; int a[maxn], dp[maxn][][]; int dfs(int pos, int status, int num, bool lim)
{
if(!pos) return (status==)&&(num==); //含有“13”,且能够被“13”整除
if(!lim && dp[pos][status][num]!=-) return dp[pos][status][num]; int ret = ;
int m = lim?a[pos]:;
for(int i = ; i<=m; i++)
{
int tmp_status;
if(status== || status== && i==) //含有“13”
tmp_status = ;
else if(i==) //前面的位不含有“13”, 但当前位(末尾)为“1"。(满足了一部分)
tmp_status = ;
else //前面的位不含有“13”,且当前位不是“1”。
tmp_status = ; // (num*10+i)%13:只需记录%13的余数,而不用记录完整的数
ret += dfs(pos-, tmp_status, (num*+i)%, lim&&(i==m));
} if(!lim) dp[pos][status][num] = ret;
return ret;
} int main()
{
int n;
memset(dp,-, sizeof(dp));
while(scanf("%d",&n)!=EOF)
{
int p = ;
while(n)
{
a[++p] = n%;
n /= ;
}
LL ans = dfs(p, , , );
printf("%d\n",ans);
}
}
HDU3652 B-number —— 数位DP的更多相关文章
- 多校5 HDU5787 K-wolf Number 数位DP
// 多校5 HDU5787 K-wolf Number 数位DP // dp[pos][a][b][c][d][f] 当前在pos,前四个数分别是a b c d // f 用作标记,当现在枚举的数小 ...
- hdu 5898 odd-even number 数位DP
传送门:hdu 5898 odd-even number 思路:数位DP,套着数位DP的模板搞一发就可以了不过要注意前导0的处理,dp[pos][pre][status][ze] pos:当前处理的位 ...
- codeforces Hill Number 数位dp
http://www.codeforces.com/gym/100827/attachments Hill Number Time Limits: 5000 MS Memory Limits: ...
- HDU 5787 K-wolf Number 数位DP
K-wolf Number Problem Description Alice thinks an integer x is a K-wolf number, if every K adjacen ...
- Fzu2109 Mountain Number 数位dp
Accept: 189 Submit: 461Time Limit: 1000 mSec Memory Limit : 32768 KB Problem Description One ...
- HDU 3709 Balanced Number (数位DP)
Balanced Number Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) ...
- beautiful number 数位DP codeforces 55D
题目链接: http://codeforces.com/problemset/problem/55/D 数位DP 题目描述: 一个数能被它每位上的数字整除(0除外),那么它就是beautiful nu ...
- FZU - 2109 Mountain Number 数位dp
Mountain Number One integer number x is called "Mountain Number" if: (1) x>0 and x is a ...
- BNU 13024 . Fi Binary Number 数位dp/fibonacci数列
B. Fi Binary Number A Fi-binary number is a number that contains only 0 and 1. It does not conta ...
- hdu 5898 odd-even number(数位dp)
Problem Description For a number,if the length of continuous odd digits is even and the length of co ...
随机推荐
- cf487C Prefix Product Sequence
Consider a sequence [a1, a2, ... , an]. Define its prefix product sequence . Now given n, find a per ...
- ElasticSearch 多索引
1.用逗号将索引隔开,如: $ curl -XPOST http://localhost:9200/aaa,website/_search/ { "took": 1, " ...
- Scrapy学习-4-Items类&Pipelines类
items类使用 作用 能使得我们非常方便的操作字段名 在items.py中定制我们的类 class ArticleItem(scrapy.Item): title = scrapy.Field() ...
- AC日记——草地排水 codevs 1993
1993 草地排水 USACO 时间限制: 2 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题解 题目描述 Description 在农夫约翰的农场上,每 ...
- AC日记——凌乱的yyy 洛谷 P1803
题目背景 快noip了,yyy很紧张! 题目描述 现在各大oj上有n个比赛,每个比赛的开始.结束的时间点是知道的. yyy认为,参加越多的比赛,noip就能考的越好(假的) 所以,他想知道他最多能参加 ...
- @font-face制作小图标的实践
1.为啥要用font-face制作小图标 1)适用性:一个图标字体要比一系列的图像要小,一旦字体图标加载完,图标则会立刻显示出来,不需要去下载一个图像. 2)可扩展性:可以使用font-size对图标 ...
- Centos7 下安装 RabbitMQ
安装 erlang 1.下载erlang 官网地址 http://www.erlang.org/download 挑选合适的版本 然后 wget 比如目前最新版本 19.3 运行命令 wget htt ...
- File类 文件过滤器
创建过滤器 package cn.zmh.File; import java.io.File; import java.io.FileFilter; // 实现FileFilter类的功能 publi ...
- 【转】 nginx rewrite 伪静态配置参数详细说明
nginx rewrite 伪静态配置参数和使用例子 附正则使用说明 正则表达式匹配,其中: * ~ 为区分大小写匹配 * ~* 为不区分大小写匹配 * !~和!~*分别为区分大小写不匹配及不区分 ...
- ReactNavtive框架教程(3)
原文:http://www.raywenderlich.com/99473/introducing-react-native-building-apps-javascript 注意:全部图片放在了百度 ...