結果

問題 No.963 門松列列(2)
ユーザー pekempeypekempey
提出日時 2020-01-05 20:02:39
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 543 ms / 3,000 ms
コード長 5,118 bytes
コンパイル時間 2,073 ms
コンパイル使用メモリ 180,216 KB
実行使用メモリ 29,256 KB
最終ジャッジ日時 2024-11-22 23:29:29
合計ジャッジ時間 4,821 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 12 ms
11,648 KB
testcase_01 AC 13 ms
11,648 KB
testcase_02 AC 12 ms
11,648 KB
testcase_03 AC 13 ms
11,648 KB
testcase_04 AC 12 ms
11,648 KB
testcase_05 AC 265 ms
20,568 KB
testcase_06 AC 38 ms
12,520 KB
testcase_07 AC 262 ms
19,392 KB
testcase_08 AC 541 ms
26,072 KB
testcase_09 AC 543 ms
26,708 KB
testcase_10 AC 541 ms
29,256 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

constexpr int MOD = 1012924417;
constexpr int ROOT = 5;

class mint {
  int n;
public:
  mint(int n_ = 0) : n(n_) {}
  explicit operator int() { return n; }
  friend mint operator-(mint a) { return -a.n + MOD * (a.n != 0); }
  friend mint operator+(mint a, mint b) { int x = a.n + b.n; return x - (x >= MOD) * MOD; }
  friend mint operator-(mint a, mint b) { int x = a.n - b.n; return x + (x < 0) * MOD; }
  friend mint operator*(mint a, mint b) { return (long long)a.n * b.n % MOD; }
  friend mint &operator+=(mint &a, mint b) { return a = a + b; }
  friend mint &operator-=(mint &a, mint b) { return a = a - b; }
  friend mint &operator*=(mint &a, mint b) { return a = a * b; }
  friend bool operator==(mint a, mint b) { return a.n == b.n; }
  friend bool operator!=(mint a, mint b) { return a.n != b.n; }
  friend istream &operator>>(istream &i, mint &a) { return i >> a.n; }
  friend ostream &operator<<(ostream &o, mint a) { return o << a.n; }
};
mint operator "" _m(unsigned long long n) { return n; }


mint modpow(mint a, long long b) {
  mint res = 1;
  while (b > 0) {
    if (b & 1) res *= a;
    a *= a;
    b >>= 1;
  }
  return res;
}

mint modinv(mint n) {
  int a = (int)n, b = MOD;
  int s = 1, t = 0;
  while (b != 0) {
    int q = a / b;
    a -= q * b;
    s -= q * t;
    swap(a, b);
    swap(s, t);
  }
  return s >= 0 ? s : s + MOD;
}


template<int N>
struct NTT {
  mint rots[N];

  NTT() {
    mint w = modpow(ROOT, (MOD - 1) / N);
    mint ws = 1;
    for (int i = 0; i < N / 2; i++) {
      rots[i + N / 2] = ws;
      ws *= w;
    }
    for (int i = N / 2 - 1; i >= 1; i--) {
      rots[i] = rots[i * 2];
    }
  }

  void ntt(vector<mint> &a) {
    const int n = a.size();
    int i = 0;
    for (int j = 1; j < n - 1; j++) {
      for (int k = n >> 1; k > (i ^= k); k >>= 1);
      if (j < i) swap(a[i], a[j]);
    }
    for (int i = 1; i < n; i *= 2) {
      for (int j = 0; j < n; j += i * 2) {
        for (int k = 0; k < i; k++) {
          mint s = a[j + k];
          mint t = a[j + k + i] * rots[i + k];
          a[j + k    ] = s + t;
          a[j + k + i] = s - t;
        }
      }
    }
  }

  void invntt(vector<mint> &a) {
    const int n = a.size();
    ntt(a);
    reverse(a.begin() + 1, a.end());
    mint inv_n = modinv(n);
    for (int i = 0; i < n; i++) {
      a[i] *= inv_n;
    }
  }

