結果

問題 No.681 Fractal Gravity Glue
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-05-20 22:42:12
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 3,622 bytes
コンパイル時間 1,785 ms
コンパイル使用メモリ 157,904 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-06-22 01:15:06
合計ジャッジ時間 2,320 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 1 ms
6,940 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 1 ms
6,940 KB
testcase_11 AC 1 ms
6,940 KB
testcase_12 AC 1 ms
6,940 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 1 ms
6,944 KB
testcase_15 AC 2 ms
6,940 KB
testcase_16 AC 1 ms
6,940 KB
testcase_17 AC 1 ms
6,940 KB
testcase_18 AC 1 ms
6,940 KB
testcase_19 AC 1 ms
6,944 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)); }


/*
  a[0] = 0
  a[i] = (D + 1) a[i - 1] + D i
  
  a[i] - 2 a[i - 1] + a[i - 2] = (D + 1) a[i - 1] - 2 (D + 1) a[i - 2] + (D + 1) a[i - 3]
  T^3 - (D + 3) T^2 + (2 D + 3) T - (D + 1)
*/

enum MO = 10L^^9 + 7;
enum INF = 3 * 10L^^18;
enum LIM = 100;

long[] mul(long[] a, long[] b, long D) {
  auto c = new long[5];
  foreach (i; 0 .. 3) foreach (j; 0 .. 3) {
    c[i + j] += a[i] * b[j];
  }
  c[] %= MO;
  foreach_reverse (i; 3 .. 5) {
    c[i - 1] = (c[i - 1] + (    D + 3) * c[i]) % MO;
    c[i - 2] = (c[i - 2] - (2 * D + 3) * c[i]) % MO;
    c[i - 3] = (c[i - 3] + (    D + 1) * c[i]) % MO;
  }
  return c[0 .. 3];
}

long getFull(long k, long D) {
  debug {
    enum lim = 10;
    auto a = new long[lim];
    a[0] = 0;
    foreach (i; 1 .. lim) {
      a[i] = ((D + 1) * a[i - 1] + D * i) % MO;
    }
    writeln(a);
    foreach (i; 3 .. lim) {
      assert((a[i] - (D + 3) * a[i - 1] + (2 * D + 3) * a[i - 2] - (D + 1) * a[i - 3]) % MO == 0);
    }
  }
  long[] x = [0, 1, 0], y = [1, 0, 0];
  for (long e = k; e; e >>= 1) {
    if (e & 1) {
      y = mul(y, x, D);
    }
    x = mul(x, x, D);
  }
  const a2 = ((D + 1) * D + D * 2) % MO;
  const ret = (y[1] * D + y[2] * a2) % MO;
  debug {
    writeln("getFull ", k, " ", D, " = ", ret);
  }
  return ret;
}

long[] len;

long calc(int k, long D, long a, long b) {
  chmax(a, 0);
  chmin(b, len[k]);
  if (a >= b) {
    return 0;
  }
  long ret;
  if (a == 0 && b == len[k]) {
    ret = getFull(k, D);
  } else {
    const m = len[k - 1] + 1;
    if (a / m == b / m) {
      ret = calc(k - 1, D, a % m, b % m);
    } else {
      ret += calc(k - 1, D, a % m, m - 1);
      ret += (b / m - a / m - 1) * getFull(k - 1, D);
      ret += (b / m - a / m) * k;
      ret += calc(k - 1, D, 0, b % m);
      ret %= MO;
    }
  }
  debug {
    writeln("calc ", k, " ", D, " ", a, " ", b, " = ", ret);
  }
  return ret;
}

void main() {
  try {
    for (; ; ) {
      const N = readLong();
      const B = readLong();
      const D = readLong();
      
      len = new long[LIM];
      len[0] = 0;
      foreach (i; 1 .. LIM) {
        len[i] = cast(long)(min((D + 1) * BigInt(len[i - 1]) + D, BigInt(INF)));
      }
      debug {
        writeln("len = ", len);
      }
      
      long ans = getFull(B, D);
      foreach (i; 0 .. LIM) {
        if (N <= len[i]) {
          ans -= calc(i, D, 0, N);
          break;
        }
      }
      ans = (ans % MO + MO) % MO;
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}
0