CF 55D Beautiful numbers (数位DP)
题意:
如果一个正整数能被其所有位上的数字整除,则称其为Beautiful number,问区间[L,R]共有多少个Beautiful number?(1<=L<=R<=9*1018)
思路:
数字很大,不能暴力。但是想要知道一个数是否为Beautiful number时,至少得等到它的所有位都出现吧?不然如何确定其实可以被整除的呢?
分析一下,类似2232和3232等这样的数字,这两个只是出现了2和3而已,他们的lcm都是6,所以有可以压缩统计的地方就是lcm,开一维来存储。接下来考虑前缀部分有没有什么可以压缩的地方,由于1~9的lcm最大是2520,那可以将前缀先模2520,最后再模那个数位的真正lcm就行了(2520必定是所有1~9中的任一组合的lcm的倍数,所以先取余是不会影响结果的),那么再开一维。所以状态为dp[位数][数位lcm][余2520的结果],就可以将所有数字给归类到这3维里面了。数位lcm可以优化,打表发现仅有47位可能的lcm而已,所以不必开2520的大小。
#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <algorithm>
#include <vector>
#include <iostream>
#define pii pair<int,int>
#define INF 0x7f3f3f3f
#define LL long long
#define ULL unsigned long long
using namespace std;
const double PI = acos(-1.0);
const int N=;
const int mod=;
int p[]={,,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,}; int _lcm(int m,int n){return (m*n)/__gcd(m, n);}
LL f[N][][mod+], bit[N], has[mod+]; LL dfs(int i,int lcm,int left, bool e)
{
if(i==) return lcm&&left%p[lcm]== ;
if(!e && ~f[i][lcm][left]) return f[i][lcm][left]; LL ans=;
int u= e? bit[i]: ;
for(int d=; d<=u; d++)
{
int t= lcm? has[_lcm(p[lcm], max(d,))]: max(d,);
ans+=dfs(i-, t, (left*+d)%mod, e&&d==u);
}
return e==true? ans: f[i][lcm][left]=ans;
} LL cal(LL n)
{
int len=;
while(n) //拆数
{
bit[++len]=n%;
n/=;
}
return dfs(len,,,true);
} int main()
{
//freopen("input.txt","r",stdin);
memset(f,-,sizeof(f));
for(int i=; i<; i++) has[p[i]]=i; LL L, R, t;cin>>t;
while( t-- )
{
cin>>L>>R;
cout<<cal(R)-cal(L-)<<endl;
}
return ;
}
AC代码
CF 55D Beautiful numbers (数位DP)的更多相关文章
- CF 55D. Beautiful numbers(数位DP)
题目链接 这题,没想出来,根本没想到用最小公倍数来更新,一直想状态压缩,不过余数什么的根本存不下,看的von学长的blog,比着写了写,就是模版改改,不过状态转移构造不出,怎么着,都做不出来. #in ...
- codeforces 55D - Beautiful numbers(数位DP+离散化)
D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
- CodeForces - 55D - Beautiful numbers(数位DP,离散化)
链接: https://vjudge.net/problem/CodeForces-55D 题意: Volodya is an odd boy and his taste is strange as ...
- CodeForces - 55D Beautiful numbers —— 数位DP
题目链接:https://vjudge.net/problem/CodeForces-55D D. Beautiful numbers time limit per test 4 seconds me ...
- Codeforces - 55D Beautiful numbers (数位dp+数论)
题意:求[L,R](1<=L<=R<=9e18)区间中所有能被自己数位上的非零数整除的数的个数 分析:丛数据量可以分析出是用数位dp求解,区间个数可以转化为sum(R)-sum(L- ...
- codeforces 55D. Beautiful numbers 数位dp
题目链接 一个数, 他的所有位上的数都可以被这个数整除, 求出范围内满足条件的数的个数. dp[i][j][k], i表示第i位, j表示前几位的lcm是几, k表示这个数mod2520, 2520是 ...
- 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP)
2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP) 链接:https://ac.nowcoder.com/acm/contest/163/ ...
- 【数位dp】CF 55D Beautiful numbers
题目 Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer n ...
- CF 55D - Beautiful numbers(数位DP)
题意: 如果一个数能被自己各个位的数字整除,那么它就叫 Beautiful numbers.求区间 [a,b] 中 Beautiful numbers 的个数. 分析:先分析出,2~9 的最大的最小公 ...
- Codeforces Beta Round #51 D. Beautiful numbers 数位dp
D. Beautiful numbers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/55/p ...
随机推荐
- Hive安装 …
Hive安装 mysql使用主机(win7)上的mysql数据库,启动后,要关闭360和win7自带的防火墙,确保在虚拟机里能拼通主机********************************* ...
- 1、在 Windows 上安装 OpenCV-Python & ubuntu16.04安装 opencv
Goals In this tutorial We will learn to setup OpenCV-Python in your Windows system. Below steps are ...
- linux下 C编程改变输出字体颜色
格式: echo "\033[字背景颜色;字体颜色m字符串\033[0m"例如:echo "\033[41;36m something here \033[0m" ...
- Unity中场景异步加载
引入命名空间 using UnityEngine.UI; using UnityEngine.SceneManagement; using System.Collections; using Syst ...
- sqlserver的语句和mysql语句
感谢原创 sqlserver和mysql基本语句的对比 http://blog.csdn.net/kk185800961/article/details/47044751 sqlserver中常见的语 ...
- JavaScript中两个数组的拼接
方法一:使用for循环 var arr = ['tom', 'jerry']; var arr2 = [1, 2]; for(var i=0; i<arr2.length; i++){ arr. ...
- u17 u18共存
公司用的Unity版本是2017版本的,由于需要尝试一些实验性的新功能,我就安装了Unity2018版本,结果发现Unity2018版本破解之后,Unity2017版本不能用了.那么怎么解决两个版本的 ...
- poj2241 The Tower of Babylon
The Tower of Babylon 题意:给你n种石头,长x,宽y,高z,每种石头数目无限,一块石头能放到另一块上的条件是:长和宽严格小于下面的石头.问叠起来的最大高度. /* 有些类似“叠箱子 ...
- 洛谷P3704 [SDOI2017]数字表格(莫比乌斯反演)
传送门 yyb大佬太强啦…… 感觉还是有一点地方没有搞懂orz //minamoto #include<cstdio> #include<iostream> #include& ...
- python 之 包的使用
6.8 包的使用 包就是一个包含有init.py文件的文件夹,所以其实我们创建包的目的就是为了用文件夹将文件/模块组织起来 强调: 在python3中,即使包下没有__init__.py文件,impo ...