hdu 5179 beautiful number(数位dp)
题意:求[l,r]中高位%低位等于0的数字个数。(不含0)
分析:此题有三种方法。
1.暴搜,毕竟最多才10个位。
2.数位dp,预处理好整体的,再处理细节。
dp[i][j]表示第i位上的数字位j的情况数,dp[i][j]+=dp[i-1][k](j%k==0)
3.猜想这样的数字并不多,于是打表。
数位dp
#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <queue> #define LL long long using namespace std; int dp[][];
int a[];
void init(){
for(int i=;i<;i++) dp[][i]=;
for(int i=;i<;i++){
for(int j=;j<;j++){
for(int k=;k<;k++){
if(j%k==) dp[i][j]+=dp[i-][k];
}
}
}
} int solve(int x){
int len=;
int ans=;
while(x){
a[++len]=x%;
x/=;
}
for(int i=;i<len;i++){///先加上len-1位的情况
for(int j=;j<;j++)
ans+=dp[i][j];
}
for(int i=;i<a[len];i++) ans+=dp[len][i]; ///len位,且小于最高位数字的情况 for(int i=len-;i>=;i--){///已最高位的数字为基准,寻找符合条件的,再加上
for(int j=a[i]-;j>;j--){
if(a[i+]%j==) ans+=dp[i][j];
}
if(!a[i]) break;
if(a[i+]<a[i] || a[i+]%a[i]!=) break;
}
return ans;
} int main(){
init();
int t;
cin>>t;
while(t--){
int l,r;
cin>>l>>r;
cout<<solve(r+)-solve(l)<<endl;
}
return ;
}
打表
#include<iostream>//离线处理
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<cstring>
#include<stack>
#include<cmath>
#include<queue>
#define INF 0x0f0f0f0f
using namespace std;
int a[] = {
,,,,,,,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,
,,,,,,,,,,,,,,
,,,,,,,,,,,,,,
,,,,,,,,,,,,,,
,,,,,,,,,,,,,,
,,,,,,,,,,,,
,,,,,,,,,,,,
,,,,,,,,,,,,
,,,,,,,,,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,,,
,,,,,,,,
,,,,,,,,
,,,,,,,,
,,,,,,,,
,,,,,,,,
,,,,,,,,
,,,,,,,,
,,,,,,,,
,,,,,,,,
,,,,,,,,
,,,,,,,,
,,,,,,,,
,,,,,,,,
,,,,,,,,
,,,,,,,,
,,,,,,,,
,,,,,,,,
,,,,,,,,
,,,,,,,,
,,,,,,,,
,,,,,,,,
,,,,,,,,
,,,
};
int main()
{
int t;
cin >> t;
cout << a[] << endl;
while (t--)
{
int ans = ;
int l, r;
scanf("%d%d", &l, &r);
for (int i = ; i < ; i++)
{
if (a[i] >= l && a[i] <= r) ans++;
}
cout << ans << endl;
}
return ;
}
hdu 5179 beautiful number(数位dp)的更多相关文章
- HDU 5179 beautiful number 数位dp
题目链接: hdu: http://acm.hdu.edu.cn/showproblem.php?pid=5179 bc(中文): http://bestcoder.hdu.edu.cn/contes ...
- HDU 5179 beautiful number (数位dp / 暴力打表 / dfs)
beautiful number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu 5898 odd-even number 数位DP
传送门:hdu 5898 odd-even number 思路:数位DP,套着数位DP的模板搞一发就可以了不过要注意前导0的处理,dp[pos][pre][status][ze] pos:当前处理的位 ...
- beautiful number 数位DP codeforces 55D
题目链接: http://codeforces.com/problemset/problem/55/D 数位DP 题目描述: 一个数能被它每位上的数字整除(0除外),那么它就是beautiful nu ...
- hdu 5179 beautiful number
beautiful number 问题描述 令 A = \sum_{i=1}^{n}a_i * {10}^{n-i}(1\leq a_i \leq 9)A=∑i=1nai∗10n−i ...
- 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 ...
- HDU 3709 Balanced Number (数位DP)
Balanced Number Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) ...
- 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 ...
- HDU 5898 odd-even number (数位DP) -2016 ICPC沈阳赛区网络赛
题目链接 题意:一个数字,它每个数位上的奇数都形成偶数长度的段,偶数位都形成奇数长度的段他就是好的.问[L , R]的好数个数. 题解:裸的数位dp, 从高到低考虑每个数位, 状态里存下到当前位为止的 ...
随机推荐
- 【XSY1476】平凡之路 斜率优化DP
题目大意 有\(n\)个格子,一开始你在\(1\)号格子.每次你只能往编号更大的格子走.从第\(i\)个格子走到第\(j\)个格子的代价是\(a_i+a_j\times(j-i)\times m\) ...
- 手机Web 开发中图片img 如何等比例缩放
如果图片本身没有设置 width.height属性的话,只需要修改 max-width:100%; 就可以了 如果图片本身设置了 width.height属性的话,需要同时修改width 和heigh ...
- MT【274】一道漂亮的不等式题
已知$x_1^2+x_2^2+\cdots+x_6^2=6,x_1+x_2+\cdots+x_6=0,$证明:$x_1x_2\cdots x_6\le\dfrac{1}{2}$ 解答:显然只需考虑2个 ...
- 【BZOJ5289】[HNOI2018]排列(贪心)
[BZOJ5289][HNOI2018]排列(贪心) 题面 BZOJ 洛谷 题解 这个限制看起来不知道在干什么,其实就是找到所有排列\(p\)中,\(p_k=x\),那么\(k<j\),其中\( ...
- luogu1608 路径统计 (spfa)
题意:给一个有向图(无零边),要求找出最短路的数量(重边只计算一次) 做spfa的时候,记一个cnt对于u-w->v如果dis[u]+w=dis[v],cnt[v]+=cnt[u] 如果dis[ ...
- java 数组转字符串 字符串转数组
字符串转数组 使用Java split() 方法 split() 方法根据匹配给定的正则表达式来拆分字符串. 注意: . . | 和 * 等转义字符,必须得加 \\.多个分隔符,可以用 | 作为连字符 ...
- [Splay][学习笔记]
胡扯 因为先学习的treap,而splay与treap中有许多共性,所以会有很多地方不会讲的很细致.关于treap和平衡树可以参考这篇博客 关于splay splay,又叫伸展树,是一种二叉排序树,它 ...
- 为Druid监控配置访问权限(配置访问监控信息的用户与密码)
转: l 为Druid监控配置访问权限(配置访问监控信息的用户与密码) 2014-09-26 09:21:48 来源:renfufei的专栏 收藏 我要投稿 Druid是一 ...
- Struts2上传文件出错
出现错误: Error setting expression 'myFile' with value '[Ljava.lang.String;@47fb02e8' 解决方法: 这是由于没有设置 < ...
- Yii2的mongodb的聚合操作
最近项目使用到mongodb的聚合操作,但是yii文档中对这方面资料较少,记录下 $where['created_time'] = ['$gt' => "$start_date_str ...