题意:
一个电子手表的示数是7进制的,现在告诉你一天有多少小时和一小时有多少分钟,问你一天里有多少个时刻,这个表上显示的数字各不相同.
分析:
先找出表上有多少位数字,再按位dfs,看最后得到的数是否<n和<m,把分和时转化为7进制,若位数大于7则直接输出0,若不大于零,则用dfs找到分和时的所有位不相同的情况
代码如下:
1 #include <iostream>
2 #include <cstdio>
3 #include <cstring>
4 #include <fstream>
5 #include <ctime>
6 #include <cmath>
7 #include <cstdlib>
8 #include <algorithm>
9 #include <set>
10 #include <map>
11 #include <list>
12 #include <stack>
13 #include <queue>
14 #include <iterator>
15 #include <vector>
16
17 using namespace std;
18
19 #define LL long long
20 #define INF 0x3f3f3f3f
21 #define MOD 1000000007
22 #define MAXN 10000010
23 #define MAXM 1000010
24
25 #include <iostream>
26 #include <cstdio>
27 #include <cstring>
28 #include <fstream>
29 #include <ctime>
30 #include <cmath>
31 #include <cstdlib>
32 #include <algorithm>
33 #include <set>
34 #include <map>
35 #include <list>
36 #include <stack>
37 #include <queue>
38 #include <iterator>
39 #include <vector>
40
41 using namespace std;
42
43 #define LL long long
44 #define INF 0x3f3f3f3f
45 #define MOD 1000000007
46 #define MAXN 10000010
47 #define MAXM 1000010
48
49 LL n, m;
50 int weishu_n, zongweishu_n_m, kinds_ans;
51 int a[10];
52
53 int check()
54 {
55 int sum , x;
56 sum = 0;
57 x = 1;
58 for(int i = weishu_n-1; i >= 0; i--)
59 {
60 sum += x*a[i];
61 x *= 7;
62 }
63 if(sum >= n)
64 return 0;
65 sum = 0;
66 x = 1;
67 for(int i = zongweishu_n_m-1; i >= weishu_n; i--)
68 {
69 sum += x*a[i];
70 x *= 7;
71 }
72 if(sum >= m)
73 return 0;
74 return 1;
75 }
76
77 void dfs(int cnt)
78 {
79 if(cnt == zongweishu_n_m)
80 {
81 if(check())
82 kinds_ans++;
83 }
84 else
85 for(int i = 0; i < 7; i++ )
86 {
87 int ok = 1;
88 for(int j = 0; j < cnt; j++ )
89 if(i == a[j])
90 {
91 ok = 0;
92 break;
93 }
94 if(ok)
95 {
96 a[cnt] = i;
97 dfs(cnt+1);
98 }
99 }
100 }
101
102 int main()
103 {
104 int pos;
105 int kn, km;
106 while(scanf("%lld%lld", &n, &m)==2)
107 {
108 memset(a, 0, sizeof(a));
109 kinds_ans = 0;
110 weishu_n = 0;
111 zongweishu_n_m = 0;
112 pos = 0;
113 kn = n-1;
114 if(!kn)
115 pos++;
116 while(kn)
117 {
118 kn /= 7;
119 pos++;
120 }
121 weishu_n = pos;
122 km = m-1;
123 if(!km)
124 pos++;
125 while(km)
126 {
127 km /= 7;
128 pos++;
129 }
130 zongweishu_n_m = pos;
131 dfs(0);
132 if(zongweishu_n_m > 7)
133 printf("0
");
134 else
135 printf("%d
", kinds_ans);
136 }
137
138 return 0;
139 }