I Wanna Become A 24-Point Master
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 759 Accepted Submission(s): 315
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 with n 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 n numbers, which are all equal to n .
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, let A 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 array A is used at most one tine.
3. The absolute value of the numerator and denominator of each element in array A is no more than 109
If there is not any way to get 24 points, print a single line with -1.
Otherwise, let A 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 array A is used at most one tine.
3. The absolute value of the numerator and denominator of each element in array A is no more than 109
Sample Input
4
Sample Output
1 * 2 5 + 3 6 + 4
Source
又是一想法构造题:给n个n,问能否通过+ - * / 构造24的值,按照规定格式输出构造方式。
暴力枚举:
n<=3 无解
n=4 4*4+4+4
n=5 (5-5/5/5)*5
n=6 6+6+6+6+6-6
n=7 (7+7+7)/7+7+7+7
n=8 (8+8+8+8)/(8+8)*8+8
n=9 (9+9+9-(9+9+9)/9)*9/9
n=10 ((10+10)/10+10)*(10/10+10/10)*10/10
n=11 (11+11/11)*(11/11+11/11)*11/11*11/11
n>=12 ((n+n+n+n)/n )*((n+n+n+n+n+n)/n)*n/n......
n>=13 ((n+n)/n)*((n+n)/n)*((n+n)/n)*((n+n+n)/n).......
#include<iostream>
#include<cstdio>
using namespace std;
int main(){
// freopen("5308.in","r",stdin);
// freopen("5308.out","w",stdout);
int n;
while(~scanf("%d",&n)){
if(n<=3){puts("-1");continue;}
if(n==4){puts("1 * 2");puts("5 + 3");puts("6 + 4");continue; }
if(n==5){puts("2 / 3");puts("6 / 4");puts("1 - 7");puts("8 * 5");continue; }
if(n==6){puts("1 + 2");puts("7 + 3");puts("8 + 4");puts("9 + 5");puts("10 - 6");continue; }
if(n==7){puts("1 + 2");puts("8 + 3");puts("9 / 4");puts("10 + 5");puts("11 + 6");puts("12 + 7");continue; }
if(n==8){puts("1 + 2");puts("9 + 3");puts("10 + 4");puts("5 + 6");puts("11 / 12");puts("13 * 7");puts("14 + 8");continue; }
if(n==9){puts("1 + 2");puts("10 + 3");puts("4 + 5");puts("12 + 6");puts("13 / 7");
puts("11 - 14");puts("15 * 8");puts("16 / 9");continue;}
if(n==10){puts("1 + 2");puts("11 / 3");puts("12 + 4");puts("5 / 6");puts("7 / 8");
puts("14 + 15");puts("13 * 16");puts("17 * 9");puts("18 / 10");continue; }
if(n==11){puts("1 / 2");puts("12 + 3");puts("4 / 5");puts("6 / 7");puts("14 + 15");puts("13 * 16");
puts("17 * 8");puts("18 * 9");puts("19 / 10");puts("20 / 11");continue; }
// if(n==12){puts("1 + 2");puts("13 * 3");puts("14 * 4");puts("15 * 5");puts("16 * 6");puts("17 * 7");
// puts("18 / 8");puts("19 / 9");puts("20 / 10");puts("21 / 11");puts("22 / 12");continue; }
if(n&1){
puts("1 + 2");printf("%d / 3\n",n+1);
puts("4 + 5");printf("%d / 6\n",n+3);
puts("7 + 8");printf("%d / 9\n",n+5);
puts("10 + 11");printf("%d + 12\n",n+7);printf("%d / 13\n",n+8);
printf("%d * %d\n",n+2,n+4);printf("%d * %d\n",n+10,n+6);
printf("%d * %d\n",n+11,n+9);
int x=n+12,y=14;
for(int i=1;i<=(n-13)/2;i++){
printf("%d * %d\n",x++,y++);
printf("%d / %d\n",x++,y++);
}
}else{
puts("1 + 2");printf("%d + 3\n",n+1);printf("%d + 4\n",n+2);printf("%d / 5\n",n+3);
puts("6 + 7");
for(int i=5;i<=8;i++)
printf("%d + %d\n",n+i,i+3);
printf("%d / 12\n",n+9);
printf("%d * %d\n",n+4,n+10);
int x=n+11,y=13;
for(int i=1;i<=(n-12)/2;i++){
printf("%d * %d\n",x++,y++);
printf("%d / %d\n",x++,y++);
}
}
}
return 0;
}