博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CodeForces 55D - Beautiful numbers
阅读量:5012 次
发布时间:2019-06-12

本文共 2250 字,大约阅读时间需要 7 分钟。

Time Limit:4000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u
 

Description

Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer number is beautiful if and only if it is divisible by each of its nonzero digits. We will not argue with this and just count the quantity of beautiful numbers in given ranges.

Input

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

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

Sample Input

Input
1 1 9
Output
9
Input
1 12 15
Output
2
#include 
#include
#include
#include
using namespace std;typedef long long LL; int a[20];const int MOD = 2520;LL dp[20][MOD][50];int hash[MOD];int GCD(int a, int b){ if(b==0) return a; return GCD(b, a%b); }int LCM(int a, int b){ return a/GCD(a,b)*b;}LL dfs(int len, int mod, int lcm, bool limit){ if(len < 1) return mod%lcm==0; if(!limit && dp[len][mod][hash[lcm]] != -1) return dp[len][mod][hash[lcm]]; int maxn = limit ? a[len] : 9; LL ret = 0; int new_lcm; for(int i=0; i<=maxn; i++) { if(i) new_lcm = LCM(lcm, i); else new_lcm = lcm; ret += dfs(len-1, (mod*10+i)%MOD, new_lcm, limit&&i==maxn); } if(!limit) dp[len][mod][hash[lcm]] = ret; return ret;}LL f(LL n){ int len = 0; while(n) { a[++len] = n % 10; n/=10; } return dfs(len, 0, 1, 1);}void Init(){ int cnt = 0; for(int i=1; i<=MOD; i++) { if(MOD % i == 0) { hash[i] = cnt++; } }// printf("cnt = %d\n", cnt); //}int main (){ Init(); int t; LL l, r; scanf("%d", &t); memset(dp, -1, sizeof(dp)); while(t--) { scanf("%I64d%I64d", &l, &r); printf("%I64d\n", f(r)-f(l-1)); } return 0;}

 

转载于:https://www.cnblogs.com/AcIsFun/p/5391833.html

你可能感兴趣的文章
Educational Codeforces Round 60 (Rated for Div. 2) C. Magic Ship
查看>>
Windows 2008 R2系统开机时如何不让Windows进行磁盘检测?
查看>>
WP7应用开发笔记(18) 本地化与多语言
查看>>
解决 .so文件64与32不兼容问题
查看>>
归并排序法
查看>>
【剑指offer】面试题26:复杂链表的复制
查看>>
spark开发生成EXE
查看>>
Vue 全家桶介绍
查看>>
WPF Bitmap转Imagesource
查看>>
Java compiler level does not match the version of the installed Java project facet.解决方法
查看>>
笔记_小结
查看>>
Linux lsof命令 umount U盘
查看>>
自定义Font
查看>>
linux svn 服务端搭建
查看>>
maven用途、核心概念、用法、常用参数和命令、扩展
查看>>
linux时间同步ntp服务的安装与配置
查看>>
django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE的解决办法...
查看>>
网络编程-socket并发-粘包问题
查看>>
python 中安装pandas
查看>>
Hibernate 的<generator class="native"></generator>的不同属性含义
查看>>