結果
問題 | No.890 移調の限られた旋法 |
ユーザー | kibuna |
提出日時 | 2019-09-21 19:37:31 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 45 ms / 2,000 ms |
コード長 | 5,711 bytes |
コンパイル時間 | 2,249 ms |
コンパイル使用メモリ | 187,840 KB |
実行使用メモリ | 26,880 KB |
最終ジャッジ日時 | 2024-09-19 03:03:53 |
合計ジャッジ時間 | 3,705 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,812 KB |
testcase_01 | AC | 2 ms
6,940 KB |
testcase_02 | AC | 2 ms
6,940 KB |
testcase_03 | AC | 2 ms
6,940 KB |
testcase_04 | AC | 2 ms
6,940 KB |
testcase_05 | AC | 2 ms
6,940 KB |
testcase_06 | AC | 2 ms
6,944 KB |
testcase_07 | AC | 2 ms
6,944 KB |
testcase_08 | AC | 2 ms
6,940 KB |
testcase_09 | AC | 2 ms
6,940 KB |
testcase_10 | AC | 2 ms
6,944 KB |
testcase_11 | AC | 2 ms
6,940 KB |
testcase_12 | AC | 2 ms
6,940 KB |
testcase_13 | AC | 44 ms
26,780 KB |
testcase_14 | AC | 43 ms
26,880 KB |
testcase_15 | AC | 44 ms
26,812 KB |
testcase_16 | AC | 44 ms
26,848 KB |
testcase_17 | AC | 43 ms
24,408 KB |
testcase_18 | AC | 41 ms
24,572 KB |
testcase_19 | AC | 24 ms
15,584 KB |
testcase_20 | AC | 16 ms
11,152 KB |
testcase_21 | AC | 5 ms
6,940 KB |
testcase_22 | AC | 35 ms
21,260 KB |
testcase_23 | AC | 43 ms
25,348 KB |
testcase_24 | AC | 25 ms
15,872 KB |
testcase_25 | AC | 8 ms
6,940 KB |
testcase_26 | AC | 45 ms
25,828 KB |
testcase_27 | AC | 42 ms
26,200 KB |
testcase_28 | AC | 28 ms
18,004 KB |
testcase_29 | AC | 19 ms
12,856 KB |
testcase_30 | AC | 38 ms
24,500 KB |
testcase_31 | AC | 23 ms
15,220 KB |
testcase_32 | AC | 34 ms
22,836 KB |
testcase_33 | AC | 37 ms
23,960 KB |
testcase_34 | AC | 38 ms
23,952 KB |
ソースコード
#include "bits/stdc++.h" using namespace std; using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; using vi = vector<int>; using vl = vector<ll>; using vvi = vector<vi>; using vvl = vector<vl>; const ll INF = 1LL << 60; const ll MOD = 1000000007; template <class T> bool chmax(T &a, const T &b) { return (a < b) ? (a = b, 1) : 0; } template <class T> bool chmin(T &a, const T &b) { return (b < a) ? (a = b, 1) : 0; } template <class C> void print(const C &c, std::ostream &os = std::cout) { std::copy(std::begin(c), std::end(c), std::ostream_iterator<typename C::value_type>(os, " ")); os << std::endl; } // list up all factors template <typename T> set<T> factors(T a) { set<T> facs; for (T i = 1; i * i <= a; ++i) { if (a % i == 0) { facs.insert(i); facs.insert(a / i); } } return facs; } template <typename T = int> void primeFactors(T a, map<T, int> &facs) { double sqrtA = sqrt(a); for (int i = 2; i <= sqrtA + 1e-10; ++i) { while (a % i == 0) { facs[i]++; a /= i; } } if (a > sqrtA) facs[a]++; return; } // Eratosthenes's sieve // create list of prime numbers in O(N) // check if the given number is prime in O(1) struct Sieve { vector<bool> isPrime; Sieve(size_t max) : isPrime(max + 1, true) { isPrime[0] = false; isPrime[1] = false; for (size_t i = 2; i * i <= max; ++i) // 0からsqrt(max)まで調べる if (isPrime[i]) // iが素数ならば for (size_t j = 2; i * j <= max; ++j) // (max以下の)iの倍数は isPrime[i * j] = false; // 素数ではない } bool operator()(size_t n) { return isPrime[n]; } }; struct Combination { vector<ll> fac, finv, inv; Combination(ll maxN) { maxN += 100; // for safety fac.resize(maxN + 1); finv.resize(maxN + 1); inv.resize(maxN + 1); fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (ll i = 2; i <= maxN; ++i) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } ll operator()(ll n, ll k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } }; // mod int struct // original : https://github.com/beet-aizu/library/blob/master/mod/mint.cpp struct mint { ll v; ll mod; mint() : v(0) {} mint(signed v, ll mod = MOD) : v(v), mod(mod) {} mint(ll t, ll mod = MOD) : mod(mod) { v = t % mod; if (v < 0) v += mod; } mint pow(ll k) { mint res(1), tmp(v); while (k) { if (k & 1) res *= tmp; tmp *= tmp; k >>= 1; } return res; } static mint add_identity() { return mint(0); } static mint mul_identity() { return mint(1); } mint inv() { return pow(mod - 2); } mint &operator+=(mint a) { v += a.v; if (v >= mod) v -= mod; return *this; } mint &operator-=(mint a) { v += mod - a.v; if (v >= mod) v -= mod; return *this; } mint &operator*=(mint a) { v = 1LL * v * a.v % mod; return *this; } mint &operator/=(mint a) { return (*this) *= a.inv(); } mint operator+(mint a) const { return mint(v) += a; }; mint operator-(mint a) const { return mint(v) -= a; }; mint operator*(mint a) const { return mint(v) *= a; }; mint operator/(mint a) const { return mint(v) /= a; }; mint operator-() const { return v ? mint(mod - v) : mint(v); } bool operator==(const mint a) const { return v == a.v; } bool operator!=(const mint a) const { return v != a.v; } bool operator<(const mint a) const { return v < a.v; } // find x s.t. a^x = b static ll log(ll a, ll b) { const ll sq = 40000; unordered_map<ll, ll> dp; dp.reserve(sq); mint res(1); for (int r = 0; r < sq; r++) { if (!dp.count(res.v)) dp[res.v] = r; res *= a; } mint p = mint(a).inv().pow(sq); res = b; for (int q = 0; q <= MOD / sq + 1; q++) { if (dp.count(res.v)) { ll idx = q * sq + dp[res.v]; if (idx > 0) return idx; } res *= p; } assert(0); return ll(-1); } static mint comb(long long n, int k) { mint num(1), dom(1); for (int i = 0; i < k; i++) { num *= mint(n - i); dom *= mint(i + 1); } return num / dom; } }; ostream &operator<<(ostream &os, mint m) { os << m.v; return os; } int main() { int n, k; cin >> n >> k; auto nfac = factors(n); auto kfac = factors(k); nfac.erase(1); kfac.erase(1); Sieve isprime(n); Combination nCk(n); mint ret = 0; vector<pair<int, mint>> used; for (auto it = kfac.rbegin(); it != kfac.rend(); it = next(it)) { if (nfac.count(*it) > 0) { mint temp = nCk(n / (*it), k / (*it)); for (auto &u : used) { if (u.first % (*it) == 0) { temp -= u.second; } } used.emplace_back(*it, temp); ret += temp; } } cout << ret << "\n"; return 0; }