結果

問題 No.2365 Present of good number
ユーザー k1suxuk1suxu
提出日時 2023-06-30 23:31:29
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 10,917 bytes
コンパイル時間 2,695 ms
コンパイル使用メモリ 268,400 KB
実行使用メモリ 13,760 KB
最終ジャッジ日時 2024-07-07 11:23:36
合計ジャッジ時間 6,083 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
13,760 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 TLE -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

// #pragma GCC target("avx")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
using namespace std;

#define rep(i,n) for(int i = 0; i < (int)n; i++)
#define FOR(n) for(int i = 0; i < (int)n; i++)
#define repi(i,a,b) for(int i = (int)a; i < (int)b; i++)
#define all(x) x.begin(),x.end()
//#define mp make_pair
#define vi vector<int>
#define vvi vector<vi>
#define vvvi vector<vvi>
#define vvvvi vector<vvvi>
#define pii pair<int,int>
#define vpii vector<pair<int,int>>

template<typename T>
void chmax(T &a, const T &b) {a = (a > b? a : b);}
template<typename T>
void chmin(T &a, const T &b) {a = (a < b? a : b);}

using ll = long long;
using ld = long double;
using ull = unsigned long long;

const ll INF = numeric_limits<long long>::max() / 2;
const ld pi = 3.1415926535897932384626433832795028;
const ll mod = 998244353;
int dx[] = {1, 0, -1, 0, -1, -1, 1, 1};
int dy[] = {0, 1, 0, -1, -1, 1, -1, 1};

#define int long long

struct fast_prime_factorizer {
    fast_prime_factorizer() = default;
    // Miller-Rabin 素数判定法
    template<class T> T pow_mod(T A, T N, T M) {
        T res = 1 % M;
        A %= M;
        while (N) {
            if (N & 1) res = (res * A) % M;
            A = (A * A) % M;
            N >>= 1;
        }
        return res;
    }

    // Pollard のロー法
    long long gcd(long long A, long long B) {
        A = abs(A), B = abs(B);
        if (B == 0) return A;
        else return gcd(B, A % B);
    }
        
    long long pollard(long long N) {
        if (N % 2 == 0) return 2;
        if (is_prime(N)) return N;

        auto f = [&](long long x) -> long long {
            return (__int128_t(x) * x + 1) % N;
        };
        long long step = 0;
        while (true) {
            ++step;
            long long x = step, y = f(x);
            while (true) {
                long long p = gcd(y - x + N, N);
                if (p == 0 || p == N) break;
                if (p != 1) return p;
                x = f(x);
                y = f(f(y));
            }
        }
    }

    bool is_prime(long long N) {
        if (N <= 1) return false;
        if (N == 2 || N == 3) return true;
        if (N % 2 == 0) return false;
        vector<long long> A = {2, 325, 9375, 28178, 450775,
                            9780504, 1795265022};
        long long s = 0, d = N - 1;
        while (d % 2 == 0) {
            ++s;
            d >>= 1;
        }
        for (auto a : A) {
            if (a % N == 0) return true;
            long long t, x = pow_mod<__int128_t>(a, d, N);
            if (x != 1) {
                for (t = 0; t < s; ++t) {
                    if (x == N - 1) break;
                    x = __int128_t(x) * x % N;
                }
                if (t == s) return false;
            }
        }
        return true;
    }

    vector<long long> factorize(long long N) {
        if (N == 1) return {};
        long long p = pollard(N);
        if (p == N) return {p};
        vector<long long> left = factorize(p);
        vector<long long> right = factorize(N / p);
        left.insert(left.end(), right.begin(), right.end());
        sort(left.begin(), left.end());
        return left;
    }
}(factorizer);

template<typename T>
vector<pair<T, int>> run_length_encoding(vector<T> s) {
    T need = s[0];
    int cnt = 0;
    vector<pair<T, int>> ret;
    for(T c : s) {
        if(c == need) {
            cnt++;
        }else {
            ret.emplace_back(need, cnt);
            need = c;
            cnt = 1;
        }
    }
    if(cnt != 0) ret.emplace_back(need, cnt);
    return ret;
}

template<int MOD>
struct Modular_Int {
    int x;

