HDU 5179 beautiful number (数位dp / 暴力打表 / dfs)

beautiful number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 801    Accepted Submission(s): 518


Problem Description
Let  A=ni=1ai10ni(1ai9)(n is the number of A's digits). We call A as “beautiful number” if and only if a[i]a[i+1] when 1i<n and a[i] mod a[j]=0 when 1in,i<jn(Such as 931 is a "beautiful number" while 87 isn't).
Could you tell me the number of “beautiful number” in the interval [L,R](including L and R)?
 

 

Input
The fist line contains a single integer  T(about 100), indicating the number of cases.
Each test case begins with two integers L,R(1LR109).
 

 

Output
For each case, output an integer means the number of “beautiful number”.
 

 

Sample Input
2
1 11
999999993 999999999
 

 

Sample Output
10
2
 
Source
 

 

Recommend
hujie

 

 

题意:

  要你输出 [L, R] 范围内的满足高位大于等于地位而且高位mod低位的数要等于0 的数的个数。

题解:

  1)离线暴力打表

    我们跑[1,1e9] 的所有满足这个条件的数。

    

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <string>
 5 #include <algorithm>
 6 #include <cmath>
 7 #include <vector>
 8 #include <queue>
 9 #include <map>
10 #include <stack>
11 #include <set>
12 using namespace std;
13 typedef long long LL;
14 typedef unsigned long long uLL;
15 #define ms(a, b) memset(a, b, sizeof(a))
16 #define pb push_back
17 #define mp make_pair
18 #define eps 0.0000000001
19 const LL INF = 0x7fffffff;
20 const int inf = 0x3f3f3f3f;
21 const int mod = 10000007;
22 const int maxn = 500+10;
23 int a[1300] ={1,2,3,4,5,6,7,8,9,11,21,22,31,33,41,42,44,51,55,61,62,63,66,71,77,81,82,84,88,91,93,99,111,211,221,222,311,331,333,411,421,422,441,442,444,511,551,555,611,621,622,631,633,661,662,663,666,711,771,777,811,821,822,841,842,844,881,882,884,888,911,931,933,991,993,999,1111,2111,2211,2221,2222,3111,3311,3331,3333,4111,4211,4221,4222,4411,4421,4422,4441,4442,4444,5111,5511,5551,5555,6111,6211,6221,6222,6311,6331,6333,6611,6621,6622,6631,6633,6661,6662,6663,6666,7111,7711,7771,7777,8111,8211,8221,8222,8411,8421,8422,8441,8442,8444,8811,8821,8822,8841,8842,8844,8881,8882,8884,8888,9111,9311,9331,9333,9911,9931,9933,9991,9993,9999,11111,21111,22111,22211,22221,22222,31111,33111,33311,33331,33333,41111,42111,42211,42221,42222,44111,44211,44221,44222,44411,44421,44422,44441,44442,44444,51111,55111,55511,55551,55555,61111,62111,62211,62221,62222,63111,63311,63331,63333,66111,66211,66221,66222,66311,66331,66333,66611,66621,66622,66631,66633,66661,66662,66663,66666,71111,77111,77711,77771,77777,81111,82111,82211,82221,82222,84111,84211,84221,84222,84411,84421,84422,84441,84442,84444,88111,88211,88221,88222,88411,88421,88422,88441,88442,88444,88811,88821,88822,88841,88842,88844,88881,88882,88884,88888,91111,93111,93311,93331,93333,99111,99311,99331,99333,99911,99931,99933,99991,99993,99999,111111,211111,221111,222111,222211,222221,222222,311111,331111,333111,333311,333331,333333,411111,421111,422111,422211,422221,422222,441111,442111,442211,442221,442222,444111,444211,444221,444222,444411,444421,444422,444441,444442,444444,511111,551111,555111,555511,555551,555555,611111,621111,622111,622211,622221,622222,631111,633111,633311,633331,633333,661111,662111,662211,662221,662222,663111,663311,663331,663333,666111,666211,666221,666222,666311,666331,666333,666611,666621,666622,666631,666633,666661,666662,666663,666666,711111,771111,777111,777711,777771,777777,811111,821111,822111,822211,822221,822222,841111,842111,842211,842221,842222,844111,844211,844221,844222,844411,844421,844422,844441,844442,844444,881111,882111,882211,882221,882222,884111,884211,884221,884222,884411,884421,884422,884441,884442,884444,888111,888211,888221,888222,888411,888421,888422,888441,888442,888444,888811,888821,888822,888841,888842,888844,888881,888882,888884,888888,911111,931111,933111,933311,933331,933333,991111,993111,993311,993331,993333,999111,999311,999331,999333,999911,999931,999933,999991,999993,999999,1111111,2111111,2211111,2221111,2222111,2222211,2222221,2222222,3111111,3311111,3331111,3333111,3333311,3333331,3333333,4111111,4211111,4221111,4222111,4222211,4222221,4222222,4411111,4421111,4422111,4422211,4422221,4422222,4441111,4442111,4442211,4442221,4442222,4444111,4444211,4444221,4444222,4444411,4444421,4444422,4444441,4444442,4444444,5111111,5511111,5551111,5555111,5555511,5555551,5555555,6111111,6211111,6221111,6222111,6222211,6222221,6222222,6311111,6331111,6333111,6333311,6333331,6333333,6611111,6621111,6622111,6622211,6622221,6622222,6631111,6633111,6633311,6633331,6633333,6661111,6662111,6662211,6662221,6662222,6663111,6663311,6663331,6663333,6666111,6666211,6666221,6666222,6666311,6666331,6666333,6666611,6666621,6666622,6666631,6666633,6666661,6666662,6666663,6666666,7111111,7711111,7771111,7777111,7777711,7777771,7777777,8111111,8211111,8221111,8222111,8222211,8222221,8222222,8411111,8421111,8422111,8422211,8422221,8422222,8441111,8442111,8442211,8442221,8442222,8444111,8444211,8444221,8444222,8444411,8444421,8444422,8444441,8444442,8444444,8811111,8821111,8822111,8822211,8822221,8822222,8841111,8842111,8842211,8842221,8842222,8844111,8844211,8844221,8844222,8844411,8844421,8844422,8844441,8844442,8844444,8881111,8882111,8882211,8882221,8882222,8884111,8884211,8884221,8884222,8884411,8884421,8884422,8884441,8884442,8884444,8888111,8888211,8888221,8888222,8888411,8888421,8888422,8888441,8888442,8888444,8888811,8888821,8888822,8888841,8888842,8888844,8888881,8888882,8888884,8888888,9111111,9311111,9331111,9333111,9333311,9333331,9333333,9911111,9931111,9933111,9933311,9933331,9933333,9991111,9993111,9993311,9993331,9993333,9999111,9999311,9999331,9999333,9999911,9999931,9999933,9999991,9999993,9999999,11111111,21111111,22111111,22211111,22221111,22222111,22222211,22222221,22222222,31111111,33111111,33311111,33331111,33333111,33333311,33333331,33333333,41111111,42111111,42211111,42221111,42222111,42222211,42222221,42222222,44111111,44211111,44221111,44222111,44222211,44222221,44222222,44411111,44421111,44422111,44422211,44422221,44422222,44441111,44442111,44442211,44442221,44442222,44444111,44444211,44444221,44444222,44444411,44444421,44444422,44444441,44444442,44444444,51111111,55111111,55511111,55551111,55555111,55555511,55555551,55555555,61111111,62111111,62211111,62221111,62222111,62222211,62222221,62222222,63111111,63311111,63331111,63333111,63333311,63333331,63333333,66111111,66211111,66221111,66222111,66222211,66222221,66222222,66311111,66331111,66333111,66333311,66333331,66333333,66611111,66621111,66622111,66622211,66622221,66622222,66631111,66633111,66633311,66633331,66633333,66661111,66662111,66662211,66662221,66662222,66663111,66663311,66663331,66663333,66666111,66666211,66666221,66666222,66666311,66666331,66666333,66666611,66666621,66666622,66666631,66666633,66666661,66666662,66666663,66666666,71111111,77111111,77711111,77771111,77777111,77777711,77777771,77777777,81111111,82111111,82211111,82221111,82222111,82222211,82222221,82222222,84111111,84211111,84221111,84222111,84222211,84222221,84222222,84411111,84421111,84422111,84422211,84422221,84422222,84441111,84442111,84442211,84442221,84442222,84444111,84444211,84444221,84444222,84444411,84444421,84444422,84444441,84444442,84444444,88111111,88211111,88221111,88222111,88222211,88222221,88222222,88411111,88421111,88422111,88422211,88422221,88422222,88441111,88442111,88442211,88442221,88442222,88444111,88444211,88444221,88444222,88444411,88444421,88444422,88444441,88444442,88444444,88811111,88821111,88822111,88822211,88822221,88822222,88841111,88842111,88842211,88842221,88842222,88844111,88844211,88844221,88844222,88844411,88844421,88844422,88844441,88844442,88844444,88881111,88882111,88882211,88882221,88882222,88884111,88884211,88884221,88884222,88884411,88884421,88884422,88884441,88884442,88884444,88888111,88888211,88888221,88888222,88888411,88888421,88888422,88888441,88888442,88888444,88888811,88888821,88888822,88888841,88888842,88888844,88888881,88888882,88888884,88888888,91111111,93111111,93311111,93331111,93333111,93333311,93333331,93333333,99111111,99311111,99331111,99333111,99333311,99333331,99333333,99911111,99931111,99933111,99933311,99933331,99933333,99991111,99993111,99993311,99993331,99993333,99999111,99999311,99999331,99999333,99999911,99999931,99999933,99999991,99999993,99999999,111111111,211111111,221111111,222111111,222211111,222221111,222222111,222222211,222222221,222222222,311111111,331111111,333111111,333311111,333331111,333333111,333333311,333333331,333333333,411111111,421111111,422111111,422211111,422221111,422222111,422222211,422222221,422222222,441111111,442111111,442211111,442221111,442222111,442222211,442222221,442222222,444111111,444211111,444221111,444222111,444222211,444222221,444222222,444411111,444421111,444422111,444422211,444422221,444422222,444441111,444442111,444442211,444442221,444442222,444444111,444444211,444444221,444444222,444444411,444444421,444444422,444444441,444444442,444444444,511111111,551111111,555111111,555511111,555551111,555555111,555555511,555555551,555555555,611111111,621111111,622111111,622211111,622221111,622222111,622222211,622222221,622222222,631111111,633111111,633311111,633331111,633333111,633333311,633333331,633333333,661111111,662111111,662211111,662221111,662222111,662222211,662222221,662222222,663111111,663311111,663331111,663333111,663333311,663333331,663333333,666111111,666211111,666221111,666222111,666222211,666222221,666222222,666311111,666331111,666333111,666333311,666333331,666333333,666611111,666621111,666622111,666622211,666622221,666622222,666631111,666633111,666633311,666633331,666633333,666661111,666662111,666662211,666662221,666662222,666663111,666663311,666663331,666663333,666666111,666666211,666666221,666666222,666666311,666666331,666666333,666666611,666666621,666666622,666666631,666666633,666666661,666666662,666666663,666666666,711111111,771111111,777111111,777711111,777771111,777777111,777777711,777777771,777777777,811111111,821111111,822111111,822211111,822221111,822222111,822222211,822222221,822222222,841111111,842111111,842211111,842221111,842222111,842222211,842222221,842222222,844111111,844211111,844221111,844222111,844222211,844222221,844222222,844411111,844421111,844422111,844422211,844422221,844422222,844441111,844442111,844442211,844442221,844442222,844444111,844444211,844444221,844444222,844444411,844444421,844444422,844444441,844444442,844444444,881111111,882111111,882211111,882221111,882222111,882222211,882222221,882222222,884111111,884211111,884221111,884222111,884222211,884222221,884222222,884411111,884421111,884422111,884422211,884422221,884422222,884441111,884442111,884442211,884442221,884442222,884444111,884444211,884444221,884444222,884444411,884444421,884444422,884444441,884444442,884444444,888111111,888211111,888221111,888222111,888222211,888222221,888222222,888411111,888421111,888422111,888422211,888422221,888422222,888441111,888442111,888442211,888442221,888442222,888444111,888444211,888444221,888444222,888444411,888444421,888444422,888444441,888444442,888444444,888811111,888821111,888822111,888822211,888822221,888822222,888841111,888842111,888842211,888842221,888842222,888844111,888844211,888844221,888844222,888844411,888844421,888844422,888844441,888844442,888844444,888881111,888882111,888882211,888882221,888882222,888884111,888884211,888884221,888884222,888884411,888884421,888884422,888884441,888884442,888884444,888888111,888888211,888888221,888888222,888888411,888888421,888888422,888888441,888888442,888888444,888888811,888888821,888888822,888888841,888888842,888888844,888888881,888888882,888888884,888888888,911111111,931111111,933111111,933311111,933331111,933333111,933333311,933333331,933333333,991111111,993111111,993311111,993331111,993333111,993333311,993333331,993333333,999111111,999311111,999331111,999333111,999333311,999333331,999333333,999911111,999931111,999933111,999933311,999933331,999933333,999991111,999993111,999993311,999993331,999993333,999999111,999999311,999999331,999999333,999999911,999999931,999999933,999999991,999999993,999999999};
24 int main() {
25 #ifdef LOCAL
26     freopen("input.txt", "r", stdin);
27 //        freopen("output.txt", "w", stdout);
28 #endif
29     ios::sync_with_stdio(0);cin.tie(0);
30     int t;
31     cin >> t;
32     while(t--){
33         int L, R;
34         cin >> L >> R;
35         int ans = 0;
36         for(int i=0;i<1299;i++){
37             if(a[i]>=L&&a[i]<=R)    ans++;
38         }
39         cout << ans << endl;
40     }
41     return 0;
42 }
View Code

 

  2)dfs

    最大也就10个位,暴力dfs每一个位。

  

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <string>
 5 #include <algorithm>
 6 #include <cmath>
 7 #include <vector>
 8 #include <queue>
 9 #include <map>
