Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class StonePuzzle {
- public static final int N = 40, signum[] = {-1, 0, 1};
- public static void main(String[] args) {
- long success = (long) Math.pow(2, N) - 1;
- for (int p1 = 1; p1 < N; p1++)
- for (int p2 = p1; p2 < N; p2++)
- for (int p3 = p2; p3 < N; p3++) {
- int p4 = N - p1 - p2 - p3;
- if (p4 < p3) continue;
- long trial = doWeighting(p1, p2, p3, p4);
- if ((success & trial) == success)
- System.out.println(String.format("%s %s %s %s", p1, p2, p3, p4));
- }
- }
- private static long doWeighting(int p1, int p2, int p3, int p4) {
- long mask = 0L;
- for (int s1 : signum)
- for (int s2 : signum)
- for (int s3 : signum)
- for (int s4 : signum) {
- int result = s1 * p1 + s2 * p2 + s3 * p3 + s4 * p4;
- if (result > 0 && result <= N) mask |= (long) 1 << (result - 1);
- }
- return mask;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement