結果

問題 No.578 3 x N グリッド上のサイクルのサイズ(easy)
ユーザー pekempeypekempey
提出日時 2017-10-14 00:32:46
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 10 ms / 2,000 ms
コード長 4,347 bytes
コンパイル時間 1,051 ms
コンパイル使用メモリ 87,280 KB
実行使用メモリ 5,828 KB
最終ジャッジ日時 2023-08-11 00:40:19
合計ジャッジ時間 3,237 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 9 ms
5,592 KB
testcase_01 AC 9 ms
5,512 KB
testcase_02 AC 9 ms
5,700 KB
testcase_03 AC 8 ms
5,608 KB
testcase_04 AC 9 ms
5,488 KB
testcase_05 AC 9 ms
5,552 KB
testcase_06 AC 9 ms
5,540 KB
testcase_07 AC 9 ms
5,508 KB
testcase_08 AC 9 ms
5,496 KB
testcase_09 AC 9 ms
5,536 KB
testcase_10 AC 9 ms
5,792 KB
testcase_11 AC 9 ms
5,576 KB
testcase_12 AC 9 ms
5,536 KB
testcase_13 AC 9 ms
5,536 KB
testcase_14 AC 9 ms
5,504 KB
testcase_15 AC 9 ms
5,492 KB
testcase_16 AC 9 ms
5,828 KB
testcase_17 AC 9 ms
5,552 KB
testcase_18 AC 9 ms
5,544 KB
testcase_19 AC 9 ms
5,824 KB
testcase_20 AC 9 ms
5,508 KB
testcase_21 AC 9 ms
5,612 KB
testcase_22 AC 9 ms
5,568 KB
testcase_23 AC 9 ms
5,488 KB
testcase_24 AC 9 ms
5,592 KB
testcase_25 AC 9 ms
5,496 KB
testcase_26 AC 9 ms
5,548 KB
testcase_27 AC 9 ms
5,484 KB
testcase_28 AC 9 ms
5,540 KB
testcase_29 AC 9 ms
5,592 KB
testcase_30 AC 10 ms
5,596 KB
testcase_31 AC 9 ms
5,664 KB
testcase_32 AC 9 ms
5,592 KB
testcase_33 AC 9 ms
5,504 KB
testcase_34 AC 9 ms
5,536 KB
testcase_35 AC 9 ms
5,696 KB
testcase_36 AC 9 ms
5,680 KB
testcase_37 AC 9 ms
5,600 KB
testcase_38 AC 9 ms
5,696 KB
testcase_39 AC 9 ms
5,596 KB
testcase_40 AC 9 ms
5,648 KB
testcase_41 AC 9 ms
5,592 KB
testcase_42 AC 9 ms
5,596 KB
testcase_43 AC 9 ms
5,652 KB
testcase_44 AC 9 ms
5,580 KB
testcase_45 AC 9 ms
5,700 KB
testcase_46 AC 9 ms
5,668 KB
testcase_47 AC 9 ms
5,536 KB
testcase_48 AC 9 ms
5,592 KB
testcase_49 AC 9 ms
5,488 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

constexpr int mod = 1e9 + 7;

struct Modint {
  int n;
  Modint(int n = 0) : n(n) {}
};

Modint operator+(Modint a, Modint b) { return Modint((a.n += b.n) >= mod ? a.n - mod : a.n); }
Modint operator-(Modint a, Modint b) { return Modint((a.n -= b.n) < 0 ? a.n + mod : a.n); }
Modint operator*(Modint a, Modint b) { return Modint(1LL * a.n * b.n % mod); }
Modint &operator+=(Modint &a, Modint b) { return a = a + b; }
Modint &operator-=(Modint &a, Modint b) { return a = a - b; }
Modint &operator*=(Modint &a, Modint b) { return a = a * b; }

Modint modinv(Modint n) {
  if (n.n == 1) return 1;
  return modinv(mod % n.n) * (mod - mod / n.n);
}
Modint operator/(Modint a, Modint b) { return a * modinv(b); }

// ref: https://en.wikipedia.org/wiki/Berlekamp%E2%80%93Massey_algorithm 
std::vector<Modint> berlekamp_massey(std::vector<Modint> s) {
  using K = Modint;
  const int N = s.size();
  std::vector<K> C(N);
  std::vector<K> B(N);
  C[0] = 1;
  B[0] = 1;
  int L = 0;
  int m = 1;
  K b = 1;
  for (int n = 0; n < N; n++) {
    K d = s[n];
    for (int i = 1; i <= L; i++) d += C[i] * s[n - i];
    if (d.n == 0) {
      m++;
    } else if (2 * L <= n) {
      auto T = C;
      for (int i = 0; i + m < N; i++) C[i + m] -= B[i] * (d / b);
      L = n + 1 - L;
      B = T;
      b = d;
      m = 1;
    } else {
      for (int i = 0; i + m < N; i++) C[i + m] -= B[i] * (d / b);
      m++;
    }
  }
  C.resize(L + 1);
  reverse(C.begin(), C.end());
  return C;
}

