結果
問題 | No.578 3 x N グリッド上のサイクルのサイズ(easy) |
ユーザー |
![]() |
提出日時 | 2017-10-13 23:43:34 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 2 ms / 2,000 ms |
コード長 | 3,259 bytes |
コンパイル時間 | 1,042 ms |
コンパイル使用メモリ | 102,632 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-11-17 11:50:48 |
合計ジャッジ時間 | 2,529 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 50 |
ソースコード
#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;}} // Polyvoid 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;}