fork(4) download
  1. import java.io.BufferedWriter;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.io.OutputStream;
  5. import java.io.OutputStreamWriter;
  6. import java.io.PrintWriter;
  7. import java.io.Writer;
  8. import java.util.InputMismatchException;
  9. import java.util.StringTokenizer;
  10. public class Main {
  11.  
  12. public interface SpaceCharFilter {
  13. public boolean isSpaceChar(int ch);
  14. }
  15. class InputReader {
  16. private InputStream stream;
  17. private byte[] buf = new byte[1024];
  18. private int curChar;
  19. private int numChars;
  20. private SpaceCharFilter filter;
  21. public InputReader(InputStream stream) {
  22. this.stream = stream;
  23. }
  24. public int read() {
  25.  
  26. if (numChars == -1)
  27.  
  28. throw new InputMismatchException();
  29.  
  30. if (curChar >= numChars) {
  31.  
  32. curChar = 0;
  33.  
  34. try {
  35.  
  36. numChars = stream.read(buf);
  37.  
  38. } catch (IOException e) {
  39.  
  40. throw new InputMismatchException();
  41.  
  42. }
  43.  
  44. if (numChars <= 0)
  45.  
  46. return -1;
  47. }
  48. return buf[curChar++];
  49. }
  50. public int readInt() {
  51.  
  52. int c = read();
  53.  
  54. while (isSpaceChar(c))
  55.  
  56. c = read();
  57. int sgn = 1;
  58. if (c == '-') {
  59. sgn = -1;
  60. c = read();
  61. }
  62. int res = 0;
  63. do {
  64. if (c < '0' || c > '9')
  65. throw new InputMismatchException();
  66. res *= 10;
  67. res += c - '0';
  68. c = read();
  69. } while (!isSpaceChar(c));
  70. return res * sgn;
  71. }
  72. public long readLong() {
  73. int c = read();
  74. while (isSpaceChar(c))
  75. c = read();
  76. int sgn = 1;
  77. if (c == '-') {
  78. sgn = -1;
  79. c = read();
  80. }
  81. long res = 0;
  82. do {
  83. if (c < '0' || c > '9')
  84. throw new InputMismatchException();
  85. res *= 10;
  86. res += c - '0';
  87. c = read();
  88. } while (!isSpaceChar(c));
  89. return res * sgn;
  90. }
  91. public String readString() {
  92. int c = read();
  93. while (isSpaceChar(c))
  94. c = read();
  95. StringBuilder res = new StringBuilder();
  96. do {
  97. res.appendCodePoint(c);
  98. c = read();
  99. } while (!isSpaceChar(c));
  100. return res.toString();
  101. }
  102. public boolean isSpaceChar(int c) {
  103. if (filter != null)
  104. return filter.isSpaceChar(c);
  105. return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
  106. }
  107. public String next() {
  108. return readString();
  109. }
  110. }
  111. class OutputWriter {
  112. private final PrintWriter writer;
  113. public OutputWriter(OutputStream outputStream) {
  114. writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
  115. }
  116. public OutputWriter(Writer writer) {
  117. this.writer = new PrintWriter(writer);
  118. }
  119. public void print(Object...objects) {
  120. for (int i = 0; i < objects.length; i++) {
  121. if (i != 0)
  122. writer.print(' ');
  123. writer.print(objects[i]);
  124. }
  125. }
  126. public void printLine(Object...objects) {
  127. print(objects);
  128. writer.println();
  129. }
  130. public void close() {
  131. writer.close();
  132. }
  133. public void flush() {
  134. writer.flush();
  135. }
  136. }
  137. /**
  138.   * @param args
  139.   */
  140. InputReader in= new InputReader(System.in);
  141. OutputWriter out = new OutputWriter(System.out);
  142. public static void main(String[] args) throws IOException
  143. {
  144. new Main().run();
  145. }
  146. void run() throws IOException
  147. {
  148. solve();
  149. out.flush();
  150. tok=null;
  151. }
  152.  
  153. public void printBinary(long x){
  154. String str=Long.toBinaryString(x);
  155. int len=str.length();
  156. for (int k=len-2;k>=0;k--){
  157. if (str.charAt(k)=='1'){
  158. out.print(len-1-k+" ");
  159. }
  160. }
  161. out.printLine();
  162. }
  163. void solve() throws IOException{
  164.  
  165. long[] dp=new long[1000*60+1];
  166. int[] V=new int[60];
  167. dp[0]=1;
  168. int N,Q;
  169. N=in.readInt();
  170. Q=in.readInt();
  171. while (N+Q>0){
  172. int mSum=0;
  173.  
  174. for (int i=0;i<N;++i) {
  175. V[i]=in.readInt();
  176. mSum+=V[i];
  177. }
  178. for (int i=0;i<=mSum;++i) dp[i]=0;
  179. dp[0]=1;
  180. for (int i=0;i<N;++i){
  181. for (int a=mSum;a>=V[i];a--){
  182. dp[a]|=((dp[a-V[i]])<<1);
  183. }
  184. }
  185. for (int q=0;q<Q;++q){
  186. int qi=in.readInt();
  187. if (qi>mSum) {
  188. out.printLine("That's impossible!");
  189. }else{
  190. if (dp[qi]==0){
  191. out.printLine("That's impossible!");
  192. }else{
  193.  
  194. printBinary(dp[qi]);
  195. }}
  196.  
  197. }
  198. N=in.readInt();
  199. Q=in.readInt();
  200. }
  201. }
  202. }
Runtime error #stdin #stdout #stderr 0.1s 320256KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Exception in thread "main" java.util.InputMismatchException
	at Main$InputReader.read(Main.java:28)
	at Main$InputReader.readInt(Main.java:56)
	at Main.solve(Main.java:170)
	at Main.run(Main.java:149)
	at Main.main(Main.java:145)