vector<Modint> poly_mod(vector<Modint> a, const vector<Modint> &m) {
  const int n = m.size();
  for (int i = a.size() - 1; i >= m.size(); i--) {
    for (int j = 0; j < m.size(); j++) {
      a[i - n + j] += a[i] * m[j];
    }
  }
  a.resize(m.size());
  return a;
}

// a*b mod m(x)
vector<Modint> poly_mul(const vector<Modint> &a, const vector<Modint> &b, const vector<Modint> &m) {
  vector<Modint> ret(a.size() + b.size() - 1);
  for (int i = 0; i < a.size(); i++) {
    for (int j = 0; j < b.size(); j++) {
      ret[i + j] += a[i] * b[j];
    }
  }
  return poly_mod(ret, m);
}

// x^n mod m(x)
vector<Modint> nth_power(long long n, const vector<Modint> &m) {
  vector<Modint> ret(1);
  vector<Modint> x(2);
  ret[0] = x[1] = 1;
  while (n > 0) {
    if (n & 1) ret = poly_mul(ret, x, m);
    x = poly_mul(x, x, m);
    n /= 2;
  }
  return poly_mod(ret, m);
}

int main() {
  int c[10] = {0, 4, 4, 4, 6, 8, 8, 6, 8, 0};

  vector<vector<int>> g = {
    /* 0 */ {0, 1, 2, 3, 4, 5, 7, 8},
    /* 0 */ {0, 0, 0, 0, 0, 0, 0, 0},

    /* 1 */ {1, 4, 5, 8, 9},
    /* 1 */ {1, 1, 1, 1, 0},

    /* 2 */ {2, 4, 7, 8, 9},
    /* 2 */ {1, 1, 1, 1, 0},

    /* 3 */ {3, 5, 7, 8, 9},
    /* 3 */ {1, 1, 1, 1, 0},

    /* 4 */ {1, 2, 4, 7, 8, 9},
    /* 4 */ {1, 1, 2, 1, 2, 0},

    /* 5 */ {5, 8},
    /* 5 */ {2, 2},

    /* 6 */ {1, 3, 6, 9},
    /* 6 */ {1, 1, 2, 0},

    /* 7 */ {2, 3, 4, 7, 8, 9},
    /* 7 */ {1, 1, 1, 2, 2, 0},

    /* 8 */ {1, 2, 3, 4, 6, 7, 8, 9},
    /* 8 */ {1, 1, 1, 2, 2, 2, 3, 0},

    /* 9 */ {9},
    /* 9 */ {0},
  };

  // std::vector<std::string> s = {"   ", "o  ", " o ", "  o", "oo ", "o o", "o o", " oo", "ooo", "   "};

  // for (int i = 0; i < 10; i++) {
  //   for (int j = 0; j < g[i * 2].size(); j++) {
  //     int ii = g[i * 2][j];
  //     std::cout << s[i] << std::endl;
  //     std::cout << s[ii] << std::endl;
  //     std::cout << g[i * 2 + 1][j] << std::endl;
  //     std::cout << std::endl;
  //   }
  // }

  static Modint dp[55][10][1000];
  dp[0][0][0] = 1;

  for (int i = 0; i < 50; i++) {
    for (int j = 0; j < 10; j++) {
      for (int k = 0; k < 950; k++) {
        for (int l = 0; l < g[j * 2].size(); l++) {
          int jj = g[j * 2][l];
          dp[i + 1][jj][k + c[jj] - 2 * g[j * 2 + 1][l]] += dp[i][j][k];
        }
      }
    }
  }

  std::vector<Modint> a;
  for (int i = 1; i <= 50; i++) {
    Modint ret;
    for (int j = 0; j < 1000; j++) {
      ret += dp[i][9][j] * j;
    }
    a.push_back(ret);
    cerr << ret.n << endl;
  }

  vector<Modint> m = berlekamp_massey(a);
  m.pop_back();
  for (int i = 0; i < m.size(); i++) {
    m[i] *= mod - 1;
  }

  long long n;
  cin >> n;
  auto x = nth_power(n, m);

  Modint ans;
  for (int i = 0; i < x.size(); i++) {
    ans += x[i] * a[i];
  }
  cout << ans.n << endl;
}
0