10 #include <stack>
11 #include <set>
12 using namespace std;
13 typedef long long LL;
14 typedef unsigned long long uLL;
15 #define ms(a, b) memset(a, b, sizeof(a))
16 #define pb push_back
17 #define mp make_pair
18 #define eps 0.0000000001
19 const LL INF = 0x7fffffff;
20 const int inf = 0x3f3f3f3f;
21 const int mod = 10000007;
22 const int maxn = 500+10;
23 int L, R;
24 int ans = 0;
25 void dfs(LL num, int pre)
26 {
27     if(num>R)   return;
28     if(num>=L)  ans++;
29     for(int i = 1;i<=pre;i++){
30         if(pre%i==0)
31             dfs(num*10+i, i);
32     }
33 }
34 int main() {
35 #ifdef LOCAL
36     freopen("input.txt", "r", stdin);
37 //        freopen("output.txt", "w", stdout);
38 #endif
39     ios::sync_with_stdio(0);cin.tie(0);
40     int t;
41     cin >> t;
42     while(t--){
43         ans = 0;
44         cin >> L >> R;
45         for(int i = 1;i<=9;i++){
46             dfs(i, i);
47         }
48         cout << ans << endl;
49     }
50     return 0;
51 }
View Code

 

  3)数位dp

    跟dfs很像,但是加上了记忆话搜索。

 

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <string>
 5 #include <algorithm>
 6 #include <cmath>
 7 #include <vector>
 8 #include <queue>
 9 #include <map>
