HDU 5308 I Wanna Become A 24-Point Master

来源:互联网 发布:已知矩阵的秩求未知数 编辑:程序博客网 时间:2024/06/06 16:41

I Wanna Become A 24-Point Master


Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)


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.
 

Input
There are no more then 100 testcases and there are no more then 5 testcases with n100. Each testcase contains only one integer n (1n105)
 

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 2n1 numbers and at firsrt Ai=n (1in). You need to print n1 lines and the ith line contains one integer a, one char band then one integer c, where 1a,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. A2n1=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 * 25 + 36 + 4

 凑24,对于n个数,我们需要用前7个数凑出6,用前5个数凑出4,然后相乘得到24,用第13、第14凑出0,用0与其余数相乘,最后24+0得到24。所以对于n小于14时,可以采用直接打表的方式或者DFS爆搜,大于14就可以按照套路直接得到24了。

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define maxn 100010#define ll long longusing namespace std;int n;char a[maxn];bool dfs(int num, int dep){        //cout<<num<<" "<<dep<<endl;        if (dep == n - 1 && num == n)        {                return true;        }        if (dep == n - 1)        {                return false;        }        if (num - n >= 0)        {                if (dfs(num - n, dep + 1))                {                        a[dep] = '+';                        return true;                }        }        if (num + n <= 1e9)        {                if (dfs(num + n, dep + 1))                {                        a[dep] = '-';                        return true;                }        }        if (num % n == 0)        {                if (dfs(num / n, dep + 1))                {                        a[dep] = '*';                        return true;                }        }        if (num * n <= 1e9)        {                if (dfs(num * n, dep + 1))                {                        a[dep] = '/';                        return true;                }        }        return false;}int main(){        while (scanf("%d", &n) != EOF)        {                if (n <= 13)                {                        if (dfs(24, 0))                        {                                int k = 2;                                for (int i = n - 2; i >= 0; i--)                                {                                        if (i == n - 2)                                        {                                                printf("%d %c %d\n", 1, a[i], k);                                        }                                        else                                        {                                                printf("%d %c %d\n", k + n - 2, a[i], ++k);                                        }                                }                        }                        else                        {                                printf("-1\n");                        }                }                else                {                        puts("1 + 2");                        printf("%d + 3\n", n + 1);                        printf("%d + 4\n", n + 2);                        printf("%d + 5\n", n + 3);                        printf("%d + 6\n", n + 4);                        printf("%d / 7\n", n + 5);                        puts("8 + 9");                        printf("%d + 10\n", n + 7);                        printf("%d + 11\n", n + 8);                        printf("%d / 12\n", n + 9);                        printf("%d * %d\n", n + 6, n + 10);                        puts("13 - 14");                        int tep = n + 12;                        for (int i = 15; i <= n; i++)                        {                                printf("%d * %d\n", i, tep);                                tep++;                        }                        printf("%d + %d\n", n + 11, tep);                }        }        return 0;}


0 0
原创粉丝点击