結果
問題 | No.665 Bernoulli Bernoulli |
ユーザー |
![]() |
提出日時 | 2021-05-03 18:21:21 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 52 ms / 2,000 ms |
コード長 | 11,276 bytes |
コンパイル時間 | 2,354 ms |
コンパイル使用メモリ | 207,032 KB |
最終ジャッジ日時 | 2025-01-21 06:20:34 |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 15 |
ソースコード
#include <bits/stdc++.h>using namespace std;template <int mod> struct ModInt {#if __cplusplus >= 201402L#define MDCONST constexpr#else#define MDCONST#endifusing lint = long long;MDCONST static int get_mod() { return mod; }static int get_primitive_root() {static int primitive_root = 0;if (!primitive_root) {primitive_root = [&]() {std::set<int> fac;int v = mod - 1;for (lint i = 2; i * i <= v; i++)while (v % i == 0) fac.insert(i), v /= i;if (v > 1) fac.insert(v);for (int g = 1; g < mod; g++) {bool ok = true;for (auto i : fac)if (ModInt(g).pow((mod - 1) / i) == 1) {ok = false;break;}if (ok) return g;}return -1;}();}return primitive_root;}int val;MDCONST ModInt() : val(0) {}MDCONST ModInt &_setval(lint v) { return val = (v >= mod ? v - mod : v), *this; }MDCONST ModInt(lint v) { _setval(v % mod + mod); }MDCONST explicit operator bool() const { return val != 0; }MDCONST ModInt operator+(const ModInt &x) const { return ModInt()._setval((lint)val + x.val); }MDCONST ModInt operator-(const ModInt &x) const { return ModInt()._setval((lint)val - x.val + mod); }MDCONST ModInt operator*(const ModInt &x) const { return ModInt()._setval((lint)val * x.val % mod); }MDCONST ModInt operator/(const ModInt &x) const { return ModInt()._setval((lint)val * x.inv() % mod); }MDCONST ModInt operator-() const { return ModInt()._setval(mod - val); }MDCONST ModInt &operator+=(const ModInt &x) { return *this = *this + x; }MDCONST ModInt &operator-=(const ModInt &x) { return *this = *this - x; }MDCONST ModInt &operator*=(const ModInt &x) { return *this = *this * x; }MDCONST ModInt &operator/=(const ModInt &x) { return *this = *this / x; }friend MDCONST ModInt operator+(lint a, const ModInt &x) { return ModInt()._setval(a % mod + x.val); }friend MDCONST ModInt operator-(lint a, const ModInt &x) { return ModInt()._setval(a % mod - x.val + mod); }friend MDCONST ModInt operator*(lint a, const ModInt &x) { return ModInt()._setval(a % mod * x.val % mod); }friend MDCONST ModInt operator/(lint a, const ModInt &x) { return ModInt()._setval(a % mod * x.inv() % mod); }MDCONST bool operator==(const ModInt &x) const { return val == x.val; }MDCONST bool operator!=(const ModInt &x) const { return val != x.val; }MDCONST bool operator<(const ModInt &x) const { return val < x.val; } // To use std::map<ModInt, T>friend std::istream &operator>>(std::istream &is, ModInt &x) {lint t;return is >> t, x = ModInt(t), is;}MDCONST friend std::ostream &operator<<(std::ostream &os, const ModInt &x) { return os << x.val; }MDCONST ModInt pow(lint n) const {lint ans = 1, tmp = this->val;while (n) {if (n & 1) ans = ans * tmp % mod;tmp = tmp * tmp % mod, n /= 2;}return ans;}static std::vector<long long> facs, invs;MDCONST static void _precalculation(int N) {int l0 = facs.size();if (N <= l0) return;facs.resize(N), invs.resize(N);for (int i = l0; i < N; i++) facs[i] = facs[i - 1] * i % mod;long long facinv = ModInt(facs.back()).pow(mod - 2).val;for (int i = N - 1; i >= l0; i--) {invs[i] = facinv * facs[i - 1] % mod;facinv = facinv * i % mod;}}MDCONST lint inv() const {if (this->val < std::min(mod >> 1, 1 << 21)) {while (this->val >= int(facs.size())) _precalculation(facs.size() * 2);return invs[this->val];} else {return this->pow(mod - 2).val;}}MDCONST ModInt fac() const {while (this->val >= int(facs.size())) _precalculation(facs.size() * 2);return facs[this->val];}MDCONST ModInt doublefac() const {lint k = (this->val + 1) / 2;return (this->val & 1) ? ModInt(k * 2).fac() / (ModInt(2).pow(k) * ModInt(k).fac()) : ModInt(k).fac() * ModInt(2).pow(k);}MDCONST ModInt nCr(const ModInt &r) const { return (this->val < r.val) ? 0 : this->fac() / ((*this - r).fac() * r.fac()); }ModInt sqrt() const {if (val == 0) return 0;if (mod == 2) return val;if (pow((mod - 1) / 2) != 1) return 0;ModInt b = 1;while (b.pow((mod - 1) / 2) == 1) b += 1;int e = 0, m = mod - 1;while (m % 2 == 0) m >>= 1, e++;ModInt x = pow((m - 1) / 2), y = (*this) * x * x;x *= (*this);ModInt z = b.pow(m);while (y != 1) {int j = 0;ModInt t = y;while (t != 1) j++, t *= t;z = z.pow(1LL << (e - j - 1));x *= z, z *= z, y *= z;e = j;}return ModInt(std::min(x.val, mod - x.val));}};template <int mod> std::vector<long long> ModInt<mod>::facs = {1};template <int mod> std::vector<long long> ModInt<mod>::invs = {0};template <typename MODINT> MODINT interpolate_iota(const std::vector<MODINT> ys, MODINT x_eval) {const int N = ys.size();if (x_eval.val < N) return ys[x_eval.val];std::vector<MODINT> facinv(N);facinv[N - 1] = MODINT(N - 1).fac().inv();for (int i = N - 1; i > 0; i--) facinv[i - 1] = facinv[i] * i;std::vector<MODINT> numleft(N);MODINT numtmp = 1;for (int i = 0; i < N; i++) {numleft[i] = numtmp;numtmp *= x_eval - i;}numtmp = 1;MODINT ret = 0;for (int i = N - 1; i >= 0; i--) {MODINT tmp = ys[i] * numleft[i] * numtmp * facinv[i] * facinv[N - 1 - i];ret += ((N - 1 - i) & 1) ? (-tmp) : tmp;numtmp *= x_eval - i;}return ret;}template <typename MODINT> MODINT sum_of_exponential_times_polynomial_limit(MODINT r, std::vector<MODINT> init) {assert(r != 1);if (init.empty()) return 0;if (init.size() == 1) return init[0] / (1 - r);auto &bs = init;const int d = int(bs.size()) - 1;MODINT rp = 1;for (int i = 1; i <= d; i++) rp *= r, bs[i] = bs[i] * rp + bs[i - 1];MODINT ret = 0;rp = 1;for (int i = 0; i <= d; i++) {ret += bs[d - i] * MODINT(d + 1).nCr(i) * rp;rp *= -r;}return ret / MODINT(1 - r).pow(d + 1);};template <typename MODINT> MODINT sum_of_exponential_times_polynomial(MODINT r, const std::vector<MODINT> &init, long long N) {if (N <= 0) return 0;if (init.size() == 1) return init[0] * (r == 1 ? MODINT(N) : (1 - r.pow(N)) / (1 - r));std::vector<MODINT> S(init.size() + 1);MODINT rp = 1;for (int i = 0; i < int(init.size()); i++) {S[i + 1] = S[i] + init[i] * rp;rp *= r;}if (N < (long long)S.size()) return S[N];if (r == 1) return interpolate_iota<MODINT>(S, N);const MODINT Sinf = sum_of_exponential_times_polynomial_limit<MODINT>(r, init);MODINT rinv = r.inv(), rinvp = 1;for (int i = 0; i < int(S.size()); i++) {S[i] = (S[i] - Sinf) * rinvp;rinvp *= rinv;}return interpolate_iota<MODINT>(S, N) * r.pow(N) + Sinf;};// Linear sieve algorithm for fast prime factorization// Complexity: O(N) time, O(N) space:// - MAXN = 10^7: ~44 MB, 80~100 ms (Codeforces / AtCoder GCC, C++17)// - MAXN = 10^8: ~435 MB, 810~980 ms (Codeforces / AtCoder GCC, C++17)// Reference:// [1] D. Gries, J. Misra, "A Linear Sieve Algorithm for Finding Prime Numbers,"// Communications of the ACM, 21(12), 999-1003, 1978.// - https://cp-algorithms.com/algebra/prime-sieve-linear.html// - https://37zigen.com/linear-sieve/struct Sieve {std::vector<int> min_factor;std::vector<int> primes;Sieve(int MAXN) : min_factor(MAXN + 1) {for (int d = 2; d <= MAXN; d++) {if (!min_factor[d]) {min_factor[d] = d;primes.emplace_back(d);}for (const auto &p : primes) {if (p > min_factor[d] or d * p > MAXN) break;min_factor[d * p] = p;}}}// Prime factorization for 1 <= x <= MAXN^2// Complexity: O(log x) (x <= MAXN)// O(MAXN / log MAXN) (MAXN < x <= MAXN^2)template <typename T> std::map<T, int> factorize(T x) {std::map<T, int> ret;assert(x > 0 and x <= ((long long)min_factor.size() - 1) * ((long long)min_factor.size() - 1));for (const auto &p : primes) {if (x < T(min_factor.size())) break;while (!(x % p)) x /= p, ret[p]++;}if (x >= T(min_factor.size())) ret[x]++, x = 1;while (x > 1) ret[min_factor[x]]++, x /= min_factor[x];return ret;}// Enumerate divisors of 1 <= x <= MAXN^2// Be careful of highly composite numbers https://oeis.org/A002182/list https://gist.github.com/dario2994/fb4713f252ca86c1254d#file-list-txt// (n, (# of div. of n)): 45360->100, 735134400(<1e9)->1344, 963761198400(<1e12)->6720template <typename T> std::vector<T> divisors(T x) {std::vector<T> ret{1};for (const auto p : factorize(x)) {int n = ret.size();for (int i = 0; i < n; i++) {for (T a = 1, d = 1; d <= p.second; d++) {a *= p.first;ret.push_back(ret[i] * a);}}}return ret; // NOT sorted}// Moebius function Table, (-1)^{# of different prime factors} for square-free x// return: [0=>0, 1=>1, 2=>-1, 3=>-1, 4=>0, 5=>-1, 6=>1, 7=>-1, 8=>0, ...] https://oeis.org/A008683std::vector<int> GenerateMoebiusFunctionTable() {std::vector<int> ret(min_factor.size());for (unsigned i = 1; i < min_factor.size(); i++) {if (i == 1)ret[i] = 1;else if ((i / min_factor[i]) % min_factor[i] == 0)ret[i] = 0;elseret[i] = -ret[i / min_factor[i]];}return ret;}// Calculate [0^K, 1^K, ..., nmax^K] in O(nmax)// Note: **0^0 == 1**template <typename MODINT> std::vector<MODINT> enumerate_kth_pows(long long K, int nmax) {assert(nmax < int(min_factor.size()));assert(K >= 0);if (K == 0) return std::vector<MODINT>(nmax + 1, 1);std::vector<MODINT> ret(nmax + 1);ret[0] = 0, ret[1] = 1;for (int n = 2; n <= nmax; n++) {if (min_factor[n] == n) {ret[n] = MODINT(n).pow(K);} else {ret[n] = ret[n / min_factor[n]] * ret[min_factor[n]];}}return ret;}};// Sieve sieve(1 << 15); // (can factorize n <= 10^9)using mint = ModInt<1000000007>;int main() {long long N;int K;cin >> N >> K;cout << sum_of_exponential_times_polynomial<mint>(1, Sieve(K).enumerate_kth_pows<mint>(K, K), N + 1) << '\n';}