    Modular_Int() = default;
    Modular_Int(int x_) : x(x_ >= 0? x_%MOD : (MOD-(-x_)%MOD)%MOD) {}

    int val() const {
        return (x%MOD+MOD)%MOD;
    }
    int get_mod() const {
        return MOD;
    }

    Modular_Int<MOD>& operator^=(int d)  {
        Modular_Int<MOD> ret(1);
        int nx = x;
        while(d) {
            if(d&1) ret *= nx;
            (nx *= nx) %= MOD;
            d >>= 1;
        }
        *this = ret;
        return *this;
    }
    Modular_Int<MOD> operator^(int d) const {return Modular_Int<MOD>(*this) ^= d;}
    Modular_Int<MOD> pow(int d) const {return Modular_Int<MOD>(*this) ^= d;}
    
    //use this basically
    Modular_Int<MOD> inv() const {
        return Modular_Int<MOD>(*this) ^ (MOD-2);
    }
    //only if the module number is not prime
    //Don't use. This is broken.
    // Modular_Int<MOD> inv() const {
    //     int a = (x%MOD+MOD)%MOD, b = MOD, u = 1, v = 0;
    //     while(b) {
    //         int t = a/b;
    //         a -= t*b, swap(a, b);
    //         u -= t*v, swap(u, v);
    //     }
    //     return Modular_Int<MOD>(u);
    // }

    Modular_Int<MOD>& operator+=(const Modular_Int<MOD> other) {
        if((x += other.x) >= MOD) x -= MOD;
        return *this;
    }
    Modular_Int<MOD>& operator-=(const Modular_Int<MOD> other) {
        if((x -= other.x) < 0) x += MOD;
        return *this;
    }
    Modular_Int<MOD>& operator*=(const Modular_Int<MOD> other) {
        int z = x;
        z *= other.x;
        z %= MOD;
        x = z;
        if(x < 0) x += MOD;
        return *this;
    }
    Modular_Int<MOD>& operator/=(const Modular_Int<MOD> other) {
        return *this = *this * other.inv();
    }
    Modular_Int<MOD>& operator++() {
        x++;
        if (x == MOD) x = 0;
        return *this;
    }
    Modular_Int<MOD>& operator--() {
        if (x == 0) x = MOD;
        x--;
        return *this;
    }
    
    Modular_Int<MOD> operator+(const Modular_Int<MOD> other) const {return Modular_Int<MOD>(*this) += other;}
    Modular_Int<MOD> operator-(const Modular_Int<MOD> other) const {return Modular_Int<MOD>(*this) -= other;}
    Modular_Int<MOD> operator*(const Modular_Int<MOD> other) const {return Modular_Int<MOD>(*this) *= other;}
    Modular_Int<MOD> operator/(const Modular_Int<MOD> other) const {return Modular_Int<MOD>(*this) /= other;}
    
    Modular_Int<MOD>& operator+=(const int other) {Modular_Int<MOD> other_(other); *this += other_; return *this;}
    Modular_Int<MOD>& operator-=(const int other) {Modular_Int<MOD> other_(other); *this -= other_; return *this;}
    Modular_Int<MOD>& operator*=(const int other) {Modular_Int<MOD> other_(other); *this *= other_; return *this;}
    Modular_Int<MOD>& operator/=(const int other) {Modular_Int<MOD> other_(other); *this /= other_; return *this;}
    Modular_Int<MOD> operator+(const int other) const {return Modular_Int<MOD>(*this) += other;}
    Modular_Int<MOD> operator-(const int other) const {return Modular_Int<MOD>(*this) -= other;}
    Modular_Int<MOD> operator*(const int other) const {return Modular_Int<MOD>(*this) *= other;}
    Modular_Int<MOD> operator/(const int other) const {return Modular_Int<MOD>(*this) /= other;}

    bool operator==(const Modular_Int<MOD> other) const {return (*this).val() == other.val();}
    bool operator!=(const Modular_Int<MOD> other) const {return (*this).val() != other.val();}
    bool operator==(const int other) const {return (*this).val() == other;}
    bool operator!=(const int other) const {return (*this).val() != other;}

