結果

問題 No.890 移調の限られた旋法
ユーザー kimiyukikimiyuki
提出日時 2019-09-21 01:32:52
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 118 ms / 2,000 ms
コード長 4,915 bytes
コンパイル時間 2,219 ms
コンパイル使用メモリ 205,928 KB
実行使用メモリ 14,656 KB
最終ジャッジ日時 2023-10-13 09:55:35
合計ジャッジ時間 5,993 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,352 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,352 KB
testcase_08 AC 2 ms
4,352 KB
testcase_09 AC 2 ms
4,356 KB
testcase_10 AC 2 ms
4,356 KB
testcase_11 AC 1 ms
4,348 KB
testcase_12 AC 2 ms
4,352 KB
testcase_13 AC 117 ms
14,388 KB
testcase_14 AC 100 ms
6,980 KB
testcase_15 AC 115 ms
13,744 KB
testcase_16 AC 111 ms
10,628 KB
testcase_17 AC 98 ms
10,216 KB
testcase_18 AC 100 ms
9,836 KB
testcase_19 AC 32 ms
8,008 KB
testcase_20 AC 34 ms
4,408 KB
testcase_21 AC 7 ms
4,352 KB
testcase_22 AC 90 ms
9,500 KB
testcase_23 AC 105 ms
6,792 KB
testcase_24 AC 61 ms
8,332 KB
testcase_25 AC 12 ms
4,352 KB
testcase_26 AC 118 ms
14,656 KB
testcase_27 AC 112 ms
8,756 KB
testcase_28 AC 67 ms
5,468 KB
testcase_29 AC 45 ms
6,300 KB
testcase_30 AC 99 ms
10,160 KB
testcase_31 AC 53 ms
6,720 KB
testcase_32 AC 91 ms
9,860 KB
testcase_33 AC 107 ms
12,984 KB
testcase_34 AC 98 ms
9,612 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; (i) < (int)(n); ++ (i))
#define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++ (i))
#define REP_R(i, n) for (int i = (int)(n) - 1; (i) >= 0; -- (i))
#define REP3R(i, m, n) for (int i = (int)(n) - 1; (i) >= (int)(m); -- (i))
#define ALL(x) std::begin(x), std::end(x)
#define dump(x) cerr << #x " = " << x << endl
using namespace std;
template <class T> using reversed_priority_queue = priority_queue<T, vector<T>, greater<T> >;
template <class T, class U> inline void chmax(T & a, U const & b) { a = max<T>(a, b); }
template <class T, class U> inline void chmin(T & a, U const & b) { a = min<T>(a, b); }
template <typename X, typename T> auto make_table(X x, T a) { return vector<T>(x, a); }
template <typename X, typename Y, typename Z, typename... Zs> auto make_table(X x, Y y, Z z, Zs... zs) { auto cont = make_table(y, z, zs...); return vector<decltype(cont)>(x, cont); }
template <typename T> ostream & operator << (ostream & out, vector<T> const & xs) { REP (i, (int)xs.size() - 1) out << xs[i] << ' '; if (not xs.empty()) out << xs.back(); return out; }

template <int32_t MOD>
struct mint {
    int32_t value;
    mint() : value() {}
    mint(int64_t value_) : value(value_ < 0 ? value_ % MOD + MOD : value_ >= MOD ? value_ % MOD : value_) {}
    inline mint<MOD> operator + (mint<MOD> other) const { int32_t c = this->value + other.value; return mint<MOD>(c >= MOD ? c - MOD : c); }
    inline mint<MOD> operator - (mint<MOD> other) const { int32_t c = this->value - other.value; return mint<MOD>(c <    0 ? c + MOD : c); }
    inline mint<MOD> operator * (mint<MOD> other) const { int32_t c = (int64_t)this->value * other.value % MOD; return mint<MOD>(c < 0 ? c + MOD : c); }
    inline mint<MOD> & operator += (mint<MOD> other) { this->value += other.value; if (this->value >= MOD) this->value -= MOD; return *this; }
    inline mint<MOD> & operator -= (mint<MOD> other) { this->value -= other.value; if (this->value <    0) this->value += MOD; return *this; }
    inline mint<MOD> & operator *= (mint<MOD> other) { this->value = (int64_t)this->value * other.value % MOD; if (this->value < 0) this->value += MOD; return *this; }
    inline mint<MOD> operator - () const { return mint<MOD>(this->value ? MOD - this->value : 0); }
    mint<MOD> pow(uint64_t k) const {
        mint<MOD> x = *this, y = 1;
        for (; k; k >>= 1) {
            if (k & 1) y *= x;
            x *= x;
        }
        return y;
    }
    mint<MOD> inv() const {
        assert (value != 0);
        int64_t a = value, b = MOD;
        int64_t x = 0, y = 1;
        for (int64_t u = 1, v = 0; a; ) {
            int64_t q = b / a;
            x -= q * u; std::swap(x, u);
            y -= q * v; std::swap(y, v);
            b -= q * a; std::swap(b, a);
        }
        assert (value * x + MOD * y == b);
        assert (b == 1);
        return x;
    }
    inline mint<MOD> operator /  (mint<MOD> other) const { return *this *  other.inv(); }
    inline mint<MOD> operator /= (mint<MOD> other)       { return *this *= other.inv(); }
    inline bool operator == (mint<MOD> other) const { return value == other.value; }
    inline bool operator != (mint<MOD> other) const { return value != other.value; }
};
template <int32_t MOD> mint<MOD> operator * (int64_t value, mint<MOD> n) { return mint<MOD>(value) * n; }
template <int32_t MOD> std::ostream & operator << (std::ostream & out, mint<MOD> n) { return out << n.value; }

template <int32_t MOD>
mint<MOD> fact(int n) {
    static std::vector<mint<MOD> > memo(1, 1);
    while (n >= memo.size()) {
        memo.push_back(memo.back() * mint<MOD>(memo.size()));
    }
    return memo[n];
}
template <int32_t PRIME>
mint<PRIME> inv_fact(int n) {
    static std::vector<mint<PRIME> > memo;
    if (memo.size() <= n) {
        int l = memo.size();
        int r = n * 1.3 + 100;
        memo.resize(r);
        memo[r - 1] = fact<PRIME>(r - 1).inv();
        for (int i = r - 2; i >= l; -- i) {
            memo[i] = memo[i + 1] * (i + 1);
        }
    }
    return memo[n];
}

/**
 * @tparam MOD must be a prime
 * @note O(n log n) at first time, otherwise O(1)
 */
template <int32_t MOD>
mint<MOD> choose(int n, int r) {
    assert (0 <= r and r <= n);
    return fact<MOD>(n) * inv_fact<MOD>(n - r) * inv_fact<MOD>(r);
}


constexpr int MOD = 1e9 + 7;
mint<MOD> solve(int n, int k) {
    dump(n);
    dump(k);
    vector<mint<MOD> > f(n);
    REP3 (i, 1, n) {
        if (i != gcd(n, i)) continue;
        int j = n / gcd(n, i);
        if (k % j == 0) {
            f[i] = choose<MOD>(i, k / j);
        }
    }
    REP3 (i, 1, n) {
        for (int j = 2 * i; j < n; j += i) {
            if (f[j] != 0) {
                f[j] -= f[i];
            }
        }
    }
    return accumulate(ALL(f), mint<MOD>(0));
}

int main() {
    int n, k; cin >> n >> k;
    cout << solve(n, k) << endl;
    return 0;
}
0