Guest User

StonePuzzle

a guest
Oct 31st, 2011
166
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class StonePuzzle {
  2. public static final int N = 40, signum[] = {-1, 0, 1};
  3.  
  4. public static void main(String[] args) {
  5. long success = (long) Math.pow(2, N) - 1;
  6. for (int p1 = 1; p1 < N; p1++)
  7. for (int p2 = p1; p2 < N; p2++)
  8. for (int p3 = p2; p3 < N; p3++) {
  9. int p4 = N - p1 - p2 - p3;
  10. if (p4 < p3) continue;
  11. long trial = doWeighting(p1, p2, p3, p4);
  12. if ((success & trial) == success)
  13. System.out.println(String.format("%s %s %s %s", p1, p2, p3, p4));
  14. }
  15. }
  16.  
  17. private static long doWeighting(int p1, int p2, int p3, int p4) {
  18. long mask = 0L;
  19. for (int s1 : signum)
  20. for (int s2 : signum)
  21. for (int s3 : signum)
  22. for (int s4 : signum) {
  23. int result = s1 * p1 + s2 * p2 + s3 * p3 + s4 * p4;
  24. if (result > 0 && result <= N) mask |= (long) 1 << (result - 1);
  25. }
  26. return mask;
  27. }
  28. }
  29.  
  30.  
RAW Paste Data