結果

問題 No.2365 Present of good number
ユーザー AnchorBluesAnchorBlues
提出日時 2023-09-23 14:46:24
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 8,445 bytes
コンパイル時間 3,149 ms
コンパイル使用メモリ 223,956 KB
実行使用メモリ 7,328 KB
最終ジャッジ日時 2023-09-23 14:46:31
合計ジャッジ時間 4,978 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
7,124 KB
testcase_01 AC 18 ms
7,108 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 17 ms
6,956 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 17 ms
7,272 KB
testcase_18 AC 17 ms
6,828 KB
testcase_19 WA -
testcase_20 AC 16 ms
7,276 KB
testcase_21 AC 17 ms
7,064 KB
testcase_22 WA -
testcase_23 AC 16 ms
7,324 KB
testcase_24 AC 17 ms
7,116 KB
testcase_25 WA -
testcase_26 AC 16 ms
6,940 KB
testcase_27 AC 16 ms
7,048 KB
testcase_28 AC 16 ms
7,124 KB
testcase_29 AC 17 ms
7,164 KB
testcase_30 AC 17 ms
6,964 KB
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 AC 17 ms
7,164 KB
testcase_37 AC 16 ms
7,092 KB
testcase_38 AC 17 ms
7,284 KB
testcase_39 AC 16 ms
7,212 KB
testcase_40 AC 17 ms
7,096 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: 関数 ‘void solve()’ 内:
main.cpp:314:60: 警告: ‘two_e’ may be used uninitialized [-Wmaybe-uninitialized]
  314 |                 ret *= modpow(3, modpow(2, L, Z) * two_e, Z);
      |                                                            ^
main.cpp:303:20: 備考: ‘two_e’ はここで定義されています
  303 |                 ll two_e, three_e;
      |                    ^~~~~
main.cpp:313:68: 警告: ‘three_e’ may be used uninitialized [-Wmaybe-uninitialized]
  313 |                 ret *= modpow(2, modpow(2, (L + 1), Z) * three_e, Z);
      |                                                                    ^
main.cpp:303:27: 備考: ‘three_e’ はここで定義されています
  303 |                 ll two_e, three_e;
      |                           ^~~~~~~

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
template <typename T>
using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using Graph = vector<vector<int>>;

const ll INF = 1LL << 60;
const ll Z = 1000000000 + 7;

template <class T>
void chmax(T& a, T b) {
    if (b > a) a = b;
}
template <class T>
void chmin(T& a, T b) {
    if (b < a) a = b;
}

template <typename T, typename S>
std::ostream& operator<<(std::ostream& os, const pair<T, S>& x) noexcept {
    return os << "(" << x.first << ", " << x.second << ")";
}

template <typename T>
void print_vector(vector<T> a) {
    cout << '[';
    for (int i = 0; i < a.size(); i++) {
        cout << a[i];
        if (i != a.size() - 1) {
            cout << ", ";
        }
    }
    cout << ']' << endl;
}

template <ll MOD>
class ModInt {
   public:
    constexpr ModInt() { val = 0; }
    constexpr ModInt(ll v) noexcept : val(v % MOD) {
        if (val < 0) val += MOD;
    }
    constexpr ll getval() const noexcept { return val; }
    constexpr ModInt operator-() const noexcept {
        if (val == 0) return 0;
        return MOD - val;
    }
    constexpr ModInt operator+(const ModInt& r) const noexcept {
        return ModInt(*this) += r;
    }
    constexpr ModInt operator-(const ModInt& r) const noexcept {
        return ModInt(*this) -= r;
    }
    constexpr ModInt operator*(const ModInt& r) const noexcept {
        return ModInt(*this) *= r;
    }
    constexpr ModInt operator/(const ModInt& r) const noexcept {
        return ModInt(*this) /= r;
    }
    constexpr ModInt& operator+=(const ModInt& r) noexcept {
        val += r.val;
        if (val >= MOD) val -= MOD;
        return *this;
    }
    constexpr ModInt& operator-=(const ModInt& r) noexcept {
        val -= r.val;
        if (val < 0) val += MOD;
        return *this;
    }
    constexpr ModInt& operator*=(const ModInt& r) noexcept {
        val = val * r.val % MOD;
        return *this;
    }
    constexpr ModInt& operator/=(const ModInt& r) noexcept {
        ll a = r.val, b = MOD, u = 1, v = 0;
        while (b) {
            ll t = a / b;
            a -= t * b;
            std::swap(a, b);
            u -= t * v;
            std::swap(u, v);
        }
        val = val * u % MOD;
        if (val < 0) val += MOD;
        return *this;
    }
    constexpr bool operator==(const ModInt& r) const noexcept {
        return this->val == r.val;
    }
    constexpr bool operator!=(const ModInt& r) const noexcept {
        return this->val != r.val;
    }
    friend constexpr std::ostream& operator<<(std::ostream& os,
                                              const ModInt<MOD>& x) noexcept {
        return os << x.val;
    }
    // n乗 を MOD で割った余り
    constexpr ModInt<MOD> pow(ll n) noexcept {
        if (n == 0) return 1;
        auto t = this->pow(n / 2);
        t = t * t;
        if (n & 1) t = t * val;
        return t;
    }
    // 逆元
    constexpr ModInt<MOD> inv() noexcept {
        ModInt<MOD> one = 1;
        return one / *this;
    }
    ModInt<MOD>& operator=(ll v) noexcept {
        val = v % MOD;
        return *this;
    }
    ModInt<MOD>& operator=(const ModInt<MOD>& v) noexcept {
        val = v.val % MOD;
        return *this;
    }

