题目连接:https://vjudge.net/contest/365059#problem/A
题目大意 :
就是求区间内能被所有位上的数字(!0)整除的数的个数
想法:
首先lcm{1~9}=2520;
想到每个数都是1~9中某些数字的lcm 所以他们一定能整除2520
由数论知识可以知道: x % km % m = x % m
所以我们可以得到 x%2520%lcm{xi}==0 是满足条件
但是我们这个是以后发现如果根据这样开数组会导致空间不够!
每个数只能是1~9的最小公倍数 所以计算了下 所有的lcm一共有48种可 能 如下:
1 2 3 4 5 6 7 8 9 10 12 14 15 18 20 21 24 28 30 35 36 40 42 45 56 60 63 70 72 84 90 105 120 126 140 168 180 210 252 280 315 360 420 504 630 840 1260 2520
离散化一下就好了
#pragma GCC optimize(3,"Ofast","inline")//O3优化 #pragma GCC optimize(2)//O2优化 #include <algorithm> #include <string> #include <string.h> #include <vector> #include <map> #include <stack> #include <set> #include <queue> #include <math.h> #include <cstdio> #include <iomanip> #include <time.h> #include <bitset> #include <cmath> #include <sstream> #include <iostream> #include <cstring> #define LL long long #define ls nod<<1 #define rs (nod<<1)+1 #define pii pair<int,int> #define mp make_pair #define pb push_back #define INF 0x3f3f3f3f #define max(a,b) (a>b?a:b) #define min(a,b) (a<b?a:b) const double eps = 1e-10; const int maxn = 2e5 + 10; const int mod = 1e9 + 7; int sgn(double a){return a < -eps ? -1 : a < eps ? 0 : 1;} using namespace std; LL L,R; int sum,len; int b[30]; LL mem[20][2522][50]; int k; int vis[2550]; LL gcd(LL a,LL b) { LL t; while (b) { t = b; b = a % b; a = t; } return a; } LL lcm(LL a,LL b) { return a / gcd(a,b) * b; } void init() { k = 0; for (int i = 1;i <= 2520;i++) { if (2520 % i == 0) vis[i] = k++; } } LL dfs(int cur,int m,int Lcm,bool f) { if (cur < 0) return m % Lcm == 0; if (!f && mem[cur][m][vis[Lcm]] != -1) return mem[cur][m][vis[Lcm]]; int v = 9; if (f) v = b[cur]; LL ans = 0; for (int i = 0;i <= v;i++) { LL tlcm; if (i == 0) tlcm = Lcm; else tlcm = lcm(Lcm,i); ans += dfs(cur-1,(10*m+i)%2520,tlcm,f && (i == v)); } if (!f) mem[cur][m][vis[Lcm]] = ans; return ans; } LL solve(LL x) { len = 0; while (x) { b[len++] = x % 10; x /= 10; } return dfs(len-1,0,1,1); } int main() { int T; scanf("%d",&T); init(); memset(mem,-1,sizeof(mem)); while (T--) { scanf("%I64d%I64d",&L,&R); LL ans = 0; ans = solve(R) - solve(L-1); printf("%I64d\n",ans); // cout << solve(R) << endl; // cout << solve(L-1) << endl; } return 0; }