[codeforces 55]D. Beautiful numbers
[codeforces 55]D. Beautiful numbers
试题描述
输入
The first line of the input contains the number of cases t (1 ≤ t ≤ 10). Each of the next t lines contains two natural numbers li and ri(1 ≤ li ≤ ri ≤ 9 ·1018).
Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cin (also you may use %I64d).
输出
Output should contain t numbers — answers to the queries, one number per line — quantities of beautiful numbers in given intervals (from li to ri, inclusively).
输入示例
输出示例
数据规模及约定
见“输入”
题解
“能同时被所有数位上的数字整除”等价于“能被所有数位上数字的最大公约数整除”。于是设 f[i][k][m] 表示一个 i 位的数,所有数字的最小公倍数等于 k,且这个数为 m。等等,状态 m 都知道这个数了,那 dp 个啥?别着急,我们一步步优化。
我们知道 lcm(1, 2, 3, 4, 5, 6, 7, 8, 9) = 2520(就是1~9的最小公倍数),那么对于刚才 m 的那一维状态是没有必要太大的,将这维状态对 2520 取个模就好了,即 m 表示这个数对 mod 2520 的值。
还有,1~9 这些数中,所有可能出现的最小公倍数只有 48 个(这个不妨读者自己写程序统计一下),所以离散一波 k 就变成最大只有 48 的数了。
最后状态数大概是:19 * 48 * 2520 = 2298240。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <algorithm>
using namespace std;
#define LL long long LL read() {
LL x = 0, f = 1; char c = getchar();
while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }
while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }
return x * f;
} #define maxn 21
#define maxm 2521
#define maxc 49
int Lcm[maxc], cl, id[maxm], Gcd[maxm][maxm];
bool tmp[maxm];
LL f[maxn][maxc][maxm], ten[maxn]; int gcd(int x, int y){ return !y ? x : gcd(y, x % y); } int get(int l, int x) {
int nl;
if(!x && !l) nl = 0;
if(x && l) nl = l * x / Gcd[l][x];
if(!x && l) nl = l;
if(x && !l) nl = x;
return nl;
} int num[maxn];
LL sum(LL x) {
int cnt = 0; LL tx = x;
while(x) num[++cnt] = x % 10, x /= 10;
LL ans = 0; int l = 1;
for(int i = cnt; i; i--) {
for(int j = 0; j < num[i]; j++) {
int nl = get(l, j);
for(int k = 0; k <= cl; k++) {
int nnl = get(nl, Lcm[k]);
LL t;
if(i < cnt) t = (tx / ten[i] * ten[i] + ten[i-1] * j) % 2520;
else t = ten[i-1] * j % 2520;
for(int m = 0; m < maxm - 1; m += nnl) {
int M = (m - t + 2520) % 2520; if(M < 0) continue;
if(f[i-1][k][M]) ans += f[i-1][k][M];
}
}
}
l = get(l, num[i]);
}
if(tx % l == 0) ans++;
return ans;
} int main() {
for(int i = 1; i < maxm; i++)
for(int j = 1; j < maxm; j++) Gcd[i][j] = gcd(i, j); tmp[1] = 1;
for(int j = 2; j <= 9; j++)
for(int x = maxm - 1; x; x--)
if(tmp[x]) tmp[x*j/Gcd[x][j]] = 1;
for(int i = 1; i < maxm; i++)
if(tmp[i]) Lcm[++cl] = i, id[i] = cl; ten[0] = 1;
for(int i = 1; i < maxn; i++) ten[i] = ten[i-1] * 10; f[0][0][0] = 1;
for(int j = 0; j <= 9; j++) f[1][id[j]][j] = 1;
for(int i = 1; i < maxn - 1; i++)
for(int k = 0; k <= cl; k++)
for(int m = 0; m < maxm; m++) if(f[i][k][m]) {
int l = Lcm[k];
for(int x = 0; x <= 9; x++) {
int nl = get(l, x);
f[i+1][id[nl]][(ten[i]*x+m)%2520] += f[i][k][m];
}
}
int T = read();
while(T--) {
LL l = read(), r = read();
printf("%I64d\n", sum(r) - sum(l - 1));
} return 0;
}
[codeforces 55]D. Beautiful numbers的更多相关文章
- CF 55 D. Beautiful numbers
D. Beautiful numbers 链接 题意: 求[L,R]中多少个数字可以整除它们的每一位上的数字. 分析: 要求模一些数字等于0等价于模它们的lcm等于0,所以可以记录当前出现的数字的lc ...
- 【Codeforces 300C】Beautiful Numbers
[链接] 我是链接,点我呀:) [题意] 让你找到长度为n的数字 这个数字只由a或者b组成 且这n个数码的和也是由a或者b组成的 求出满足这样要求的数字的个数 [题解] 枚举答案数字中b的个数为y,那 ...
- CF D. Beautiful numbers (数位dp)
http://codeforces.com/problemset/problem/55/D Beautiful Numbers : 这个数能整除它的全部位上非零整数.问[l,r]之间的Beautifu ...
- codeforces 55D - Beautiful numbers(数位DP+离散化)
D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
- 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 ...
- codeforces Beautiful Numbers
来源:http://codeforces.com/problemset/problem/1265/B B. Beautiful Numbers You are given a permutat ...
- Codeforces Round #181 (Div. 2) C. Beautiful Numbers 排列组合 暴力
C. Beautiful Numbers 题目连接: http://www.codeforces.com/contest/300/problem/C Description Vitaly is a v ...
- Codeforces Beta Round #51 D. Beautiful numbers
D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
- CodeForces 55D "Beautiful numbers"(数位DP+离散化处理)
传送门 参考资料: [1]:CodeForces 55D Beautiful numbers(数位dp&&离散化) 我的理解: 起初,我先定义一个三维数组 dp[ i ][ j ][ ...
随机推荐
- OC description
description方法的作用是打印对象,对于一个类,如果没有重写description方法,NSLog(@“%@”,此处写类的对象), 输出的是该类的地址如下: -- :::] <Class ...
- wpf 线程
一.线程概述:[引用MSDN] 通常,WPF 应用程序从两个线程开始:一个用于处理呈现,一个用于管理 UI.呈现线程有效地隐藏在后台运行,而 UI 线程则接收输入.处理事件.绘制屏幕以及运行应用程序代 ...
- Interface/接口
1. 类和结构能够实现接口 2. 接口声明包含如下四种类型:属性.方法.事件和索引:这些函数声明不能包含任何实现代码,而在每一个成员的主体后必须使用分号 3. 继承接口的类或结构必须实现接口中的所有成 ...
- PHP_$_SERVER_说明详解
PHP编程中经常需要用到一些服务器的一些资料,特把$_SERVER的详细参数整理下,方便以后使用. $_SERVER['PHP_SELF'] #当前正在执行 脚本的文件名,与 document roo ...
- Win10微软官方最终正式版ISO镜像文件
Win10微软官方最终正式版ISO镜像文件 据说Windows 10是微软发布的最后一个Windows版本,下一代Windows将作为Update形式出现.Windows 10将发布7个发行版本,分别 ...
- Centos 6.0将光盘作为yum源的设置方法
在使用Centos 的时候,用yum来安装软件包是再方便不过了,但是如果在无法连接互联网的情况下,yum就不好用了. 下面介绍一种方式,就是将Centos安装光盘作为yum源,然后使用yum来安装软件 ...
- 药企信息sop
中国药品生产企业 http://db.yaozh.com/shengchanqiye 全球药品生产企业 http://db.yaozh.com/quanqiuqiye
- ecshop修改注册、增加手机
1.去掉“用户名”注册 a.去掉提交 user_passport.dwt页面去掉 <input name="username" type="text" s ...
- JNDI全面总结
JNDI全面总结原理: 在DataSource中事先建立多个数据库连接,保存在数据库连接池中.当程序访问数据库时,只用从连接池中取空闲状态的数据库连接即可,访问结束,销毁资源,数据库连接重新回到连接池 ...
- Scala 中object和class的区别
Scala中没有静态类型,但是有有“伴侣对象”,起到类似的作用. Scala中类对象中不可有静态变量和静态方法,但是提供了“伴侣对象”的功能:在和类的同一个文件中定义同名的Object对象:(须在同一 ...