    Modular_Int<MOD> operator-() const {return Modular_Int<MOD>(0LL)-Modular_Int<MOD>(*this);}

    //入れ子にしたい
    // friend constexpr istream& operator>>(istream& is, mint& x) noexcept {
    //     int X;
    //     is >> X;
    //     x = X;
    //     return is;
    // }
    // friend constexpr ostream& operator<<(ostream& os, mint& x) {
    //     os << x.val();
    //     return os;
    // }
};

const int MOD_VAL = 1e9+7;
// const int MOD_VAL = 998244353;
using mint = Modular_Int<MOD_VAL>;

istream& operator>>(istream& is, mint& x) {
    int X;
    is >> X;
    x = X;
    return is;
}
ostream& operator<<(ostream& os, mint& x) {
    os << x.val();
    return os;
}

// istream& operator<<(istream& is, mint &a) {
//     int x;
//     is >> x;
//     a = mint(x);
//     return is;
// }
// ostream& operator<<(ostream& os, mint a) {
//     os << a.val();
//     return os;
// }

// vector<mint> f = {1}, rf = {1};
// void factor_init(int n) {
//     ++n;
//     f.resize(n, 0);
//     rf.resize(n, 0);
//     f[0] = 1;
//     repi(i, 1, n) f[i] = (f[i - 1] * i);
////     repi(i, 0, n) rf[n-1] *= i;
////     for(int i = n-1; i > 0; --i) rf[i-1] = rf[i] * i;
//     repi(i, 0, n) rf[i] = f[i].inv();
// }
// mint P(int n, int k) {
//     assert(n>=k);
//     while(n > f.size()-1) {
//         f.push_back(f.back() * f.size());
//         rf.push_back(f.back().inv());
//     }
//     return f[n] * f[n-k];
// }
// mint C(int n, int k) {
//     assert(n>=k);
//     while(n > f.size()-1) {
//         f.push_back(f.back() * f.size());
//         rf.push_back(f.back().inv());
//     }
//     return f[n]*rf[n-k]*rf[k];
// }
// mint H(int n, int k) {
//     assert(n>=1);
//     return C(n+k-1, k);
// }
// mint Cat(int n) {
//     return C(2*n, n)-C(2*n, n-1);
// }

void solve() {
    const int TMP_MOD = 1e9+6;
    int n, k;
    cin >> n >> k;
    vi factor = factorizer.factorize(n);
    unordered_map<int, int> now;
    for(auto e : factor) ++now[e];

    while(now.size() > 1) {
        unordered_map<int, int> nxt;
        for(auto e : now) {
            for(auto f : factorizer.factorize(e.first+1)) {
                (nxt[f] += e.second) %= TMP_MOD;
            }
        }
        now.swap(nxt);

        --k;

        if(k == 0) {
            mint ans = 1;
            for(auto e : now) {
                ans *= mint(e.first).pow(e.second);
            }
            cout << ans.val() << endl;
            return;
        }
    }

    if(now.begin()->first == 3) {
        unordered_map<int, int> nxt;
        for(auto e : now) {
            for(auto f : factorizer.factorize(e.first+1)) {
                (nxt[f] += e.second) %= TMP_MOD;
            }
        }
        now.swap(nxt);
        --k;

        if(k == 0) {
            mint ans = 1;
            for(auto e : now) {
                ans *= mint(e.first).pow(e.second);
            }
            cout << ans.val() << endl;
            return;
        }
    }

    int power = k/2;
    k %= 2;
    int mul = 2;
    while(power > 0) {
        if(power&1) (now.begin()->second *= mul) %= TMP_MOD;
        mul = (mul * mul) % TMP_MOD;
        power >>= 1;
    }

    if(k == 1) {
        unordered_map<int, int> nxt;
        for(auto e : now) {
            for(auto f : factorizer.factorize(e.first+1)) {
                (nxt[f] += e.second) %= TMP_MOD;
            }
        }
        now.swap(nxt);
        --k;
    }

    if(k == 0) {
        mint ans = 1;
        for(auto e : now) {
            ans *= mint(e.first).pow(e.second);
        }
        cout << ans.val() << endl;
        return;
    }
}

signed main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    solve();
    return 0;
}
0