题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5308
题面:
I Wanna Become A 24-Point Master
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 602 Accepted Submission(s): 249
Special Judge
Problem Description
Recently Rikka falls in love with an old but interesting game -- 24 points. She wants to become a master of this game, so she asks Yuta to give her some problems to practice.
Quickly, Rikka solved almost all of the problems but the remained one is really difficult:
In this problem, you need to write a program which can get 24 points withn
numbers, which are all equal to n .
Quickly, Rikka solved almost all of the problems but the remained one is really difficult:
In this problem, you need to write a program which can get 24 points with
Input
There are no more then 100 testcases and there are no more then 5 testcases with
n≥100 .
Each testcase contains only one integer n (1≤n≤105)
Output
For each testcase:
If there is not any way to get 24 points, print a single line with -1.
Otherwise, letA
be an array with 2n−1
numbers and at firsrt Ai=n (1≤i≤n) .
You need to print n−1
lines and the i th
line contains one integer a ,
one char b
and then one integer c, where 1≤a,c<n+i
and b
is "+","-","*" or "/". This line means that you let
Aa
and Ac
do the operation b
and store the answer into An+i .
If your answer satisfies the following rule, we think your answer is right:
1.A2n−1=24
2. Each position of the arrayA
is used at most one tine.
3. The absolute value of the numerator and denominator of each element in arrayA
is no more than 109
If there is not any way to get 24 points, print a single line with -1.
Otherwise, let
If your answer satisfies the following rule, we think your answer is right:
1.
2. Each position of the array
3. The absolute value of the numerator and denominator of each element in array
Sample Input
4
Sample Output
1 * 2 5 + 3 6 + 4
Source
解题:
如此之大的数据量,搜索是肯定不行。但还是被题目那句大于100的数据不会超过5组给蒙了一下。队友之前想着能不能从24往前搜,实则也是不行的。
由于根本不知道前面到底有什么数。又该相应如何的操作。看了题解后,恍然大悟。就应该去构造。
枚举n比較小的情况,然后当n大于等于14时,能够去凑((4*n)/n)*((6*n)/n),尽管是12个n,可是仍要从14,開始,由于多余的n须要通过一次减法,多次乘法消去。最后再加上之前算出的24就可以。
代码:
#include <iostream> #include <cstdio> #include <cmath> using namespace std; int main() { int n,tmp; while(~scanf("%d",&n)) { // printf("%d: ",n); if(n<=3) printf("-1 "); else if(n==4) printf("1 * 2 5 + 3 6 + 4 "); else if(n==5) printf("1 * 2 3 / 6 4 - 7 5 * 8 "); else if(n==6) printf("1 + 2 3 + 4 5 - 6 7 + 8 10 - 9 "); else if(n==7) printf("1 + 2 3 + 8 9 / 4 10 + 5 11 + 6 12 + 7 "); else if(n==8) printf("1 + 2 3 + 9 4 - 5 11 * 6 12 * 7 13 * 8 10 + 14 "); else if(n==9) printf("1 + 2 3 + 10 4 / 5 6 / 7 8 / 9 11 - 12 15 - 13 16 - 14 "); else if(n==10) printf("1 + 2 3 / 4 5 / 6 7 / 8 9 / 10 11 + 12 16 + 13 17 + 14 18 + 15 "); else if(n==11) printf("1 + 2 3 / 4 5 / 6 7 - 8 15 * 9 16 * 10 17 * 11 12 + 13 19 + 14 20 + 18 "); else if(n==12) printf("1 + 2 3 - 4 5 * 14 6 * 15 7 * 16 8 * 17 9 * 18 10 * 19 11 * 20 12 * 21 13 + 22 "); else if(n==13) printf("1 + 2 3 / 4 5 / 6 7 - 8 17 * 9 18 * 10 19 * 11 20 * 12 21 * 13 22 + 14 23 - 15 24 - 16 "); else { printf("1 + 2 3 + 4 5 + 6 7 + 8 9 + 10 "); printf("%d + %d %d + %d %d + %d ",n+1,n+2,n+3,n+4,n+5,n+6); printf("%d / 11 %d / 12 ",n+7,n+8); printf("%d * %d ",n+9,n+10); printf("13 - 14 "); tmp=n-14; int i; for(i=0;i<tmp;i++) { printf("%d * %d ",n+12+i,15+i); } printf("%d + %d ",n+11,n+12+tmp); } // printf(" "); } return 0; }