10 #include <stack>
11 #include <set>
12 using namespace std;
13 typedef long long LL;
14 typedef unsigned long long uLL;
15 #define ms(a, b) memset(a, b, sizeof(a))
16 #define pb push_back
17 #define mp make_pair
18 #define eps 0.0000000001
19 const LL INF = 0x7fffffff;
20 const int inf = 0x3f3f3f3f;
21 const int mod = 10000007;
22 const int maxn = 500+10;
23 int L, R;
24 int ans = 0;
25 int a[15];
26 int dp[25][10];
27 int dfs(int pos, bool lead, bool limit, int pre)
28 {
29     if(pos == 0)    return 1;
30     if(!limit&&!lead&&dp[pos][pre]!=-1) return dp[pos][pre];
31     int up = limit?a[pos]:9;
32     LL ans = 0;
33     for(int i = 0;i<=up;i++){
34         if(lead||pre>=i&&i!=0&&pre%i==0){
35             ans +=dfs(pos-1, lead&&i==0, limit&&i==a[pos], i);
36         }
37     }
38     if(!limit&&!lead)   dp[pos][pre] = ans;
39     return ans;
40 }
41 int solve(int x)
42 {
43     int len = 0;
44     while(x){
45         a[++len] = x%10;
46         x/=10;
47     }
48     return dfs(len, true, true, 0);
49 }
50 int main() {
51 #ifdef LOCAL
52     freopen("input.txt", "r", stdin);
53 //        freopen("output.txt", "w", stdout);
54 #endif
55     ios::sync_with_stdio(0);cin.tie(0);
56     int t;
57     cin >> t;
58     ms(dp, -1);
59     while(t--){
60         cin >> L >> R;
61         cout << solve(R) - solve(L-1) << endl;
62     }
63     return 0;
64 }
View Code

 

转载于:https://www.cnblogs.com/denghaiquan/p/7247439.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值