  vector<mint> convolution(vector<mint> a, vector<mint> b) {
    const int n = a.size() + b.size() - 1;
    int t = 1;
    while (t < n) t *= 2;
    a.resize(t);
    b.resize(t);
    ntt(a);
    ntt(b);
    for (int i = 0; i < t; i++) {
      a[i] *= b[i];
    }
    invntt(a);
    a.resize(n);
    return a;
  }
};
NTT<1 << 21> fft;

typedef vector<mint> poly; 

poly operator-(poly a) {
  for (int i = 0; i < a.size(); i++) {
    a[i] = -a[i];
  }
  return a;
}

poly operator+(poly a, mint b) {
  a[0] += b;
  return a;
}

poly operator+(poly a, poly b) {
  assert(a.size() == b.size());
  for (int i = 0; i < a.size(); i++) {
    a[i] += b[i];
  }
  return a;
}

poly operator*(poly a, poly b) {
  assert(a.size() == b.size());
  const int n = a.size();
  a = fft.convolution(a, b);
  a.resize(n);
  return a;
}

poly operator*(poly a, mint b) {
  for (int i = 0; i < a.size(); i++) {
    a[i] *= b;
  }
  return a;
}

poly operator-(poly a, poly b) {
  assert(a.size() == b.size());
  for (int i = 0; i < a.size(); i++) {
    a[i] -= b[i];
  }
  return a;
}

poly &operator+=(poly &a, poly b) { return a = a + b; }
poly &operator-=(poly &a, poly b) { return a = a - b; }

poly cut(poly &a, int n) {
  assert(n <= a.size());
  vector<mint> b(n);
  for (int i = 0; i < n; i++) {
    b[i] = a[i];
  }
  return b;
}

// g = 1 / f
// 1 / g - f = 0
// g <- g - (1 / g - f) / (- 1 / g^2)
// g <- g * (2 - fg)
poly pinv(poly a) {
  const int n = a.size();
  poly x = {modinv(a[0])};
  for (int i = 1; i < n; i *= 2) {
    const int m = min(i * 2, n);
    x.resize(m);
    x = (-cut(a, m) * x + 2) * x;
  }
  return x;
}

vector<mint> F_{1, 1}, R_{1, 1}, I_{0, 1};

void check_fact(int n) {
  for (int i = I_.size(); i <= n; i++) {
    I_.push_back(I_[MOD % i] * (MOD - MOD / i));
    F_.push_back(F_[i - 1] * i);
    R_.push_back(R_[i - 1] * I_[i]);
  }
}

mint I(int n) { check_fact(abs(n)); return n >= 0 ? I_[n] : -I_[-n]; }
mint F(int n) { check_fact(n); return n < 0 ? 0 : F_[n]; }
mint R(int n) { check_fact(n); return n < 0 ? 0 : R_[n]; }
mint C(int n, int r) { return F(n) * R(n - r) * R(r); }
mint P(int n, int r) { return F(n) * R(n - r); }
mint H(int n, int r) { return n == 0 ? (r == 0) : C(n + r - 1, r); }

mint alt(int n) {
  return n % 2 == 0 ? 1 : MOD - 1;
}

vector<mint> pcos(int n) {
  vector<mint> res(n);
  for (int i = 0; i < n; i += 2) {
    res[i] = alt(i / 2) * R(i);
  }
  return res;
}

vector<mint> psin(int n) {
  vector<mint> res(n);
  for (int i = 1; i < n; i += 2) {
    res[i] = alt(i / 2) * R(i);
  }
  return res;
}

vector<mint> ptan(int n) {
  return psin(n) * pinv(pcos(n));
}

int main() {
  int N; cin >> N;
  auto ans = ptan(N + 1) + pinv(pcos(N + 1));
  cout << ans[N] * F(N) * 2 << endl;
}
0