fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static String[] splitBinary(String binary) {
  11. char[] first = new char[binary.length()];
  12. char[] second = new char[binary.length()];
  13.  
  14. Arrays.fill(first, '0');
  15. Arrays.fill(second, '0');
  16.  
  17. boolean taken = false;
  18.  
  19. for (int i = 0; i < binary.length(); i++) {
  20. if (binary.charAt(i) == '1') {
  21. if (!taken) {
  22. first[i] = '1';
  23. taken = true;
  24. } else {
  25. second[i] = '1';
  26. }
  27. }
  28. }
  29.  
  30. return new String[]{new String(first), new String(second)};
  31. }
  32.  
  33. public static boolean isPowerOfTwo(int n){
  34. if (n == 0)
  35. return false;
  36.  
  37. while (n != 1) {
  38. if (n % 2 != 0)
  39. return false;
  40. n = n / 2;
  41. }
  42. return true;
  43. }
  44.  
  45. public static void main (String[] args) throws java.lang.Exception
  46. {
  47. // your code goes here
  48. Scanner sc = new Scanner(System.in);
  49.  
  50. int T = sc.nextInt();
  51. for(int i = 0; i < T; i++){
  52. int N = sc.nextInt();
  53. int S = sc.nextInt();
  54.  
  55. if((S % 2) != 0 || S < N){
  56.  
  57. System.out.println(-1);
  58.  
  59. }else{
  60. if(N % 2 == 0){ //even
  61.  
  62. int rem = N - 2;
  63. int pending = S - rem;
  64. System.out.print(pending/2 + " " + pending/2 + " ");
  65. for(int j = 0; j < rem; j++){
  66. System.out.print(1 + " ");
  67. }
  68. System.out.println();
  69. }else{
  70.  
  71. int rem = N - 3;
  72. int pending = S - rem;
  73. if(isPowerOfTwo(pending/2)){
  74. System.out.print(-1);
  75. }else{
  76. System.out.print(pending/2 + " ");
  77. String[] k = splitBinary(Integer.toBinaryString(pending / 2));
  78. System.out.print(Integer.parseInt(k[0], 2) + " " + Integer.parseInt(k[1], 2));
  79. for(int j = 0; j < rem; j++){
  80. System.out.print(1 + " ");
  81. }
  82. }
  83. System.out.println();
  84. }
  85.  
  86. }
  87. }
  88. }
  89. }
Success #stdin #stdout 0.16s 60892KB
stdin
4
4 30
3 14
2 5
5 64
 
stdout
14 14 1 1 
7 4 3
-1
31 16 151 1