結果
| 問題 |
No.359 門松行列
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-05-03 02:16:47 |
| 言語 | D (dmd 2.109.1) |
| 結果 |
AC
|
| 実行時間 | 3 ms / 2,000 ms |
| コード長 | 3,293 bytes |
| コンパイル時間 | 905 ms |
| コンパイル使用メモリ | 127,644 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-06-22 01:10:04 |
| 合計ジャッジ時間 | 1,644 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 17 |
ソースコード
import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, std.numeric, std.regex, std.typecons;
import core.bitop;
class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }
bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }
int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }
long L;
long[] A;
void main() {
try {
for (; ; ) {
const numCases = readInt();
foreach (caseId; 0 .. numCases) {
L = readLong();
A = new long[9];
foreach (i; 0 .. 9) {
A[i] = readLong();
}
auto p = new long[9];
auto q = new long[9];
int cnt;
foreach (i; 0 .. 9) {
if (A[i] == 0) {
if (cnt++ == 0) {
p[i] = 0;
q[i] = 1;
} else {
p[i] = L;
q[i] = -1;
}
} else {
p[i] = A[i];
q[i] = 0;
}
}
assert(cnt == 2);
long[] ts;
ts ~= 1;
ts ~= L;
foreach (i; 0 .. 9) foreach (j; i + 1 .. 9) {
// p[i] + q[i] t = p[j] + p[j] t
long dp = -(p[j] - p[i]);
long dq = q[j] - q[i];
if (dq != 0) {
if (dq < 0) {
dp *= -1;
dq *= -1;
}
if (dp % dq == 0) {
ts ~= dp / dq;
ts ~= dp / dq + 1;
} else {
ts ~= dp / dq + ((dp % dq > 0) ? 1 : 0);
}
}
}
ts = ts.sort.uniq.array;
bool check(long t, int i, int j, int k) {
const a = p[i] + q[i] * t;
const b = p[j] + q[j] * t;
const c = p[k] + q[k] * t;
return (a != b && a != c && b != c && ((a < b && b > c) || (a > b && b < c)));
}
long ans;
foreach (j; 1 .. ts.length) {
const t = ts[j - 1];
bool ok = true;
ok = ok && (0 < t && t < L);
ok = ok && check(t, 0, 1, 2);
ok = ok && check(t, 3, 4, 5);
ok = ok && check(t, 6, 7, 8);
ok = ok && check(t, 0, 3, 6);
ok = ok && check(t, 1, 4, 7);
ok = ok && check(t, 2, 5, 8);
ok = ok && check(t, 2, 4, 6);
ok = ok && check(t, 0, 4, 8);
if (ok) {
ans += (ts[j] - ts[j - 1]);
}
}
writeln(ans);
}
}
} catch (EOFException e) {
}
}