[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\n");
78         else printf("%s\n", wtf);
79     }
80     return 0;
81 }
时间: 2024-12-05 17:20:38

[HDOJ1015]Safecracker(DFS, 组合数学)的相关文章

HDOJ-1015 Safecracker 【DFS】

Safecracker Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 13669    Accepted Submission(s): 7150 Problem Description === Op tech briefing, 2002/11/02 06:42 CST === "The item is locked in a Klei

hdu 1015 Safecracker DFS解法 ,简单明了

Safecracker Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9327    Accepted Submission(s): 4721 Problem Description === Op tech briefing, 2002/11/02 06:42 CST === "The item is locked in a Klei

HDU 1015 Safecracker(DFS)

Safecracker Problem Description === Op tech briefing, 2002/11/02 06:42 CST === "The item is locked in a Klein safe behind a painting in the second-floor library. Klein safes are extremely rare; most of them, along with Klein and his factory, were des

洛谷——P3914 染色计数

P3914 染色计数 题目描述 有一颗NN个节点的树,节点用1,2,\cdots,N1,2,?,N编号.你要给它染色,使得相邻节点的颜色不同.有MM种颜色,用1,2,\cdots,M1,2,?,M编号.每个节点可以染MM种颜色中的若干种,求不同染色方案的数量除以(10^9 + 7109+7)的余数. 输入输出格式 输入格式: 第1 行,2 个整数N,MN,M. 接下来NN行,第ii行表示节点ii可以染的颜色.第1个整数k_iki?,表示可以染的颜色数量.接下来k_iki?个整数,表示可以染的颜色

hdoj 1015 Safecracker【DFS】

Safecracker Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 10612    Accepted Submission(s): 5429 Problem Description === Op tech briefing, 2002/11/02 06:42 CST === "The item is locked in a Kle

HDU 1015 Safecracker【数值型DFS】

Safecracker Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3966    Accepted Submission(s): 2028 Problem Description === Op tech briefing, 2002/11/02 06:42 CST === "The item is locked in a Klein

hdu - 1010 Tempter of the Bone (dfs+奇偶性剪枝) &amp;&amp; hdu-1015 Safecracker(简单搜索)

http://acm.hdu.edu.cn/showproblem.php?pid=1010 这题就是问能不能在t时刻走到门口,不能用bfs的原因大概是可能不一定是最短路路径吧. 但是这题要过除了细心外,还需要强力的剪枝. 奇偶性剪枝:参考 http://www.cppblog.com/Geek/archive/2010/04/26/113615.html 1 #include <iostream> 2 #include <cstring> 3 #include <cstdi

HDU-1796 How many integers can you find(组合数学、dfs)

How many integers can you find Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8997    Accepted Submission(s): 2697 Problem Description Now you get a number N, and a M-integers set, you should

HDU ACM 1015 Safecracker 暴力DFS

分析:暴搜,复杂度一次最高也才12^5. #include<iostream> #include<cmath> using namespace std; #define N 30 char s[15],ts[10],ans[10]; int tar,index[N]; bool vis[N]; void dfs(int d,int n) { int i,tmp; if(d==5) { tmp=index[ts[0]-'A']-pow(index[ts[1]-'A'],2)+pow(