[HDOJ1015]Safecracker(DFS, 组合数学)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1015

这都能过……

 1 #include <algorithm>
 2 #include <iostream>
 3 #include <iomanip>
 4 #include <cstring>
 5 #include <climits>
 6 #include <complex>
 7 #include <fstream>
 8 #include <cassert>
 9 #include <cstdio>
10 #include <bitset>
11 #include <vector>
12 #include <deque>
13 #include <queue>
14 #include <stack>
15 #include <ctime>
16 #include <set>
17 #include <map>
18 #include <cmath>
19 
20 using namespace std;
21 
22 typedef long long ll;
23 const ll maxn = 15;
24 ll aim;
25 ll n;
26 char str[maxn];
27 char ans[maxn];
28 char wtf[maxn];
29 bool flag;
30 
31 ll quickmul(ll x, ll n) {
32     ll ans = 1;
33     ll t = x;
34     while(n) {
35         if(n & 1) {
36             ans = (ans * t);
37         }
38         t = t * t;
39         n >>= 1;
40     }
41     return ans;
42 }
43 
44 int main() {
45     // freopen("in", "r", stdin);
46     // freopen("out", "w", stdout);
47     while(~scanf("%I64d %s", &aim, str)) {
48         if(aim == 0 && strcmp("END", str) == 0) break; 
49         memset(wtf, 0, sizeof(wtf));
50         n = strlen(str);
51         ll nn = 1 << n;
52         flag = 0;
53         sort(str, str+n, greater<char>());
54         for(ll i = 1; i < nn; i++) {
55             ll cnt = 0;
56             memset(ans, 0, sizeof(ans));
57             for(ll j = 0; j < n; j++) {
58                 if((1 << j) & i) {
59                     ans[cnt++] = str[j];
60                 }
61             }
62             if(cnt != 5) continue;
63             sort(ans, ans+cnt);
64             do {
65                 ll tmp = ans[0] - 'A' + 1;
66                 for(ll k = 1; k < cnt; k++) {
67                     tmp += quickmul(-1, k) * quickmul(ans[k]-'A'+1, k+1);
68                 }
69                 if(tmp == aim) {
70                     flag = 1;
71                     if(strcmp(wtf, ans) < 0) {
72                         strcpy(wtf, ans);
73                     }
74                 }
75             }while(next_permutation(ans, ans+cnt));
76         }
77         if(!flag) printf("no solution
");
78         else printf("%s
", wtf);
79     }
80     return 0;
81 }
原文地址:https://www.cnblogs.com/kirai/p/5462862.html