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 ...
随机推荐
- 《深入分析Java Web技术内幕》读后感(Session、cookie)
第10章 P263 理解Cookie 理解Session Session如何工作的
- GPLT天梯赛 L2-022. 重排链表
L2-022. 重排链表 时间限制 500 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一个单链表 L1→L2→...→Ln-1→Ln,请 ...
- WPF在ViewModel中绑定按钮点击(CommandBase定义)
定义CommandBase public class CommandBase:ICommand { private readonly Action<object> _commandpara ...
- JVM内存GC的骗局
此文已由作者尧飘海授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 概述 在日常程序开发中,很多JAVA程度员不太关心内存的使用情况.当然,如果程序员运气较好或者系统没有大规模 ...
- 327. Count of Range Sum(inplace_marge)
Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...
- MYSQL limit,offset 区别(转)
SELECT keyword FROM keyword_rank WHERE advertiserid='59' order by keyword LIMIT 2 OFFSET 1; 比如这个SQL ...
- 初步了解XMLHttpRequest对象、http请求的封装
构造器 var xhr = new XMLHttpRequest() 设置超时时间 xhr.ontimeout= 设置超时时间为 1s 设置超时时间(单位:ms) 0为永不超时 HTTP 请求的状态 ...
- 洛谷P3003 [USACO10DEC]苹果交货Apple Delivery
P3003 [USACO10DEC]苹果交货Apple Delivery 题目描述 Bessie has two crisp red apples to deliver to two of her f ...
- cogs 2691. Sumdiv
2691. Sumdiv ★★★ 输入文件:sumdiv.in 输出文件:sumdiv.out 简单对比时间限制:1 s 内存限制:12 MB [题目描述] 考虑两个自然数A和B.定义 ...
- IT兄弟连 Java语法教程 Java语言的其他特性
Java语言中除了非常重要的跨平台特性外,还有如下几个关键特性: ● 语法简单易学 Java语言的语法简单明了,容易掌握,而且是纯面向对象(OOP)的语言,Java语言的简单性主要体现在以下几个方面 ...