結果
問題 | No.2487 Multiple of M |
ユーザー | 259-Momone |
提出日時 | 2023-09-29 22:04:56 |
言語 | C++17(gcc12) (gcc 12.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 3 ms / 2,000 ms |
コード長 | 2,311 bytes |
コンパイル時間 | 3,868 ms |
コンパイル使用メモリ | 269,036 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-07-23 06:53:20 |
合計ジャッジ時間 | 4,580 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 53 |
ソースコード
// #include <atcoder/modint> #include <bits/extc++.h> int main() { using namespace std; // using modint = atcoder::static_modint<998244353>; unsigned long N, M, K; cin >> N >> M >> K; auto g{gcd(M, K)}; vector<unsigned long> prime_factors; for (unsigned long p{2}; p * p <= g; ++p) if (g % p == 0) { prime_factors.emplace_back(p); while (g % p == 0) g /= p; } if(g > 1)prime_factors.emplace_back(g); const auto P{size(prime_factors)}; auto M_p{M}; vector<unsigned long> counter(60, 1); for (unsigned long i{}; i < P; ++i) { const auto p{prime_factors[i]}; unsigned long x_M{1}, x_K{1}; while (M_p % p == 0) { M_p /= p; x_M *= p; } while (K % (x_K * p) == 0) x_K *= p; unsigned long x{1}; for (unsigned long j{}; j < 60; ++j) { counter[j] *= x; x = min(x_M, x * x_K); } } for (unsigned long j{60}; --j;) counter[j] -= counter[j - 1]; while (counter.back() == 0) counter.pop_back(); counter.emplace_back(M / M_p * (M_p - 1)); const auto C{size(counter)}; vector matrix(C, vector<unsigned long>(C)); for (unsigned long i{}; i < C; ++i) { for (unsigned long j{}; j < C; ++j) matrix[i][j] = counter[j]; matrix[i][i - (i && i + 1 < C)] -= 1; } vector ans(C, vector<unsigned long>(C)); for (unsigned long i{}; i < C; ++i) ans[i][i] = 1; const auto multiply{ [C](const auto &lhs, const auto &rhs) { vector ret(C, vector<unsigned long>(C)); for (unsigned long i{}; i < C; ++i) { for (unsigned long j{}; j < C; ++j) { for (unsigned long k{}; k < C; ++k) { ret[i][k] += lhs[i][j] * rhs[j][k] % 998244353; } } } for (auto &&row : ret) for (auto &&x : row) x %= 998244353; return ret; }}; while (N) { if (N & 1) ans = multiply(ans, matrix); matrix = multiply(matrix, matrix); N /= 2; } cout << ans[0][0] << endl; return 0; }