Codeforces Beta Round #51 D. Beautiful numbers
4 seconds
256 megabytes
standard input
standard output
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.
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).
1
1 9
9
1
12 15
2
数位dp,这题,要求最后的数,对于,每一个数字都要整除,那么,我们就可以通过,一个数能整除所有数字的最小公倍数就可以了!所以我们用dp[i][j][]表示第i位,最小公倍数为j,第i位时余下的是k时的最后的个数,这题还可以进行优化,最是把最小公倍数编号,因为,2-9最小公倍数最大是2520,也就是说,只有48个,这样,大大节省了内存,加快速度!
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
#define M 23
__int64 dp[M][55][3500],pri[M];
int hash[3500],kk=0;
int gcd(int a,int b){
if(a==0)return b;
return gcd(b%a,a);
}
int get(int t,int i){
if(i==0||i==1)return t;
return t*i/gcd(t,i);
}
__int64 dfs(int pos,int t,int flag,int pre){
if(pos==0)return pre%t==0;
if(!flag&&dp[pos][hash[t]][pre]!=-1)return dp[pos][hash[t]][pre];
int u=flag?pri[pos]:9;
__int64 ans=0;
for(int i=0;i<=u;i++)
ans+=dfs(pos-1,get(t,i),flag&&i==u,(pre*10+i)%2520);
return flag?ans:dp[pos][hash[t]][pre]=ans;
}
__int64 solve(__int64 x){
int cnt=0;
while(x){
pri[++cnt]=x%10;x/=10;
}
return dfs(cnt,1,1,0);
}
int init(){
memset(dp,-1,sizeof(dp));
int cnt=0;
hash[0]=0;
for(int i=1;i<=8;i*=2)
for(int j=1;j<=9;j*=3)
for(int k=1;k<=5;k*=5)
for(int x=1;x<=7;x*=7)
hash[i*j*k*x]=++cnt;
}
int main()
{
int tcase;__int64 n,m;
init();
while(scanf("%d",&tcase)!=EOF){
while(tcase--){
scanf("%I64d%I64d",&m,&n);
printf("%I64d\n",solve(n)-solve(m-1));
}
}
return 0;
}
Codeforces Beta Round #51 D. Beautiful numbers的更多相关文章
- 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 Beta Round #51 D. Beautiful numbers(数位dp)
题目链接:https://codeforces.com/contest/55/problem/D 题目大意:给你一段区间[l,r],要求这段区间中可以整除自己每一位(除0意外)上的数字的整数个数,例如 ...
- codeforces 55d//Beautiful numbers// Codeforces Beta Round #51
题意:一个数能整除它所有的位上的数字(除了0),统计这样数的个数. 注意离散化,为了速度更快需存入数组查找. 不要每次memset,记录下已有的长度下符合条件的个数. 数位dp肯定是从高位到低位. 记 ...
- Codeforces Beta Round #51 B. Smallest number dfs
B. Smallest number Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/55/pro ...
- Codeforces Beta Round #51 C. Pie or die 博弈论找规律 有趣的题~
C. Pie or die Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/55/problem/ ...
- Codeforces Beta Round #51 A. Flea travel 水题
A. Flea travel Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/55/problem ...
- Codeforces Beta Round #80 (Div. 2 Only)【ABCD】
Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...
- Codeforces Beta Round #62 题解【ABCD】
Codeforces Beta Round #62 A Irrational problem 题意 f(x) = x mod p1 mod p2 mod p3 mod p4 问你[a,b]中有多少个数 ...
- Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】
Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...
随机推荐
- ie9以上浏览器input文本框/密码框后面的小叉子/小眼睛问题
找了很久不知什么属性控制的这个东西,经过群友的指点重要找到.
- CodeIgniter框架介绍
入口 index.php :一些路径常量定义: => CodeIgniter.php : 1. 初始化一些 system/core内的类,例如Config, Utf8, URI 和Router等 ...
- winform下载网页源码
public partial class Form1 : Form{public Form1(){InitializeComponent();} private void button1_Click( ...
- js将数字转换成大写的人民币表达式
function changeNumMoneyToChinese(money) { var cnNums = new Array("零", "壹", " ...
- Uva_11916 Emoogle Grid
题目链接 题意: 有个N X M的棋盘, 有K种颜色, 有B个不可涂色的位置, 共有R种涂色方案. 1)每个可涂色的位置必须涂上一种颜色 2)不可涂色位置不能涂色 3)每个位置必须从K种颜色中选出一种 ...
- Codeforces Round #205 (Div. 2) : A
题意: 要求找到最少次数的交换次数使得两组数都是偶数: 很明显答案要么是0,要么是1,或者不管怎么交换都不行(-1): 所以: #include<cstdio> #define maxn ...
- Semaphore的介绍和使用
转自:http://www.itzhai.com/the-introduction-and-use-of-semaphore.html 相关介绍 public class Semaphore exte ...
- KeilC51常用功能模块使用说明
本文档包括单片机系统中常用到的时钟中断.通讯及键盘扫描等模块(见所附源程序)的说明.这些模块使用前后台系统模型.为达到最大的灵活性, 需要在用户工程中定义config.h文件, 在其中定义各模块可选参 ...
- Linux下修改MySQL编码的方法
有时候因为编码需要修改mysql的编码,windows下修改有图文界面简单一些,linux大家就可以参考下面的方法 默认登录mysql之后可以通过SHOW VARIABLES语句查看系统变量及其值 ...
- Reverse Linked List 递归非递归实现
单链表反转--递归非递归实现 Java接口: ListNode reverseList(ListNode head) 非递归的实现 有2种,参考 头结点插入法 就地反转 递归的实现 1) Divide ...