結果

問題 No.359 門松行列
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-05-03 02:16:47
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 3,293 bytes
コンパイル時間 813 ms
コンパイル使用メモリ 113,536 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-04 01:02:04
合計ジャッジ時間 1,838 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 3 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 3 ms
4,380 KB
testcase_12 AC 3 ms
4,380 KB
testcase_13 AC 3 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 3 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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) {
  }
}
0