Advertisement
Guest User

Untitled

a guest
Mar 31st, 2015
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.02 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6.  
  7. package lightswitch;
  8.  
  9. import java.io.File;
  10. import java.io.FileNotFoundException;
  11. import java.math.BigInteger;
  12. import java.util.Arrays;
  13. import java.util.Scanner;
  14.  
  15. /**
  16. *
  17. * @author ASUS
  18. */
  19. public class LightSwitch {
  20.  
  21. /**
  22. * @param args the command line arguments
  23. */
  24. public static char flip(char a){
  25. if (a=='0')return '1';
  26. return '0';
  27. }
  28. public static void main(String[] args) throws FileNotFoundException {
  29. Scanner sc=new Scanner(new File("out"));
  30.  
  31. // Scanner sc=new Scanner(System.in);
  32. int tst=0;
  33. while (sc.hasNext()){
  34. tst++;
  35. int p1=0,p2=0;
  36. BigInteger bi=sc.nextBigInteger();
  37. char[] str1 = bi.toString(2).toCharArray();
  38. bi=sc.nextBigInteger();
  39. char[] str2 = bi.toString(2).toCharArray();
  40. int len=Math.max(str1.length,str2.length);
  41. StringBuilder str1b=new StringBuilder();
  42. for (int i=0;i<len-str1.length;++i){
  43. str1b.append("0");
  44. }
  45. str1b.append(str1);
  46. // System.out.println(str1b.toString());
  47. str1=str1b.toString().toCharArray();
  48. str1b=new StringBuilder();
  49. for (int i=0;i<len-str2.length;++i){
  50. str1b.append("0");
  51. }
  52. str1b.append(str2);
  53. // System.out.println(str1b.toString());
  54. str2=str1b.toString().toCharArray();
  55. if (str1.length==1){
  56. if (str1[0]==str2[0]){
  57. if (str1[0]=='0')break;
  58. System.out.print("Case Number "+tst+": ");
  59.  
  60. System.out.println(0);
  61.  
  62. }
  63. else {
  64. System.out.print("Case Number "+tst+": ");
  65. System.out.println(1);
  66. }
  67. System.out.println();
  68. continue;
  69. }
  70.  
  71. System.out.print("Case Number "+tst+": ");
  72.  
  73. char[] str=Arrays.copyOf(str1, str1.length);
  74. int ans=1000000000;
  75. BigInteger moves=new BigInteger("-1");
  76. StringBuilder X=new StringBuilder("0");
  77. int sz=str.length;// , sol1=0 , sol2=1;
  78.  
  79. for(int j=1;j<sz;j++){
  80. if(str[j-1]!=str2[j-1]){
  81. str[j-1]=flip(str[j-1]);
  82. str[j]=flip(str[j]);
  83. if(j!=sz-1) str[j+1]=flip(str[j+1]);
  84. // sol1++;
  85.  
  86. X.append("1");
  87. }
  88. else
  89. X.append("0");
  90. }
  91. String s1=new String(str);
  92. String s2=new String(str2);
  93. if (s1.equals(s2)){
  94. p1=1;
  95. // ans=sol1;
  96. moves=new BigInteger(X.toString(),2);
  97. // System.out.println(moves);
  98. }
  99. //
  100.  
  101. //-------------------------------------------------------
  102.  
  103.  
  104.  
  105. X=new StringBuilder("1");
  106. str=Arrays.copyOf(str1, str1.length);
  107. str[0]=flip(str[0]); str[1]=flip(str[1]);
  108. for(int j=1;j<sz;j++){
  109. if(str[j-1]!=str2[j-1]){
  110. str[j-1]=flip(str[j-1]);
  111. str[j]=flip(str[j]);
  112. if(j!=sz-1) str[j+1]=flip(str[j+1]);
  113. // sol2++;
  114.  
  115. X.append("1");
  116. }
  117. else
  118. X.append("0");
  119. }
  120. // System.out.println(str);
  121.  
  122.  
  123.  
  124. //-------------------------------------------------------
  125. s1=new String(str);
  126. s2=new String(str2);
  127. if (s1.equals(s2)){
  128. p2=1;
  129. // if (sol2<ans){
  130. // ans=sol2;
  131. // moves=new BigInteger(X.toString(),2);
  132. // }
  133. // else if (sol2==ans){
  134. BigInteger moves2=new BigInteger(X.toString(),2);
  135. if (moves.equals(new BigInteger("-1"))||moves.compareTo(moves2)>0)
  136. moves=moves2;
  137. //}
  138. }
  139.  
  140. if (moves.equals(new BigInteger("-1"))){
  141. System.out.println("impossible");
  142. }
  143. else {
  144. // System.out.println(ans);
  145. // System.out.println(sol1+" " +p1);
  146. // System.out.println(sol2+" "+p2);
  147. //
  148. System.out.println(moves);
  149. }
  150.  
  151. System.out.println();
  152. }
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159. // TODO code application logic here
  160. }
  161.  
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement