CodeForces 55D Beautiful numbers(数位dp)
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
1
1 9
9
1
12 15
2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | /* CodeForces 55D Beautiful numbers(数位dp) problem: 问[l,r]之间有多少个能被它所包含的非零数整除 solve: 如果能这些数整除,则能被他们的最小公倍数整除. 1~9的最小公倍数为2520,所以在过程中维护lcm和这个数对2520的取余 而且可以处理出1~9的所有lcm,离散化处理. hhh-2016-08-25 16:56:23 */ #pragma comment(linker,"/STACK:124000000,124000000") #include <algorithm> #include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <vector> #include <math.h> #include <queue> #include <map> #define lson i<<1 #define rson i<<1|1 #define ll long long #define clr(a,b) memset(a,b,sizeof(a)) #define scanfi(a) scanf("%d",&a) #define scanfl(a) scanf("%I64d",&a) #define key_val ch[ch[root][1]][0] #define inf 0x3f3f3f3f using namespace std; const ll mod = 2520; const int maxn = 100010; int tot,t[maxn]; ll dp[20][3000][50]; int bin[3000]; int cnt = 0; int lcm( int a, int b) { if (!a && !b) return 0; if (!a) return b; if (!b) return a; int ta=a,tb=b; while (a % b != 0) { int p = a % b; a = b; b = p; } return ta/b*tb; } int fin( int pos) { int l = 0,r = cnt-1; while (l < r) { int mid = (l+r) >> 1; if (bin[mid] > pos) { r = mid -1; } else if (bin[mid] == pos) { return mid; } else l = mid + 1; } } ll dfs( int len, int nex, int div , int flag) { if (len < 0 && div == 0) return 0; if (len < 0) return nex % div == 0; int tp = bin[ div ]; if (dp[len][nex][tp] != -1 && !flag) return dp[len][nex][tp] ; ll ans = 0; int n = flag ? t[len] : 9; for ( int i = 0; i <= n; i++) { int lc; lc = lcm( div ,i); // cout << lc <<" " << i <<endl; int ta = (nex*10+i) % mod; ans += dfs(len-1,ta,lc,flag && i == n); } // cout << ans<<" "<<div<<endl; if (!flag) dp[len][nex][tp] = ans; return ans; } ll cal(ll a) { tot = 0; while (a) { int p = a%10LL; t[tot++] = p; a /= 10LL; } ll ans = 0; ans += dfs(tot-1,0,0,1); return ans; } ll a,b; int main() { int T; cnt = 0; bin[cnt++] = 0; for ( int i = 1;i <= mod;i++) { if (mod % i == 0) bin[i] = cnt++; } // freopen("in.txt","r",stdin); memset (dp,-1, sizeof (dp)); scanfi(T); while (T--) { scanfl(a),scanfl(b); printf ( "%I64d\n" ,cal(b) - cal(a-1)); } } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 现代计算机视觉入门之:什么是视频
· 你所不知道的 C/C++ 宏知识
· 聊一聊 操作系统蓝屏 c0000102 的故障分析
· SQL Server 内存占用高分析
· .NET Core GC计划阶段(plan_phase)底层原理浅谈
· 盘点!HelloGitHub 年度热门开源项目
· 某Websocket反爬逆向分析+请求加解密+还原html
· 02现代计算机视觉入门之:什么是视频
· 回顾我的软件开发经历:我与代码生成器的涅槃之路
· DeepSeek V3 两周使用总结