/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
static int idCount = 0;
static class State implements Comparable<State>
{
final int id = idCount++;
final int bit;
final State[] parent;
State
(int bit,
String s, State...
parent) {
this.bit = bit;
this.type = s;
this.parent = parent;
}
@Override
{
return text;
}
@Override
public int compareTo(State o)
{
}
@Override
public boolean equals
(Object o
) {
return o instanceof State & ((State) o).bit == bit;
}
@Override
public int hashCode()
{
return bit;
}
{
return Integer.
toBinaryString(bit
| 0b100000000
).
substring(1); }
private void set(Set<State> set)
{
set.add(this);
for (State s : parent)
s.set(set);
}
}
static boolean or(State a, State b, List<State> list)
{
int bit = a.bit | b.bit;
for (State s : list)
{
if (s.bit == bit) return false;
}
list.add(new State(bit, "or", a, b));
return true;
}
static boolean and(State a, State b, List<State> list)
{
int bit = a.bit & b.bit;
for (State s : list)
{
if (s.bit == bit) return false;
}
list.add(new State(bit, "and", a, b));
return true;
}
static boolean not(State a, List<State> list)
{
int bit = a.bit ^ 0b11111111;
for (State s : list)
{
if (s.bit == bit) return false;
}
list.add(new State(bit, "not", a));
return true;
}
static void combination(List<State> list)
{
int n = 1;
while (true)
{
int size = list.size();
for (int i = 0; i < size - 1; i++)
{
State a = list.get(i);
for (int j = n; j < size; j++)
{
and(a, list.get(j), list);
}
}
for (int i = 0; i < size - 1; i++)
{
State a = list.get(i);
for (int j = n; j < size; j++)
{
or(a, list.get(j), list);
}
}
if (list.size() == size) return;
n = size;
}
}
public static void main
(String[] args
) {
State x = new State(0b00001111, " x");
State y = new State(0b00110011, " y");
State z = new State(0b01010101, " z");
State notX = new State(0b11110000, "x'");
State notY = new State(0b11001100, "y'");
State notZ = new State(0b10101010, "z'");
ArrayList
<State
> orand
= new ArrayList
<>(Arrays.
asList(x, y, z
)); combination(orand);
for (int n1 = 0; n1 < orand.size(); n1++)
{
ArrayList<State> orandnot = new ArrayList<>(orand);
if (!not(orand.get(n1), orandnot)) continue;
combination(orandnot);
for (int n2 = 0; n2 < orandnot.size(); n2++)
{
ArrayList<State> result = new ArrayList<>(orandnot);
if (!not(orandnot.get(n2), result)) continue;
combination(result);
if (result.contains(notX) && result.contains(notY) && result.contains(notZ))
{
for (State s : result)
{
if (s.equals(notX)) notX = s;
else if (s.equals(notY)) notY = s;
else if (s.equals(notZ)) notZ = s;
}
TreeSet<State> set = new TreeSet<>();
notX.set(set);
notY.set(set);
notZ.set(set);
int notCount = 0, orCount = 0, andCount = 0;
for (State s : set)
{
if (s.type == "not")
{
s.
name = String.
format("N%02d",
++notCount
); s.
text = String.
format("%s = not(%s) : %s =~%s", s.
name, s.
parent[0].
name, s.
bit(), s.
parent[0].
bit()); }
else if (s.type == "or")
{
s.
name = String.
format("O%02d",
++orCount
); s.
text = String.
format("%s = or(%s, %s) : %s = %s | %s", s.
name, s.
parent[0].
name, s.
parent[1].
name, s.
bit(), s.
parent[0].
bit(), s.
parent[1].
bit()); }
else if (s.type == "and")
{
s.
name = String.
format("A%02d",
++andCount
); s.
text = String.
format("%s = and(%s, %s) : %s = %s & %s", s.
name, s.
parent[0].
name, s.
parent[1].
name, s.
bit(), s.
parent[0].
bit(), s.
parent[1].
bit()); }
else
{
s.name = s.type;
s.
text = String.
format("%s: %s", s.
name, s.
bit()); }
}
System.
out.
printf("x' = %s%n", notX.
name); System.
out.
printf("y' = %s%n", notY.
name); System.
out.
printf("z' = %s%n", notZ.
name); System.
out.
printf("not=%d, or=%d, and=%d%n", notCount, orCount, andCount
); }
}
}
}
}
LyogcGFja2FnZSB3aGF0ZXZlcjsgLy8gZG9uJ3QgcGxhY2UgcGFja2FnZSBuYW1lISAqLwoKaW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS5sYW5nLio7CmltcG9ydCBqYXZhLmlvLio7CgovKiBOYW1lIG9mIHRoZSBjbGFzcyBoYXMgdG8gYmUgIk1haW4iIG9ubHkgaWYgdGhlIGNsYXNzIGlzIHB1YmxpYy4gKi8KY2xhc3MgSWRlb25lCnsKICAgIHN0YXRpYyBpbnQgaWRDb3VudCA9IDA7CgogICAgc3RhdGljIGNsYXNzIFN0YXRlIGltcGxlbWVudHMgQ29tcGFyYWJsZTxTdGF0ZT4KICAgIHsKICAgICAgICBmaW5hbCBpbnQgaWQgPSBpZENvdW50Kys7CiAgICAgICAgZmluYWwgaW50IGJpdDsKICAgICAgICBmaW5hbCBTdGF0ZVtdIHBhcmVudDsKICAgICAgICBTdHJpbmcgdHlwZTsKICAgICAgICBTdHJpbmcgbmFtZTsKICAgICAgICBTdHJpbmcgdGV4dDsKCiAgICAgICAgU3RhdGUoaW50IGJpdCwgU3RyaW5nIHMsIFN0YXRlLi4uIHBhcmVudCkKICAgICAgICB7CiAgICAgICAgICAgIHRoaXMuYml0ID0gYml0OwogICAgICAgICAgICB0aGlzLnR5cGUgPSBzOwogICAgICAgICAgICB0aGlzLnBhcmVudCA9IHBhcmVudDsKICAgICAgICB9CgogICAgICAgIEBPdmVycmlkZQogICAgICAgIHB1YmxpYyBTdHJpbmcgdG9TdHJpbmcoKQogICAgICAgIHsKICAgICAgICAgICAgcmV0dXJuIHRleHQ7CiAgICAgICAgfQoKICAgICAgICBAT3ZlcnJpZGUKICAgICAgICBwdWJsaWMgaW50IGNvbXBhcmVUbyhTdGF0ZSBvKQogICAgICAgIHsKICAgICAgICAgICAgcmV0dXJuIEludGVnZXIuY29tcGFyZShpZCwgby5pZCk7CiAgICAgICAgfQoKICAgICAgICBAT3ZlcnJpZGUKICAgICAgICBwdWJsaWMgYm9vbGVhbiBlcXVhbHMoT2JqZWN0IG8pCiAgICAgICAgewogICAgICAgICAgICByZXR1cm4gbyBpbnN0YW5jZW9mIFN0YXRlICYgKChTdGF0ZSkgbykuYml0ID09IGJpdDsKICAgICAgICB9CgogICAgICAgIEBPdmVycmlkZQogICAgICAgIHB1YmxpYyBpbnQgaGFzaENvZGUoKQogICAgICAgIHsKICAgICAgICAgICAgcmV0dXJuIGJpdDsKICAgICAgICB9CgogICAgICAgIFN0cmluZyBiaXQoKQogICAgICAgIHsKICAgICAgICAgICAgcmV0dXJuIEludGVnZXIudG9CaW5hcnlTdHJpbmcoYml0IHwgMGIxMDAwMDAwMDApLnN1YnN0cmluZygxKTsKICAgICAgICB9CgogICAgICAgIHByaXZhdGUgdm9pZCBzZXQoU2V0PFN0YXRlPiBzZXQpCiAgICAgICAgewogICAgICAgICAgICBzZXQuYWRkKHRoaXMpOwogICAgICAgICAgICBmb3IgKFN0YXRlIHMgOiBwYXJlbnQpCiAgICAgICAgICAgICAgICBzLnNldChzZXQpOwogICAgICAgIH0KICAgIH0KCiAgICBzdGF0aWMgYm9vbGVhbiBvcihTdGF0ZSBhLCBTdGF0ZSBiLCBMaXN0PFN0YXRlPiBsaXN0KQogICAgewogICAgICAgIGludCBiaXQgPSBhLmJpdCB8IGIuYml0OwogICAgICAgIGZvciAoU3RhdGUgcyA6IGxpc3QpCiAgICAgICAgewogICAgICAgICAgICBpZiAocy5iaXQgPT0gYml0KSByZXR1cm4gZmFsc2U7CiAgICAgICAgfQogICAgICAgIGxpc3QuYWRkKG5ldyBTdGF0ZShiaXQsICJvciIsIGEsIGIpKTsKICAgICAgICByZXR1cm4gdHJ1ZTsKICAgIH0KCiAgICBzdGF0aWMgYm9vbGVhbiBhbmQoU3RhdGUgYSwgU3RhdGUgYiwgTGlzdDxTdGF0ZT4gbGlzdCkKICAgIHsKICAgICAgICBpbnQgYml0ID0gYS5iaXQgJiBiLmJpdDsKICAgICAgICBmb3IgKFN0YXRlIHMgOiBsaXN0KQogICAgICAgIHsKICAgICAgICAgICAgaWYgKHMuYml0ID09IGJpdCkgcmV0dXJuIGZhbHNlOwogICAgICAgIH0KICAgICAgICBsaXN0LmFkZChuZXcgU3RhdGUoYml0LCAiYW5kIiwgYSwgYikpOwogICAgICAgIHJldHVybiB0cnVlOwogICAgfQoKICAgIHN0YXRpYyBib29sZWFuIG5vdChTdGF0ZSBhLCBMaXN0PFN0YXRlPiBsaXN0KQogICAgewogICAgICAgIGludCBiaXQgPSBhLmJpdCBeIDBiMTExMTExMTE7CiAgICAgICAgZm9yIChTdGF0ZSBzIDogbGlzdCkKICAgICAgICB7CiAgICAgICAgICAgIGlmIChzLmJpdCA9PSBiaXQpIHJldHVybiBmYWxzZTsKICAgICAgICB9CiAgICAgICAgbGlzdC5hZGQobmV3IFN0YXRlKGJpdCwgIm5vdCIsIGEpKTsKICAgICAgICByZXR1cm4gdHJ1ZTsKICAgIH0KCiAgICBzdGF0aWMgdm9pZCBjb21iaW5hdGlvbihMaXN0PFN0YXRlPiBsaXN0KQogICAgewogICAgICAgIGludCBuID0gMTsKICAgICAgICB3aGlsZSAodHJ1ZSkKICAgICAgICB7CiAgICAgICAgICAgIGludCBzaXplID0gbGlzdC5zaXplKCk7CiAgICAgICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgc2l6ZSAtIDE7IGkrKykKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgU3RhdGUgYSA9IGxpc3QuZ2V0KGkpOwogICAgICAgICAgICAgICAgZm9yIChpbnQgaiA9IG47IGogPCBzaXplOyBqKyspCiAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgYW5kKGEsIGxpc3QuZ2V0KGopLCBsaXN0KTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IHNpemUgLSAxOyBpKyspCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIFN0YXRlIGEgPSBsaXN0LmdldChpKTsKICAgICAgICAgICAgICAgIGZvciAoaW50IGogPSBuOyBqIDwgc2l6ZTsgaisrKQogICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgIG9yKGEsIGxpc3QuZ2V0KGopLCBsaXN0KTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgICBpZiAobGlzdC5zaXplKCkgPT0gc2l6ZSkgcmV0dXJuOwogICAgICAgICAgICBuID0gc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AgICAgICAgIGlmICghbm90KG9yYW5kbm90LmdldChuMiksIHJlc3VsdCkpIGNvbnRpbnVlOwogICAgICAgICAgICAgICAgY29tYmluYXRpb24ocmVzdWx0KTsKCiAgICAgICAgICAgICAgICBpZiAocmVzdWx0LmNvbnRhaW5zKG5vdFgpICYmIHJlc3VsdC5jb250YWlucyhub3RZKSAmJiByZXN1bHQuY29udGFpbnMobm90WikpCiAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgZm9yIChTdGF0ZSBzIDogcmVzdWx0KQogICAgICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAgICAgaWYgKHMuZXF1YWxzKG5vdFgpKSBub3RYID0gczsKICAgICAgICAgICAgICAgICAgICAgICAgZWxzZSBpZiAocy5lcXVhbHMobm90WSkpIG5vdFkgPSBzOwogICAgICAgICAgICAgICAgICAgICAgICBlbHNlIGlmIChzLmVxdWFscyhub3RaKSkgbm90WiA9IHM7CiAgICAgICAgICAgICAgICAgICAgfQoKICAgICAgICAgICAgICAgICAgICBUcmVlU2V0PFN0YXRlPiBzZXQgPSBuZXcgVHJlZVNldDw+KCk7CiAgICAgICAgICAgICAgICAgICAgbm90WC5zZXQoc2V0KTsKICAgICAgICAgICAgICAgICAgICBub3RZLnNldChzZXQpOwogICAgICAgICAgICAgICAgICAgIG5vdFouc2V0KHNldCk7CiAgICAgICAgICAgICAgICAgICAgaW50IG5vdENvdW50ID0gMCwgb3JDb3VudCA9IDAsIGFuZENvdW50ID0gMDsKICAgICAgICAgICAgICAgICAgICBmb3IgKFN0YXRlIHMgOiBzZXQpCiAgICAgICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgICAgICBpZiAocy50eXBlID09ICJub3QiKQogICAgICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBzLm5hbWUgPSBTdHJpbmcuZm9ybWF0KCJOJTAyZCIsICsrbm90Q291bnQpOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgcy50ZXh0ID0gU3RyaW5nLmZvcm1hdCgiJXMgPSBub3QoJXMpICAgICAgOiAlcyA9fiVzIiwgcy5uYW1lLCBzLnBhcmVudFswXS5uYW1lLCBzLmJpdCgpLCBzLnBhcmVudFswXS5iaXQoKSk7CiAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgZWxzZSBpZiAocy50eXBlID09ICJvciIpCiAgICAgICAgICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIHMubmFtZSA9IFN0cmluZy5mb3JtYXQoIk8lMDJkIiwgKytvckNvdW50KTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIHMudGV4dCA9IFN0cmluZy5mb3JtYXQoIiVzID0gIG9yKCVzLCAlcykgOiAlcyA9ICVzIHwgJXMiLCBzLm5hbWUsIHMucGFyZW50WzBdLm5hbWUsIHMucGFyZW50WzFdLm5hbWUsIHMuYml0KCksIHMucGFyZW50WzBdLmJpdCgpLCBzLnBhcmVudFsxXS5iaXQoKSk7CiAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgZWxzZSBpZiAocy50eXBlID09ICJhbmQiKQogICAgICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBzLm5hbWUgPSBTdHJpbmcuZm9ybWF0KCJBJTAyZCIsICsrYW5kQ291bnQpOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgcy50ZXh0ID0gU3RyaW5nLmZvcm1hdCgiJXMgPSBhbmQoJXMsICVzKSA6ICVzID0gJXMgJiAlcyIsIHMubmFtZSwgcy5wYXJlbnRbMF0ubmFtZSwgcy5wYXJlbnRbMV0ubmFtZSwgcy5iaXQoKSwgcy5wYXJlbnRbMF0uYml0KCksIHMucGFyZW50WzFdLmJpdCgpKTsKICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICBlbHNlCiAgICAgICAgICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIHMubmFtZSA9IHMudHlwZTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIHMudGV4dCA9IFN0cmluZy5mb3JtYXQoIiVzOiAlcyIsIHMubmFtZSwgcy5iaXQoKSk7CiAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKHMpOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICBTeXN0ZW0ub3V0LnByaW50ZigieCcgPSAlcyVuIiwgbm90WC5uYW1lKTsKICAgICAgICAgICAgICAgICAgICBTeXN0ZW0ub3V0LnByaW50ZigieScgPSAlcyVuIiwgbm90WS5uYW1lKTsKICAgICAgICAgICAgICAgICAgICBTeXN0ZW0ub3V0LnByaW50ZigieicgPSAlcyVuIiwgbm90Wi5uYW1lKTsKICAgICAgICAgICAgICAgICAgICBTeXN0ZW0ub3V0LnByaW50Zigibm90PSVkLCBvcj0lZCwgYW5kPSVkJW4iLCBub3RDb3VudCwgb3JDb3VudCwgYW5kQ291bnQpOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQp9