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. 【ABAP系列】SAP abap dialog screen屏幕参数简介

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP abap dialog ...

  2. Bootstrap 学习笔记 项目实战 响应式导航栏

    导航代码HTML: <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset=&q ...

  3. oracle--单行函数和多行函数

    单行函数 1.字符函数 函  数 功  能 示  例 结 果 INITCAP (char) 首字母大写 initcap ('hello') Hello LOWER (char) 转换为小写 lower ...

  4. [19/05/05-星期日] JDBC(Java DataBase Connectivity,java数据库连接)_mysql基本知识

    一.概念 (1).是一种开放源代码的关系型数据库管理系统(RDBMS,Relational Database Management System):目前有很多大公司(新浪.京东.阿里)使用: (2). ...

  5. HDU 1880 题解(字符串哈希)

    题面: 魔咒词典 Time Limit: 8000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  6. swiper和tab相结合

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. ActiveMQ的介绍及使用

    一.消息中间件概述 什么是消息中间件 发送者将消息发送给消息服务器,消息服务器将消感存放在若千队列中,在合适的时候再将消息转发给接收者. 这种模式下,发送和接收是异步的,发送者无需等待; 二者的生命周 ...

  8. 解析安装mysql

    大多数人在结束咱们前面学习的基础知识的时候,其实一脸懵逼,不过我们已经开始步入了另一个新的高度,针对基础知识还是必须巩固针对性的进行补充,可以分模块总结:比如基础知识的数据结构---->函数-- ...

  9. .NET Reactor使用教程(加密源代码示例)

    更多:https://www.cnblogs.com/PiaoMiaoGongZi/category/1120300.html 1.打开 Eziriz .NET Reactor,主界面如图1所示: 图 ...

  10. 2018-8-10-调试-ms-源代码

    title author date CreateTime categories 调试 ms 源代码 lindexi 2018-08-10 19:17:19 +0800 2018-2-13 17:23: ...