/* 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,
BitSet bitset
) {
int bit = a.bit | b.bit;
if (bitset.get(bit)) return false;
list.add(new State(bit, "or", a, b));
bitset.set(bit);
return true;
}
static boolean and
(State a, State b, List
<State
> list,
BitSet bitset
) {
int bit = a.bit & b.bit;
if (bitset.get(bit)) return false;
list.add(new State(bit, "and", a, b));
bitset.set(bit);
return true;
}
static boolean not
(State a, List
<State
> list,
BitSet bitset
) {
int bit = a.bit ^ 0b11111111;
if (bitset.get(bit)) return false;
list.add(new State(bit, "not", a));
bitset.set(bit);
return true;
}
static void combination
(List
<State
> list,
BitSet bitset
) {
int n = 1;
while (true)
{
int size = list.size();
for (int i = 0; i < size - 1; i++)
{
for (int j
= Math.
max(n, i
+ 1); j
< size
; j
++) {
and(list.get(i), list.get(j), list, bitset);
}
}
for (int i = 0; i < size - 1; i++)
{
for (int j
= Math.
max(n, i
+ 1); j
< size
; j
++) {
or(list.get(i), list.get(j), list, bitset);
}
}
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
)); orandSet.set(x.bit);
orandSet.set(y.bit);
orandSet.set(z.bit);
combination(orand, orandSet);
for (int n1 = 0; n1 < orand.size(); n1++)
{
ArrayList<State> orandnot = new ArrayList<>(orand);
if (!not(orand.get(n1), orandnot, orandnotSet)) continue;
combination(orandnot, orandnotSet);
for (int n2 = n1 + 1; n2 < orandnot.size(); n2++)
{
ArrayList<State> result = new ArrayList<>(orandnot);
if (!not(orandnot.get(n2), result, resultSet)) continue;
combination(result, resultSet);
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
); }
}
}
}
}
LyogcGFja2FnZSB3aGF0ZXZlcjsgLy8gZG9uJ3QgcGxhY2UgcGFja2FnZSBuYW1lISAqLwoKaW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS5sYW5nLio7CmltcG9ydCBqYXZhLmlvLio7CgovKiBOYW1lIG9mIHRoZSBjbGFzcyBoYXMgdG8gYmUgIk1haW4iIG9ubHkgaWYgdGhlIGNsYXNzIGlzIHB1YmxpYy4gKi8KY2xhc3MgSWRlb25lCnsKICAgIHN0YXRpYyBpbnQgaWRDb3VudCA9IDA7CgogICAgc3RhdGljIGNsYXNzIFN0YXRlIGltcGxlbWVudHMgQ29tcGFyYWJsZTxTdGF0ZT4KICAgIHsKICAgICAgICBmaW5hbCBpbnQgaWQgPSBpZENvdW50Kys7CiAgICAgICAgZmluYWwgaW50IGJpdDsKICAgICAgICBmaW5hbCBTdGF0ZVtdIHBhcmVudDsKICAgICAgICBTdHJpbmcgdHlwZTsKICAgICAgICBTdHJpbmcgbmFtZTsKICAgICAgICBTdHJpbmcgdGV4dDsKCiAgICAgICAgU3RhdGUoaW50IGJpdCwgU3RyaW5nIHMsIFN0YXRlLi4uIHBhcmVudCkKICAgICAgICB7CiAgICAgICAgICAgIHRoaXMuYml0ID0gYml0OwogICAgICAgICAgICB0aGlzLnR5cGUgPSBzOwogICAgICAgICAgICB0aGlzLnBhcmVudCA9IHBhcmVudDsKICAgICAgICB9CgogICAgICAgIEBPdmVycmlkZQogICAgICAgIHB1YmxpYyBTdHJpbmcgdG9TdHJpbmcoKQogICAgICAgIHsKICAgICAgICAgICAgcmV0dXJuIHRleHQ7CiAgICAgICAgfQoKICAgICAgICBAT3ZlcnJpZGUKICAgICAgICBwdWJsaWMgaW50IGNvbXBhcmVUbyhTdGF0ZSBvKQogICAgICAgIHsKICAgICAgICAgICAgcmV0dXJuIEludGVnZXIuY29tcGFyZShpZCwgby5pZCk7CiAgICAgICAgfQoKICAgICAgICBAT3ZlcnJpZGUKICAgICAgICBwdWJsaWMgYm9vbGVhbiBlcXVhbHMoT2JqZWN0IG8pCiAgICAgICAgewogICAgICAgICAgICByZXR1cm4gbyBpbnN0YW5jZW9mIFN0YXRlICYgKChTdGF0ZSkgbykuYml0ID09IGJpdDsKICAgICAgICB9CgogICAgICAgIEBPdmVycmlkZQogICAgICAgIHB1YmxpYyBpbnQgaGFzaENvZGUoKQogICAgICAgIHsKICAgICAgICAgICAgcmV0dXJuIGJpdDsKICAgICAgICB9CgogICAgICAgIFN0cmluZyBiaXQoKQogICAgICAgIHsKICAgICAgICAgICAgcmV0dXJuIEludGVnZXIudG9CaW5hcnlTdHJpbmcoYml0IHwgMGIxMDAwMDAwMDApLnN1YnN0cmluZygxKTsKICAgICAgICB9CgogICAgICAgIHByaXZhdGUgdm9pZCBzZXQoU2V0PFN0YXRlPiBzZXQpCiAgICAgICAgewogICAgICAgICAgICBzZXQuYWRkKHRoaXMpOwogICAgICAgICAgICBmb3IgKFN0YXRlIHMgOiBwYXJlbnQpCiAgICAgICAgICAgICAgICBzLnNldChzZXQpOwogICAgICAgIH0KICAgIH0KCiAgICBzdGF0aWMgYm9vbGVhbiBvcihTdGF0ZSBhLCBTdGF0ZSBiLCBMaXN0PFN0YXRlPiBsaXN0LCBCaXRTZXQgYml0c2V0KQogICAgewogICAgICAgIGludCBiaXQgPSBhLmJpdCB8IGIuYml0OwogICAgICAgIGlmIChiaXRzZXQuZ2V0KGJpdCkpIHJldHVybiBmYWxzZTsKICAgICAgICBsaXN0LmFkZChuZXcgU3RhdGUoYml0LCAib3IiLCBhLCBiKSk7CiAgICAgICAgYml0c2V0LnNldChiaXQpOwogICAgICAgIHJldHVybiB0cnVlOwogICAgfQoKICAgIHN0YXRpYyBib29sZWFuIGFuZChTdGF0ZSBhLCBTdGF0ZSBiLCBMaXN0PFN0YXRlPiBsaXN0LCBCaXRTZXQgYml0c2V0KQogICAgewogICAgICAgIGludCBiaXQgPSBhLmJpdCAmIGIuYml0OwogICAgICAgIGlmIChiaXRzZXQuZ2V0KGJpdCkpIHJldHVybiBmYWxzZTsKICAgICAgICBsaXN0LmFkZChuZXcgU3RhdGUoYml0LCAiYW5kIiwgYSwgYikpOwogICAgICAgIGJpdHNldC5zZXQoYml0KTsKICAgICAgICByZXR1cm4gdHJ1ZTsKICAgIH0KCiAgICBzdGF0aWMgYm9vbGVhbiBub3QoU3RhdGUgYSwgTGlzdDxTdGF0ZT4gbGlzdCwgQml0U2V0IGJpdHNldCkKICAgIHsKICAgICAgICBpbnQgYml0ID0gYS5iaXQgXiAwYjExMTExMTExOwogICAgICAgIGlmIChiaXRzZXQuZ2V0KGJpdCkpIHJldHVybiBmYWxzZTsKICAgICAgICBsaXN0LmFkZChuZXcgU3RhdGUoYml0LCAibm90IiwgYSkpOwogICAgICAgIGJpdHNldC5zZXQoYml0KTsKICAgICAgICByZXR1cm4gdHJ1ZTsKICAgIH0KCiAgICBzdGF0aWMgdm9pZCBjb21iaW5hdGlvbihMaXN0PFN0YXRlPiBsaXN0LCBCaXRTZXQgYml0c2V0KQogICAgewogICAgICAgIGludCBuID0gMTsKICAgICAgICB3aGlsZSAodHJ1ZSkKICAgICAgICB7CiAgICAgICAgICAgIGludCBzaXplID0gbGlzdC5zaXplKCk7CiAgICAgICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgc2l6ZSAtIDE7IGkrKykKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgZm9yIChpbnQgaiA9IE1hdGgubWF4KG4sIGkgKyAxKTsgaiA8IHNpemU7IGorKykKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICBhbmQobGlzdC5nZXQoaSksIGxpc3QuZ2V0KGopLCBsaXN0LCBiaXRzZXQpOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgc2l6ZSAtIDE7IGkrKykKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgZm9yIChpbnQgaiA9IE1hdGgubWF4KG4sIGkgKyAxKTsgaiA8IHNpemU7IGorKykKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICBvcihsaXN0LmdldChpKSwgbGlzdC5nZXQoaiksIGxpc3QsIGJpdHNldCk7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgICAgaWYgKGxpc3Quc2l6ZSgpID09IHNpemUpIHJldHVybjsKICAgICAgICAgICAgbiA9IHNpemU7CiAgICAgICAgfQogICAgfQoKCiAgICBwdWJsaWMgc3RhdGljIHZvaWQgbWFpbihTdHJpbmdbXSBhcmdzKQogICAgewogICAgICAgIFN0YXRlIHggPSBuZXcgU3RhdGUoMGIwMDAwMTExMSwgIiAgeCIpOwogICAgICAgIFN0YXRlIHkgPSBuZXcgU3RhdGUoMGIwMDExMDAxMSwgIiAgeSIpOwogICAgICAgIFN0YXRlIHogPSBuZXcgU3RhdGUoMGIwMTAxMDEwMSwgIiAgeiIpOwogICAgICAgIFN0YXRlIG5vdFggPSBuZXcgU3RhdGUoMGIxMTExMDAwMCwgIngnIik7CiAgICAgICAgU3RhdGUgbm90WSA9IG5ldyBTdGF0ZSgwYjExMDAxMTAwLCAieSciKTsKICAgICAgICBTdGF0ZSBub3RaID0gbmV3IFN0YXRlKDBiMTAxMDEwMTAsICJ6JyIpOwoKICAgICAgICBBcnJheUxpc3Q8U3RhdGU+IG9yYW5kID0gbmV3IEFycmF5TGlzdDw+KEFycmF5cy5hc0xpc3QoeCwgeSwgeikpOwogICAgICAgIEJpdFNldCBvcmFuZFNldCA9IG5ldyBCaXRTZXQoKTsKICAgICAgICBvcmFuZFNldC5zZXQoeC5iaXQpOwogICAgICAgIG9yYW5kU2V0LnNldCh5LmJpdCk7CiAgICAgICAgb3JhbmRTZXQuc2V0KHouYml0KTsKICAgICAgICBjb21iaW5hdGlvbihvcmFuZCwgb3JhbmRTZXQpOwoKICAgICAgICBmb3IgKGludCBuMSA9IDA7IG4xIDwgb3JhbmQuc2l6ZSgpOyBuMSsrKQogICAgICAgIHsKICAgICAgICAgICAgQXJyYXlMaXN0PFN0YXRlPiBvcmFuZG5vdCA9IG5ldyBBcnJheUxpc3Q8PihvcmFuZCk7CiAgICAgICAgICAgIEJpdFNldCBvcmFuZG5vdFNldCA9IChCaXRTZXQpIG9yYW5kU2V0LmNsb25lKCk7CiAgICAgICAgICAgIGlmICghbm90KG9yYW5kLmdldChuMSksIG9yYW5kbm90LCBvcmFuZG5vdFNldCkpIGNvbnRpbnVlOwogICAgICAgICAgICBjb21iaW5hdGlvbihvcmFuZG5vdCwgb3JhbmRub3RTZXQpOwoKICAgICAgICAgICAgZm9yIChpbnQgbjIgPSBuMSArIDE7IG4yIDwgb3JhbmRub3Quc2l6ZSgpOyBuMisrKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBBcnJheUxpc3Q8U3RhdGU+IHJlc3VsdCA9IG5ldyBBcnJheUxpc3Q8PihvcmFuZG5vdCk7CiAgICAgICAgICAgICAgICBCaXRTZXQgcmVzdWx0U2V0ID0gKEJpdFNldCkgb3JhbmRub3RTZXQuY2xvbmUoKTsKICAgICAgICAgICAgICAgIGlmICghbm90KG9yYW5kbm90LmdldChuMiksIHJlc3VsdCwgcmVzdWx0U2V0KSkgY29udGludWU7CiAgICAgICAgICAgICAgICBjb21iaW5hdGlvbihyZXN1bHQsIHJlc3VsdFNldCk7CgogICAgICAgICAgICAgICAgaWYgKHJlc3VsdC5jb250YWlucyhub3RYKSAmJiByZXN1bHQuY29udGFpbnMobm90WSkgJiYgcmVzdWx0LmNvbnRhaW5zKG5vdFopKQogICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgIGZvciAoU3RhdGUgcyA6IHJlc3VsdCkKICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgIGlmIChzLmVxdWFscyhub3RYKSkgbm90WCA9IHM7CiAgICAgICAgICAgICAgICAgICAgICAgIGVsc2UgaWYgKHMuZXF1YWxzKG5vdFkpKSBub3RZID0gczsKICAgICAgICAgICAgICAgICAgICAgICAgZWxzZSBpZiAocy5lcXVhbHMobm90WikpIG5vdFogPSBzOwogICAgICAgICAgICAgICAgICAgIH0KCiAgICAgICAgICAgICAgICAgICAgVHJlZVNldDxTdGF0ZT4gc2V0ID0gbmV3IFRyZWVTZXQ8PigpOwogICAgICAgICAgICAgICAgICAgIG5vdFguc2V0KHNldCk7CiAgICAgICAgICAgICAgICAgICAgbm90WS5zZXQoc2V0KTsKICAgICAgICAgICAgICAgICAgICBub3RaLnNldChzZXQpOwogICAgICAgICAgICAgICAgICAgIGludCBub3RDb3VudCA9IDAsIG9yQ291bnQgPSAwLCBhbmRDb3VudCA9IDA7CiAgICAgICAgICAgICAgICAgICAgZm9yIChTdGF0ZSBzIDogc2V0KQogICAgICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAgICAgaWYgKHMudHlwZSA9PSAibm90IikKICAgICAgICAgICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgcy5uYW1lID0gU3RyaW5nLmZvcm1hdCgiTiUwMmQiLCArK25vdENvdW50KTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIHMudGV4dCA9IFN0cmluZy5mb3JtYXQoIiVzID0gbm90KCVzKSAgICAgIDogJXMgPX4lcyIsIHMubmFtZSwgcy5wYXJlbnRbMF0ubmFtZSwgcy5iaXQoKSwgcy5wYXJlbnRbMF0uYml0KCkpOwogICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgIGVsc2UgaWYgKHMudHlwZSA9PSAib3IiKQogICAgICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBzLm5hbWUgPSBTdHJpbmcuZm9ybWF0KCJPJTAyZCIsICsrb3JDb3VudCk7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBzLnRleHQgPSBTdHJpbmcuZm9ybWF0KCIlcyA9ICBvciglcywgJXMpIDogJXMgPSAlcyB8ICVzIiwgcy5uYW1lLCBzLnBhcmVudFswXS5uYW1lLCBzLnBhcmVudFsxXS5uYW1lLCBzLmJpdCgpLCBzLnBhcmVudFswXS5iaXQoKSwgcy5wYXJlbnRbMV0uYml0KCkpOwogICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgIGVsc2UgaWYgKHMudHlwZSA9PSAiYW5kIikKICAgICAgICAgICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgcy5uYW1lID0gU3RyaW5nLmZvcm1hdCgiQSUwMmQiLCArK2FuZENvdW50KTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIHMudGV4dCA9IFN0cmluZy5mb3JtYXQoIiVzID0gYW5kKCVzLCAlcykgOiAlcyA9ICVzICYgJXMiLCBzLm5hbWUsIHMucGFyZW50WzBdLm5hbWUsIHMucGFyZW50WzFdLm5hbWUsIHMuYml0KCksIHMucGFyZW50WzBdLmJpdCgpLCBzLnBhcmVudFsxXS5iaXQoKSk7CiAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgZWxzZQogICAgICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBzLm5hbWUgPSBzLnR5cGU7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBzLnRleHQgPSBTdHJpbmcuZm9ybWF0KCIlczogJXMiLCBzLm5hbWUsIHMuYml0KCkpOwogICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbihzKTsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgU3lzdGVtLm91dC5wcmludGYoIngnID0gJXMlbiIsIG5vdFgubmFtZSk7CiAgICAgICAgICAgICAgICAgICAgU3lzdGVtLm91dC5wcmludGYoInknID0gJXMlbiIsIG5vdFkubmFtZSk7CiAgICAgICAgICAgICAgICAgICAgU3lzdGVtLm91dC5wcmludGYoInonID0gJXMlbiIsIG5vdFoubmFtZSk7CiAgICAgICAgICAgICAgICAgICAgU3lzdGVtLm91dC5wcmludGYoIm5vdD0lZCwgb3I9JWQsIGFuZD0lZCVuIiwgbm90Q291bnQsIG9yQ291bnQsIGFuZENvdW50KTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KfQ==