标签:
题目链接:http://codeforces.com/problemset/problem/686/C
给你n和m,问你有多少对(a, b) 满足0<=a <n 且 0 <=b < m 且a的7进制和n-1的7进制位数相同 且b的7进制和m-1的7进制位数相同,还有a和b的7进制上的每位上的数各不相同。
看懂题目,就很简单了,先判断a和b的7进制位数是否超过7,不超过的话就dfs暴力枚举计算就可以了。
1 //#pragma comment(linker, "/STACK:102400000, 102400000") 2 #include <algorithm> 3 #include <iostream> 4 #include <cstdlib> 5 #include <cstring> 6 #include <cstdio> 7 #include <vector> 8 #include <cmath> 9 #include <ctime> 10 #include <list> 11 #include <set> 12 #include <map> 13 using namespace std; 14 typedef long long LL; 15 typedef pair <int, int> P; 16 const int N = 1e5 + 5; 17 int num[10], n, m, ans; 18 bool vis[10]; 19 20 int get(int num[], int len) { 21 int res = 0, temp = 1; 22 for(int i = len; i >= 1; --i) { 23 res += num[len] * temp; 24 temp *= 7; 25 } 26 return res; 27 } 28 29 void dfs(int len1, int len2, int dep) { 30 if(dep == len1 + len2) { 31 int sum = 0, temp = 1; 32 for(int i = len1+len2; i >= len1+1; --i) { 33 sum += temp*num[i]; 34 temp *= 7; 35 } 36 if(sum < m) 37 ans++; 38 return ; 39 } 40 else if(dep == len1) { 41 int sum = 0, temp = 1; 42 for(int i = len1; i >= 1; --i) { 43 sum += temp*num[i]; 44 temp *= 7; 45 } 46 if(sum >= n) 47 return ; 48 } 49 dep++; 50 for(int i = 0 ; i < 7 ; ++i) { 51 if(vis[i]) 52 continue; 53 vis[i] = true; 54 num[dep] = i; 55 dfs(len1, len2, dep); 56 vis[i] = false; 57 } 58 59 } 60 61 int main() 62 { 63 cin >> n >> m; 64 int cnt1 = 0, cnt2 = 0, temp = n - 1; 65 do { 66 temp /= 7; 67 ++cnt1; 68 } while(temp); 69 temp = m - 1; 70 do { 71 temp /= 7; 72 ++cnt2; 73 } while(temp); 74 if (cnt1 + cnt2 > 7) { 75 cout << 0 << endl; 76 } else { 77 dfs(cnt1, cnt2, 0); 78 cout << ans << endl; 79 } 80 return 0; 81 }
Codeforces Round #359 (Div. 2) C. Robbers' watch (暴力DFS)
标签:
原文地址:http://www.cnblogs.com/Recoder/p/5724428.html