import java.util.*;
import static java.
util.
Arrays.
*;
public class BottlesOnShelf {
public int getNumBroken(int N, int[] left, int[] right, int[] damage) {
int M = damage.length;
int[] xs = new int[2 * M];
for (int i = 0; i < M; i++) {
xs[2 * i] = --left[i];
xs[2 * i + 1] = right[i];
}
sort(xs);
L[] on = new L[2 * M - 1];
for (int i = 0; i < 2 * M - 1; i++)
on[i] = new L();
for (int i = 0; i < 2 * M - 1; i++)
if (xs[i] < xs[i + 1])
for (int j = 0; j < M; j++)
if (left[j] <= xs[i] && xs[i + 1] <= right[j])
on[i].add(j);
int ret = 0;
for (int i = 0; i < 2 * M - 1; i++) {
int m = on[i].size();
long s = 0;
loop: for (int j = 1; j < (1 << m); j++) {
L l = new L();
for (int k = 0; k < m; k++)
if ((j & (1 << k)) > 0)
l.add(on[i].get(k));
int gcd = 0;
for (int k : l)
gcd = gcd(damage[k], gcd);
int lcm = gcd;
for (int k : l) {
int d = damage[k] / gcd(damage[k], lcm);
continue loop;
lcm *= d;
}
s += (xs[i + 1] / lcm - xs[i] / lcm)
* (Integer.
bitCount(j
) % 2 == 0 ? -1 : 1); }
ret += s;
}
return ret;
}
class L extends ArrayList<Integer> {};
int gcd(int x, int y) {
return y == 0 ? x : gcd(y, x % y);
}
}
aW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgc3RhdGljIGphdmEudXRpbC5BcnJheXMuKjsKCnB1YmxpYyBjbGFzcyBCb3R0bGVzT25TaGVsZiB7CgogICAgcHVibGljIGludCBnZXROdW1Ccm9rZW4oaW50IE4sIGludFtdIGxlZnQsIGludFtdIHJpZ2h0LCBpbnRbXSBkYW1hZ2UpIHsKICAgICAgICBpbnQgTSA9IGRhbWFnZS5sZW5ndGg7CiAgICAgICAgaW50W10geHMgPSBuZXcgaW50WzIgKiBNXTsKICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IE07IGkrKykgewogICAgICAgICAgICB4c1syICogaV0gPSAtLWxlZnRbaV07CiAgICAgICAgICAgIHhzWzIgKiBpICsgMV0gPSByaWdodFtpXTsKICAgICAgICB9CiAgICAgICAgc29ydCh4cyk7CiAgICAgICAgTFtdIG9uID0gbmV3IExbMiAqIE0gLSAxXTsKICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IDIgKiBNIC0gMTsgaSsrKQogICAgICAgICAgICBvbltpXSA9IG5ldyBMKCk7CiAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCAyICogTSAtIDE7IGkrKykKICAgICAgICAgICAgaWYgKHhzW2ldIDwgeHNbaSArIDFdKQogICAgICAgICAgICAgICAgZm9yIChpbnQgaiA9IDA7IGogPCBNOyBqKyspCiAgICAgICAgICAgICAgICAgICAgaWYgKGxlZnRbal0gPD0geHNbaV0gJiYgeHNbaSArIDFdIDw9IHJpZ2h0W2pdKQogICAgICAgICAgICAgICAgICAgICAgICBvbltpXS5hZGQoaik7CgogICAgICAgIGludCByZXQgPSAwOwogICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgMiAqIE0gLSAxOyBpKyspIHsKICAgICAgICAgICAgaW50IG0gPSBvbltpXS5zaXplKCk7CiAgICAgICAgICAgIGxvbmcgcyA9IDA7CiAgICAgICAgICAgIGxvb3A6IGZvciAoaW50IGogPSAxOyBqIDwgKDEgPDwgbSk7IGorKykgewogICAgICAgICAgICAgICAgTCBsID0gbmV3IEwoKTsKICAgICAgICAgICAgICAgIGZvciAoaW50IGsgPSAwOyBrIDwgbTsgaysrKQogICAgICAgICAgICAgICAgICAgIGlmICgoaiAmICgxIDw8IGspKSA+IDApCiAgICAgICAgICAgICAgICAgICAgICAgIGwuYWRkKG9uW2ldLmdldChrKSk7CgogICAgICAgICAgICAgICAgaW50IGdjZCA9IDA7CiAgICAgICAgICAgICAgICBmb3IgKGludCBrIDogbCkKICAgICAgICAgICAgICAgICAgICBnY2QgPSBnY2QoZGFtYWdlW2tdLCBnY2QpOwoKICAgICAgICAgICAgICAgIGludCBsY20gPSBnY2Q7CiAgICAgICAgICAgICAgICBmb3IgKGludCBrIDogbCkgewogICAgICAgICAgICAgICAgICAgIGludCBkID0gZGFtYWdlW2tdIC8gZ2NkKGRhbWFnZVtrXSwgbGNtKTsKICAgICAgICAgICAgICAgICAgICBpZihJbnRlZ2VyLk1BWF9WQUxVRSAvIGQgPCBsY20pCiAgICAgICAgICAgICAgICAgICAgICAgIGNvbnRpbnVlIGxvb3A7CiAgICAgICAgICAgICAgICAgICAgbGNtICo9IGQ7CiAgICAgICAgICAgICAgICB9CgogICAgICAgICAgICAgICAgcyArPSAoeHNbaSArIDFdIC8gbGNtIC0geHNbaV0gLyBsY20pCiAgICAgICAgICAgICAgICAgICAgICAgICogKEludGVnZXIuYml0Q291bnQoaikgJSAyID09IDAgPyAtMSA6IDEpOwogICAgICAgICAgICB9CiAgICAgICAgICAgIHJldCArPSBzOwogICAgICAgIH0KICAgICAgICByZXR1cm4gcmV0OwogICAgfQoKICAgIGNsYXNzIEwgZXh0ZW5kcyBBcnJheUxpc3Q8SW50ZWdlcj4ge307CgogICAgaW50IGdjZChpbnQgeCwgaW50IHkpIHsKICAgICAgICByZXR1cm4geSA9PSAwID8geCA6IGdjZCh5LCB4ICUgeSk7CiAgICB9Cn0=