   private:
    ll val;
};

using mint = ModInt<Z>;

ll gcd(ll x, ll y) { return (x % y) ? gcd(y, x % y) : y; }
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }
ll ceilll(ll x, ll y) { return (x + y - 1) / y; }
ll mod(ll x, ll y) { return (x + 10000000) % y; }

// 約数全列挙
vector<ll> all_divisors(ll K) {
    vector<ll> ret;
    for (ll i = 1; i * i <= K; i++) {
        if (K % i != 0) continue;
        ret.push_back(i);
        if (i * i != K) ret.push_back(K / i);
    }
    return ret;
}

// 素因数分解
vector<pair<ll, ll>> prime_factorize(ll N) {
    vector<pair<ll, ll>> res;
    for (ll a = 2; a * a <= N; ++a) {
        if (N % a != 0) continue;
        ll ex = 0;
        while (N % a == 0) {
            ++ex;
            N /= a;
        }
        res.push_back({a, ex});
    }
    if (N != 1) res.push_back({N, 1});
    return res;
}

class Eratosthenes {
   public:
    // コンストラクタ
    Eratosthenes();
    Eratosthenes(int);

    // 高速素因数分解
    vector<pii> factorize(int n) const;
    // 素数判定
    bool is_prime(int n) const;
    // 約数の個数
    int n_divisors(int n) const;

   private:
    std::vector<bool> _isprime;
    std::vector<int> _minfactor;
};

// コンストラクタ
Eratosthenes::Eratosthenes() {}

Eratosthenes::Eratosthenes(int N) {
    _isprime = std::vector<bool>(N + 1, true);
    _minfactor = std::vector<int>(N + 1, -1);
    // 1 は予めふるい落としておく
    _isprime[1] = false;
    _minfactor[1] = 1;

    // 篩
    for (int p = 2; p <= N; ++p) {
        // すでに合成数であるものはスキップする
        if (!_isprime[p]) continue;

        // p についての情報更新
        _minfactor[p] = p;

        // p 以外の p の倍数から素数ラベルを剥奪
        for (int q = p * 2; q <= N; q += p) {
            // q は合成数なのでふるい落とす
            _isprime[q] = false;

            // q は p で割り切れる旨を更新
            if (_minfactor[q] == -1) _minfactor[q] = p;
        }
    }
}

vector<pii> Eratosthenes::factorize(int n) const {
    vector<pii> res;
    while (n > 1) {
        int p = _minfactor[n];
        int exp = 0;

        // n で割り切れる限り割る
        while (_minfactor[n] == p) {
            n /= p;
            ++exp;
        }
        res.emplace_back(p, exp);
    }
    return res;
}

bool Eratosthenes::is_prime(int n) const { return _isprime[n]; }

int Eratosthenes::n_divisors(int n) const {
    auto tmp = this->factorize(n);
    int ret = 1;
    for (auto& v : tmp) {
        ret *= v.second + 1;
    }
    return ret;
}

auto es = Eratosthenes(1000000);

vector<pll> f(vector<pll>& pf) {
    unordered_map<ll, ll> mp;
    for (auto& v : pf) {
        ll q = v.first + 1;
        auto x = es.factorize(q);
        for (auto& u : x) {
            mp[u.first] += v.second * u.second;
        }
    }
    vector<pll> ret;
    for (auto& v : mp) {
        ret.push_back({v.first, v.second});
    }
    return ret;
}

// 2^a * 4^b の形になっているか
bool is_2a3b(vector<pll>& pf) {
    if (pf.size() > 2) return false;
    for (auto& v : pf) {
        if (v.first != 2 && v.first != 3) return false;
    }
    return true;
}

ll modpow(ll a, ll n, ll mod) {
    ll res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}

void solve() {
    ll N, K;
    cin >> N >> K;
    auto pf = prime_factorize(N);
    bool guchoku = false;
    if (guchoku) {
        // 愚直な実装
        for (int i = 0; i < K; i++) {
            pf = f(pf);
            print_vector(pf);
            // if (i >= 100) break;
        }
    } else {
        // print_vector(pf);
        while (true) {
            if (K == 0) break;
            if (is_2a3b(pf)) break;
            pf = f(pf);
            // print_vector(pf);
            K--;
        }
        if (K > 0) {
            if (K % 2 == 0) {
                mint ret = 1;
                for (auto& v : pf) {
                    ret *= modpow(v.first, modpow(2, K / 2, Z) * v.second, Z);
                }
                std::cout << ret << "\n";
                return;
            } else {
                ll L = (K - 1) / 2;
                ll two_e, three_e;
                for (auto& v : pf) {
                    if (v.first == 2) {
                        two_e = v.second;
                    }
                    if (v.first == 3) {
                        three_e = v.second;
                    }
                }
                mint ret = 1;
                ret *= modpow(2, modpow(2, (L + 1), Z) * three_e, Z);
                ret *= modpow(3, modpow(2, L, Z) * two_e, Z);
                std::cout << ret << "\n";
                return;
            }
        }
    }
    // print_vector(pf);
    mint ret = 1;
    for (auto& v : pf) {
        ret *= mint(v.first).pow(v.second);
    }
    std::cout << ret << "\n";
}

int main() {
    ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int T = 1;
    while (T--) {
        solve();
    }
    return 0;
}
0