Description
You've designed a computer and implemented all the common arithmetic operators: addition, subtraction, multiplication and integer division. However, your budget was very limited, so you could only afford to place a single register in the computer. The register can store any non-negative integer value. Since there is only one register, there is no need to identify the store location or the operands of each operation or its result. The programming language has four instructions: '+', '-', '*' and '/'. Each instruction performs the corresponding operation using the value in the register as both its parameters. It then stores the result in the same register, overwriting the previous content.
A program for your computer is a sequential list of zero or more instructions. You want to show that, even with its limitations, your newly constructed computer is powerful. You will be given two ints s and t. Return the shortest program that finishes with a value of t in the register if it contained s before executing. If there is more than one possible answer, return the one that comes earliest lexicographically. If there is no program that can do the job, return ":-(" (quotes for clarity) instead.
Input
There are at most 100 cases,each case one line contain 2 space seperated integers S and T. (1 <= S,T <= 10^9)
Output
Output the shortest program that finishes with a value of t in the register if it contained s before executing. If there is more than one possible answer, return the one that comes earliest lexicographically. If there is no program that can do the job, return ":-(" (quotes for clarity) instead.
Sample Input
7 392 7 256 4 256 7 9
Sample Output
+*+ /+*** ** :-(
//--------------------------------------------------------------------------------------
简单广搜题,却被我做的很悲剧,无限WA。今天可耻地去看了测试数据才发现错在哪里。
//----------------------------------------
起初思路是:
1如果目标数可以拆成起始数与2的积就用*、+向上广搜
2如果不能但目标数因数只有2,先除成1后再向上广搜
3以上不符合或者搜不到就输出:-(
错误:以为能直接向上搜到的话,直接向上搜肯定比除成1后再向上搜要快,其实不是。
比如,起始数为2^n,目标数为2^(2n-1),起始数要不断的加才能上去,但除成1后可以用乘,当n比较大时,后者会较快到达目标。
//----------------------------------------
于是改了下,不去分情况,直接广搜。
因为除法只可能在第一次用,所以把起始数插进队列后,把1和除号也插进队列(脑子果断进水),依然WA。
错误:假设两种方法以同样的步数到目标,一个/开头,一个*开头,按字典序应该输出后者,但由于/是先插进去的,反而会输出前者。
//----------------------------------------
最后还是回到最老老实实的广搜,把起始数插入队列,先插乘再插加最后除没插过就插下除,唉~~~
//----------------------------------------
每次陷进一个错误出来后都会很混乱,而且没有从头理下头绪的习惯,只是在原来的基础上改下去。这种错误跟着代码走一遍就能发现的,下次要冷静点了,唉~~~