beautiful number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 801    Accepted Submission(s): 518

Problem Description
Let A=∑ni=1ai∗10n−i(1≤ai≤9)(n is the number of A's digits). We call A as “beautiful number” if and only if a[i]≥a[i+1] when 1≤i<n and a[i] mod a[j]=0 when 1≤i≤n,i<j≤n(Such as 931 is a "beautiful number" while 87 isn't).
Could you tell me the number of “beautiful number” in the interval [L,R](including L and R)?
 
Input
The fist line contains a single integer T(about 100), indicating the number of cases.
Each test case begins with two integers L,R(1≤L≤R≤109).
 
Output
For each case, output an integer means the number of “beautiful number”.
 
Sample Input
2
1 11
999999993 999999999
 
Sample Output
10
2
 
Source
 
Recommend
hujie

题意:

  要你输出 [L, R] 范围内的满足高位大于等于地位而且高位mod低位的数要等于0 的数的个数。

题解:

  1)离线暴力打表

    我们跑[1,1e9] 的所有满足这个条件的数。

    

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <set>
using namespace std;
typedef long long LL;
typedef unsigned long long uLL;
#define ms(a, b) memset(a, b, sizeof(a))
#define pb push_back
#define mp make_pair
#define eps 0.0000000001
const LL INF = 0x7fffffff;
const int inf = 0x3f3f3f3f;
const int mod = ;
const int maxn = +;
int a[] ={,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,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ios::sync_with_stdio();cin.tie();
int t;
cin >> t;
while(t--){
int L, R;
cin >> L >> R;
int ans = ;
for(int i=;i<;i++){
if(a[i]>=L&&a[i]<=R) ans++;
}
cout << ans << endl;
}
return ;
}

  2)dfs

    最大也就10个位,暴力dfs每一个位。

  

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <set>
using namespace std;
typedef long long LL;
typedef unsigned long long uLL;
#define ms(a, b) memset(a, b, sizeof(a))
#define pb push_back
#define mp make_pair
#define eps 0.0000000001
const LL INF = 0x7fffffff;
const int inf = 0x3f3f3f3f;
const int mod = ;
const int maxn = +;
int L, R;
int ans = ;
void dfs(LL num, int pre)
{
if(num>R) return;
if(num>=L) ans++;
for(int i = ;i<=pre;i++){
if(pre%i==)
dfs(num*+i, i);
}
}
int main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
ios::sync_with_stdio();cin.tie();
int t;
cin >> t;
while(t--){
ans = ;
cin >> L >> R;
for(int i = ;i<=;i++){
dfs(i, i);
}
cout << ans << endl;
}
return ;
}

  3)数位dp

    跟dfs很像,但是加上了记忆话搜索。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <set>
using namespace std;
typedef long long LL;
typedef unsigned long long uLL;
#define ms(a, b) memset(a, b, sizeof(a))
#define pb push_back
#define mp make_pair
#define eps 0.0000000001
const LL INF = 0x7fffffff;
const int inf = 0x3f3f3f3f;
const int mod = ;
const int maxn = +;
int L, R;
int ans = ;
int a[];
int dp[][];
int dfs(int pos, bool lead, bool limit, int pre)
{
if(pos == ) return ;
if(!limit&&!lead&&dp[pos][pre]!=-) return dp[pos][pre];
int up = limit?a[pos]:;
LL ans = ;
for(int i = ;i<=up;i++){
if(lead||pre>=i&&i!=&&pre%i==){
ans +=dfs(pos-, lead&&i==, limit&&i==a[pos], i);
}
}
if(!limit&&!lead) dp[pos][pre] = ans;
return ans;
}
int solve(int x)
{
int len = ;
while(x){
a[++len] = x%;
x/=;
}
return dfs(len, true, true, );
}
int main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
ios::sync_with_stdio();cin.tie();
int t;
cin >> t;
ms(dp, -);
while(t--){
cin >> L >> R;
cout << solve(R) - solve(L-) << endl;
}
return ;
}

