https://vjudge.net/contest/67836#problem/F

"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 destroyed in World War II. Fortunately old Brumbaugh from research knew Klein's secrets and wrote them down before he died. A Klein safe has two distinguishing features: a combination lock that uses letters instead of numbers, and an engraved quotation on the door. A Klein quotation always contains between five and twelve distinct uppercase letters, usually at the beginning of sentences, and mentions one or more numbers. Five of the uppercase letters form the combination that opens the safe. By combining the digits from all the numbers in the appropriate way you get a numeric target. (The details of constructing the target number are classified.) To find the combination you must select five letters v, w, x, y, and z that satisfy the following equation, where each letter is replaced by its ordinal position in the alphabet (A=1, B=2, ..., Z=26). The combination is then vwxyz. If there is more than one solution then the combination is the one that is lexicographically greatest, i.e., the one that would appear last in a dictionary."

v - w^2 + x^3 - y^4 + z^5 = target

"For example, given target 1 and letter set ABCDEFGHIJKL, one possible solution is FIECB, since 6 - 9^2 + 5^3 - 3^4 + 2^5 = 1. There are actually several solutions in this case, and the combination turns out to be LKEBA. Klein thought it was safe to encode the combination within the engraving, because it could take months of effort to try all the possibilities even if you knew the secret. But of course computers didn't exist then."

=== Op tech directive, computer division, 2002/11/02 12:30 CST ===

"Develop a program to find Klein combinations in preparation for field deployment. Use standard test methodology as per departmental regulations. Input consists of one or more lines containing a positive integer target less than twelve million, a space, then at least five and at most twelve distinct uppercase letters. The last line will contain a target of zero and the letters END; this signals the end of the input. For each line output the Klein combination, break ties with lexicographic order, or 'no solution' if there is no correct combination. Use the exact format shown below."

Sample Input

1 ABCDEFGHIJKL
11700519 ZAYEXIWOVU
3072997 SOUGHT
1234567 THEQUICKFROG
0 END

Sample Output

LKEBA
YOXUZ
GHOST
no solution

时间复杂度:$O(C_n^5 * A_5^5)$

题解:dfs

代码:

#include <bits/stdc++.h>
using namespace std; int tar, len;
char a[20];
int visi[20];
char res[10];
char tmp[10]; int debug(char *s) {
double sum = 0;
for(int i = 0; i < 5; i += 2) {
sum += pow(double(s[i] - 'A' + 1), double(i + 1));
}
for(int i = 1; i < 5;i += 2) {
sum -= pow(double(s[i] - 'A' + 1), double(i + 1));
}
return sum;
} void dfs(int step) {
if(step == 5) {
tmp[5] = '\0';
if(debug(tmp) == tar) {
if(strcmp(tmp, res) > 0)
strcpy(res, tmp);
}
return ;
}
for(int i = 0; i < len; i ++) {
if(!visi[i]) {
tmp[step] = a[i];
visi[i] = 1;
dfs(step + 1);
visi[i] = 0;
}
}
} int main() {
while(scanf("%d %s", &tar, a)) {
if(tar == 0 && strcmp(a, "END") == 0)
break;
memset(visi, 0, sizeof(visi));
strcpy(res, "@");
len = strlen(a); for(int i = 0; i < len; i ++) {
visi[i] = 1;
tmp[0] = a[i];
dfs(1);
visi[i] = 0;
} if(strcmp(res, "@") == 0)
printf("no solution\n");
else
printf("%s\n", res);
}
return 0;
}

  

ZOJ 1403 F-Safecracker的更多相关文章

  1. 暴力 ZOJ 1403 Safecracker

    题目传送门 /* 暴力:纯暴力,在家水水 */ #include <cstdio> #include <cstring> #include <algorithm> ...

  2. ZOJ 1403&&HDU 1015 Safecracker【暴力】

    Safecracker Time Limit: 2 Seconds      Memory Limit: 65536 KB === Op tech briefing, 2002/11/02 06:42 ...

  3. ZOJ 1403 解密

    参考自:https://www.cnblogs.com/ECJTUACM-873284962/p/6412212.htmlSafecracker Time Limit: 2 Seconds       ...

  4. dp式子100个……

    1.        资源问题1-----机器分配问题F[I,j]:=max(f[i-1,k]+w[i,j-k]) 2.        资源问题2------01背包问题F[I,j]:=max(f[i- ...

  5. A过的题目

    1.TreeMap和TreeSet类:A - Language of FatMouse ZOJ1109B - For Fans of Statistics URAL 1613 C - Hardwood ...

  6. dp方程

    1.        资源问题1 -----机器分配问题 F[I,j]:=max(f[i-1,k]+w[i,j-k]) 2.        资源问题2 ------01背包问题   F[I,j]:=ma ...

  7. The 2014 ACM-ICPC Asia Mudanjiang Regional

    继续复盘之前的Regional......出题者说这一套题太简单,对当时没有AK很不满......真是醉了,弱校没法活了 [A]签到题 [B]树结构,树的中心 [C]-_-/// [D]概率DP [E ...

  8. Mysql_以案例为基准之查询

    查询数据操作

  9. ZOJ 3962 Seven Segment Display 16进制的八位数加n。求加的过程中所有的花费。显示[0,F]有相应花费。

    Seven Segment Display Time Limit: Seconds Memory Limit: KB A seven segment display, or seven segment ...

随机推荐

  1. Spring Cloud之 Config Server 使用ip端口号配置高可用

    先看官方文档的配置 --- spring: profiles: peer1 eureka: instance: hostname: peer1 client: serviceUrl: defaultZ ...

  2. git 之忽略文件 gitignore 创建和使用规则

    1..gitignore文件的创建:首先要强调一点,这个文件的完整文件名就是“.gitignore”,注意最前面有个“.”.这样没有扩展名的文件在Windows下不太好创建,这里给出win7的创建方法 ...

  3. Python语言发展的关键时间节点

    1989年:Python想法的产生 1991年:发布最早的Python可用版本 2000年:发布Python2.0 2010年:发布Python2.x系列的最后一个版本,主版本号为2.7 2008年: ...

  4. P2257 YY的GCD

    P2257 YY的GCD 题目描述 神犇YY虐完数论后给傻×kAc出了一题 给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, y)有多少对 k ...

  5. 20145234黄斐《信息安全系统设计基础》第七周(Linux命令复习)

    已经到了11月,学期过半,而<信息安全系统设计基础>这门课也要到了期中考试了.所以,我在这里,对前半个学期的最基础的知识,做一个复习 复习计划分为两步,本次为Linux命令,下次计划复习g ...

  6. 青岛Uber优步司机奖励政策(12月28日到1月3日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  7. Unknown host 'services.gradle.org' 解决方法

    报错如下: Unknown host 'services.gradle.org'. You may need to adjust the proxy settings in Gradle. Learn ...

  8. .net core mvc 模型绑定 之 json and urlencoded

    .net core mvc 模型绑定, FromQuery,对应 url 中的 urlencoded string ("?key1=value1&key2=value2") ...

  9. Tomcat7后台通过get接收数据处理乱码

    Tomcat7后台通过get接收数据处理乱码 //因为tomcat7 默认将用get传来的数据用ISO-8859-1封装, //将ajax传过来的值解码,再转码,//因为tomcat7 默认将用get ...

  10. JavaScript事件及BOM和DOM

    1. 事件 1.1 事件绑定 # 写在html元素中<button onclick="code..."></div>​# 把事件当做元素对象的方法btnEl ...