結果

問題 No.2365 Present of good number
ユーザー srjywrdnprktsrjywrdnprkt
提出日時 2023-07-09 03:19:22
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 3,807 bytes
コンパイル時間 2,649 ms
コンパイル使用メモリ 218,164 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-30 05:42:22
合計ジャッジ時間 5,015 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const ll modc = 1e9+6;
class mint {
    ll x;
public:
    mint(ll x=0) : x((x%modc+modc)%modc) {}
    mint operator-() const {
      return mint(-x);
    }
    mint& operator+=(const mint& a) {
        if ((x += a.x) >= modc) x -= modc;
        return *this;
    }
    mint& operator-=(const mint& a) {
        if ((x += modc-a.x) >= modc) x -= modc;
        return *this;
    }
    mint& operator*=(const  mint& a) {
        (x *= a.x) %= modc;
        return *this;
    }
    mint operator+(const mint& a) const {
        mint res(*this);
        return res+=a;
    }
    mint operator-(const mint& a) const {
        mint res(*this);
        return res-=a;
    }
    mint operator*(const mint& a) const {
        mint res(*this);
        return res*=a;
    }
    mint pow(ll t) const {
        if (!t) return 1;
        mint a = pow(t>>1);
        a *= a;
        if (t&1) a *= *this;
        return a;
    }
    mint inv() const {
        return pow(modc-2);
    }
    mint& operator/=(const mint& a) {
        return (*this) *= a.inv();
    }
    mint operator/(const mint& a) const {
        mint res(*this);
        return res/=a;
    }
    bool operator == (const mint& a) const{
        return x == a.x;
    }
    friend ostream& operator<<(ostream& os, const mint& m){
        os << m.x;
        return os;
    }
    friend istream& operator>>(istream& ip, mint&m) {
        ip >> m.x;
        return ip;
    }
    ll val(){
        return x;
    }
};

vector<vector<mint>> dot(vector<vector<mint>> &a, vector<vector<mint>> &b){
    int M = a.size();
    vector<vector<mint>> c(M, vector<mint>(M));
    for (int i=0; i<M; i++){
        for (int k=0; k<M; k++){
            for (int j=0; j<M; j++) c[i][j] += (a[i][k] * b[k][j]);
        }
    }
    return c;
}

vector<vector<mint>> pow(vector<vector<mint>> A, ll N){
    int M = A.size();
    vector<vector<mint>> B(M, vector<mint>(M));
    for (int i=0; i<M; i++) B[i][i] = 1;
    while(N){
        if (N % 2 == 1) B = dot(B, A);
        N >>= 1;
        A = dot(A, A);
    }
    return B;
}

map<ll, ll> prime_factor(ll n){
    ll m = n;

    map<ll, ll> prime;
    if (n % 2 == 0){
        while(n % 2 == 0){
            prime[2]++;
            n /= 2;
        }
    }

    for (ll i = 3; i*i <= m; i+=2){
        if (n % i == 0){
            while(n % i == 0){
                prime[i]++;
                n /= i;
            }
        }
    }

    if (n != 1){
        prime[n]++;
    }

    return prime;
}

ll mod_exp(ll b, ll e, ll m){
    if (e > 0 && b == 0) return 0;
    ll ans = 1;
    b %= m;

    while (e > 0){
        if ((e & 1LL)) ans = (ans * b) % m;
        e = e >> 1LL;
        b = (b*b) % m;
    }

    return ans;
}

const ll modc2 = 1e9+7;

int main(){

    ll N, K;
    cin >> N >> K;
    map<ll, ll> mp = prime_factor(N), pr;
    for (int i=0; i<min(K, 60LL); i++){
        map<ll, ll> mp2;
        for (auto [p, e] : mp){
            pr = prime_factor(p+1);
            for (auto [p1, e1] : pr){
                mp2[p1] += (e1 * e) % modc;
                mp2[p1] %= modc;
            }
        }
        swap(mp, mp2);
    }

    if (K <= 60){
        ll ans=1;
        for (auto [p, e] : mp){
            ans *= mod_exp(p, e, modc2);
            ans %= modc2;
        }

        cout << ans << endl;
        return 0;
    }

    K -= 60;

    for (auto [x, y] : mp) assert(x == 2 || x == 3);

    vector<vector<mint>> m = {{0, 2}, {1, 0}};

    m = pow(m, K);

    mint two, thr;
    ll ans=1;
    two = m[0][0] * mp[2] + m[0][1] * mp[3];
    thr = m[1][0] * mp[2] + m[1][1] * mp[3];

    ans *= mod_exp(2, two.val(), modc2);
    ans %= modc2;
    ans *= mod_exp(3, thr.val(), modc2);
    ans %= modc2;

    cout << ans << endl;

    return 0;
}
0