HDU 5179 beautiful number (数位dp / 暴力打表 / dfs)的更多相关文章

  1. HDU 5179 beautiful number 数位dp

    题目链接: hdu: http://acm.hdu.edu.cn/showproblem.php?pid=5179 bc(中文): http://bestcoder.hdu.edu.cn/contes ...

  2. hdu 5179 beautiful number(数位dp)

    原题链接 题意:求[l,r]中高位%低位等于0的数字个数.(不含0)分析:此题有三种方法.1.暴搜,毕竟最多才10个位.2.数位dp,预处理好整体的,再处理细节. dp[i][j]表示第i位上的数字位 ...

  3. hdu 5898 odd-even number 数位DP

    传送门:hdu 5898 odd-even number 思路:数位DP,套着数位DP的模板搞一发就可以了不过要注意前导0的处理,dp[pos][pre][status][ze] pos:当前处理的位 ...

  4. beautiful number 数位DP codeforces 55D

    题目链接: http://codeforces.com/problemset/problem/55/D 数位DP 题目描述: 一个数能被它每位上的数字整除(0除外),那么它就是beautiful nu ...

  5. hdu 5179 beautiful number

    beautiful number 问题描述 令 A = \sum_{i=1}^{n}a_i * {10}^{n-i}(1\leq a_i \leq 9)A=∑​i=1​n​​a​i​​∗10​n−i​ ...

  6. 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 ...

  7. HDU 3709 Balanced Number (数位DP)

    Balanced Number Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) ...

  8. 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 ...

  9. HDU 5898 odd-even number (数位DP) -2016 ICPC沈阳赛区网络赛

    题目链接 题意:一个数字,它每个数位上的奇数都形成偶数长度的段,偶数位都形成奇数长度的段他就是好的.问[L , R]的好数个数. 题解:裸的数位dp, 从高到低考虑每个数位, 状态里存下到当前位为止的 ...

随机推荐

  1. 关于存储过程的一些sql

    1.关于事务的回滚Set XACT_ABORT ON; 开启为on时 ,如果事务语句产生运行错误 ,将整个事务终止进行回滚,Off时只回滚产生错误的语句. 2.获取事务语句中上一次插入值的行号@@ID ...

  2. 项目被os x占用

    xattr -d com.apple.FinderInfo 空格后拖入项目回车就行了

  3. HDU 1009 FatMouse' Trade题解

    版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/.未经本作者同意不得转载. https://blog.csdn.net/kenden23/article ...

  4. VS 2019编辑含有资源文件.resx的项目时提示MSB3086 任务未能使用 SdkToolsPath 或注册表项找到“al.exe”

    环境: Win10 X64, VS2019 错误提示: 错误 MSB3086 任务未能使用 SdkToolsPath“”或注册表项“HKEY_LOCAL_MACHINE\SOFTWARE\Micros ...

  5. 二、搭建Swagger

    1.新建.netCore webapi项目 2.安装swagger ,通过 Package Manager 控制台:Install-Package Swashbuckle.AspNetCore 3.修 ...

  6. Linux性能优化从入门到实战:10 内存篇:如何利用Buffer和Cache优化程序的运行效率?

    缓存命中率   缓存命中率,是指直接通过缓存获取数据的请求次数,占所有数据请求次数的百分比,可以衡量缓存使用的好坏.命中率越高,表示使用缓存带来的收益越高,应用程序的性能也就越好.   实际上,缓存是 ...

  7. LinuxC语言实现ATM取款机实验Socket

    链接:https://pan.baidu.com/s/1sZt4qhYc6CDJVpoJHbtw-Q 提取码:53ot 复制这段内容后打开百度网盘手机App,操作更方便哦 本实验用的是Centos7t ...

  8. 64bit assembly

    16个通用寄存器: %rax %rbx %rcx %rdx %rsi %rdi %rbp %rsp %r8 %r9 %r10 %r11 %r12 %r13 %r14 %r15

  9. whetstone

    https://www.cnblogs.com/findumars/p/4173040.html 下载源码:http://www.netlib.org/benchmark/whetstone.c ar ...

  10. python笔记(2)---不定长参数

    python自定义函数中有两种不定长参数, 第一种是*name:加了星号 * 的参数会以元组(tuple)的形式导入 第二种是**name:加了星号 * *的参数会以字典(dict)的形式导入 *na ...