結果

問題 No.579 3 x N グリッド上のサイクルのサイズ(hard)
ユーザー Min_25Min_25
提出日時 2017-10-13 23:43:19
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 3,259 bytes
コンパイル時間 1,001 ms
コンパイル使用メモリ 93,852 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-11 00:36:25
合計ジャッジ時間 4,123 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 1 ms
4,376 KB
testcase_30 AC 1 ms
4,376 KB
testcase_31 AC 1 ms
4,376 KB
testcase_32 AC 1 ms
4,376 KB
testcase_33 AC 2 ms
4,380 KB
testcase_34 AC 2 ms
4,376 KB
testcase_35 AC 2 ms
4,376 KB
testcase_36 AC 2 ms
4,376 KB
testcase_37 AC 1 ms
4,380 KB
testcase_38 AC 1 ms
4,376 KB
testcase_39 AC 1 ms
4,376 KB
testcase_40 AC 2 ms
4,376 KB
testcase_41 AC 2 ms
4,376 KB
testcase_42 AC 2 ms
4,380 KB
testcase_43 AC 1 ms
4,376 KB
testcase_44 AC 2 ms
4,380 KB
testcase_45 AC 2 ms
4,376 KB
testcase_46 AC 2 ms
4,380 KB
testcase_47 AC 2 ms
4,380 KB
testcase_48 AC 2 ms
4,376 KB
testcase_49 AC 2 ms
4,380 KB
testcase_50 AC 1 ms
4,380 KB
testcase_51 AC 2 ms
4,380 KB
testcase_52 AC 1 ms
4,376 KB
testcase_53 AC 2 ms
4,376 KB
testcase_54 AC 2 ms
4,376 KB
testcase_55 AC 2 ms
4,380 KB
testcase_56 AC 2 ms
4,376 KB
testcase_57 AC 2 ms
4,380 KB
testcase_58 AC 2 ms
4,380 KB
testcase_59 AC 2 ms
4,376 KB
testcase_60 AC 2 ms
4,376 KB
testcase_61 AC 2 ms
4,376 KB
testcase_62 AC 2 ms
4,380 KB
testcase_63 AC 2 ms
4,380 KB
testcase_64 AC 1 ms
4,380 KB
testcase_65 AC 1 ms
4,380 KB
testcase_66 AC 2 ms
4,380 KB
testcase_67 AC 1 ms
4,380 KB
testcase_68 AC 2 ms
4,376 KB
testcase_69 AC 2 ms
4,376 KB
testcase_70 AC 2 ms
4,380 KB
testcase_71 AC 2 ms
4,380 KB
testcase_72 AC 2 ms
4,380 KB
testcase_73 AC 1 ms
4,376 KB
testcase_74 AC 1 ms
4,380 KB
testcase_75 AC 2 ms
4,380 KB
testcase_76 AC 2 ms
4,380 KB
testcase_77 AC 2 ms
4,376 KB
testcase_78 AC 2 ms
4,380 KB
testcase_79 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cassert>
#include <cmath>
#include <cstring>

#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <functional>
#include <stack>
#include <queue>

#include <tuple>

#define getchar getchar_unlocked
#define putchar putchar_unlocked

#define _rep(_1, _2, _3, _4, name, ...) name
#define rep2(i, n) rep3(i, 0, n)
#define rep3(i, a, b) rep4(i, a, b, 1)
#define rep4(i, a, b, c) for (int i = int(a); i < int(b); i += int(c))
#define rep(...) _rep(__VA_ARGS__, rep4, rep3, rep2, _)(__VA_ARGS__)

using namespace std;

using i64 = long long;
using u8 = unsigned char;
using u32 = unsigned;
using u64 = unsigned long long;
using f80 = long double;

int get_int() {
  int c, n;
  while ((c = getchar()) < '0');
  n = c - '0';
  while ((c = getchar()) >= '0') n = n * 10 + (c - '0');
  return n;
}

namespace Poly {

using poly = vector<int>;

vector<i64> tmp64;
int mod;
i64 lmod;

int mul_mod(int a, int b) { return i64(a) * b % mod; }
i64 add_mod64(i64 a, i64 b) { return (a += b - lmod) < 0 ? a + lmod : a; }
i64 sub_mod64(i64 a, i64 b) { return (a -= b) < 0 ? a + lmod : a; }
int fixed(int a) { return (a %= mod) < 0 ? a + mod : a; }

void set_mod(int m) {
  mod = m;
  lmod = i64(mod) << 32;
}

poly poly_mul(const poly& f, const poly& g) {
  int s1 = f.size(), s2 = g.size(), s = s1 + s2 - 1;
  tmp64.assign(s, 0);
  rep(i, s1) if (f[i]) rep(j, s2) {
    tmp64[i + j] = add_mod64(tmp64[i + j], i64(f[i]) * int(g[j]));
  }
  poly ret(s);
  rep(i, s) ret[i] = tmp64[i] % mod;
  return ret;
}

poly poly_rem(const poly& f, const poly& g) {
  int s1 = f.size(), s2 = g.size();
  if (s1 < s2) return f;
  assert(g[0] == 1);
  tmp64.resize(s1);
  copy(f.begin(), f.end(), tmp64.begin());
  rep(i, s1 - s2 + 1) {
    int c = tmp64[i] % mod;
    if (c) rep(j, 1, s2) tmp64[i + j] = sub_mod64(tmp64[i + j], i64(c) * int(g[j]));
    tmp64[i] = c;
  }
  poly ret(s2 - 1);
  rep(i, s2 - 1) ret[i] = tmp64[s1 - s2 + 1 + i] % mod;
  return ret;
}

poly x_pow_mod(i64 e, const poly& f) {
  if (e == 0) return poly(1, 1);
  poly ret = poly_rem(poly({1, 0}), f);
  for (i64 mask = (i64(1) << __lg(e)) >> 1; mask; mask >>= 1) {
    ret = poly_mul(ret, ret);
    if (e & mask) ret.push_back(0);
    ret = poly_rem(ret, f);
  }
  return ret;
}

int nth_term(i64 e, poly f, poly terms, const int mod) {
  if (mod == 1) return 0;
  assert(f.size() <= terms.size() + 1);
  set_mod(mod);
  rep(i, f.size()) f[i] = fixed(f[i]);
  rep(i, terms.size()) terms[i] = fixed(terms[i]);
  auto rem = x_pow_mod(e, f); reverse(rem.begin(), rem.end());
  i64 ret = 0;
  rep(i, rem.size()) ret = add_mod64(ret, i64(rem[i]) * terms[i]);
  return ret % mod;
}

} // Poly

void solve() {
  using namespace Poly;
  auto f = poly({1, -16, 102, -342, 685, -884, 753, -386, 62, 56, -39, 4, 4});
  auto g = poly({32, 316, 2292, 14422, 84744, 479004, 2638328, 14258574, 75940592, 399782668, 84795558, 786749020, 442043859, 352536615, 76576421, 744912747, 420315017, 25759333});

  i64 N;
  while (~scanf("%lld", &N)) {
    printf("%d\n", nth_term(N - 1, f, g, 1e9 + 7));
  }
}

int main() {
  clock_t beg = clock();
  solve();
  clock_t end = clock();
  fprintf(stderr, "%.3f sec\n", double(end - beg) / CLOCKS_PER_SEC);
  return 0;
}
0