QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#111067#6566. Power of DivisorsKhNURE_KIVI#AC ✓15ms7740kbC++177.8kb2023-06-05 18:27:552023-06-05 18:28:00

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-06-05 18:28:00]
  • 评测
  • 测评结果:AC
  • 用时:15ms
  • 内存:7740kb
  • [2023-06-05 18:27:55]
  • 提交

answer

//#pragma GCC optimize("Ofast", "unroll-loops")
//#pragma GCC target("sse", "sse2", "sse3", "ssse3", "sse4")

#ifdef __APPLE__
#include <iostream>
#include <cmath>
#include <algorithm>
#include <stdio.h>
#include <cstdint>
#include <cstring>
#include <string>
#include <cstdlib>
#include <vector>
#include <bitset>
#include <map>
#include <queue>
#include <ctime>
#include <stack>
#include <set>
#include <list>
#include <random>
#include <deque>
#include <functional>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <complex>
#include <numeric>
#include <cassert>
#include <array>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <thread>
#else
#include <bits/stdc++.h>
#endif

#define all(a) a.begin(),a.end()
#define len(a) (int)(a.size())
#define mp make_pair
#define pb push_back
#define fir first
#define sec second
#define fi first
#define se second

using namespace std;

typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;

template<typename T>
bool umin(T &a, T b) {
    if (b < a) {
        a = b;
        return true;
    }
    return false;
}
template<typename T>
bool umax(T &a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

#if __APPLE__
#define D for (bool _FLAG = true; _FLAG; _FLAG = false)
#define LOG(...) print(#__VA_ARGS__" ::", __VA_ARGS__) << endl
template <class ...Ts> auto &print(Ts ...ts) { return ((cerr << ts << " "), ...); }
#else
#define D while (false)
#define LOG(...)
#endif

const int max_n = -1, inf = 1000111222;

ll __gcd(ll a,ll b)
{
    while (a&&b){
        if (a>=b){
            a%=b;
        }
        else{
            b%=a;
        }
    }
    return a+b;
}

class Factorizer {
public:
    Factorizer(): generator(time(0)) {
        memset(d, 0, sizeof(d));
        for (int i = 2; i < max_p; ++i) {
            if (!d[i]) {
                d[i] = i;
                for (long long j = 1LL * i * i; j < max_p; j += i) {
                    if (!d[j]) {
                        d[j] = i;
                    }
                }
            }
        }
    }

    static long long mul(long long a, long long b, long long mod) {
        long long x = (long double) a * b / mod;
        long long y = (a * b - x * mod) % mod;
        return y < 0 ? y + mod : y;
    }

    static long long power(long long a, long long b, long long mod) {
        long long res = 1 % mod;
        while (b) {
            if (b % 2) {
                res = mul(res, a, mod);
            }
            b /= 2;
            a = mul(a, a, mod);
        }
        return res;
    }

    /* works for x <= 3.8 * 10^18
     * doesn't work 3825123056546413051
     */
    static bool is_prime(long long x) {
        for (int val : {2, 3, 5, 7, 11, 13, 17, 19, 23}) {
            if (x == val) {
                return true;
            }
            if (!miller_simple(x, val)) {
                return false;
            }
        }
        return true;
    }

    vector<long long> factorize(long long x) {
        vector<long long> res;
        factorize(x, res);
        sort(res.begin(), res.end());
        return res;
    }

    vector<pair<long long, int>> factorize_count(long long x) {
        vector<long long> res = factorize(x);
        vector<pair<long long, int>> v;
        for (int i = 0; i < res.size(); ) {
            int pos = i;
            while (i < res.size() && res[pos] == res[i]) {
                ++i;
            }
            v.push_back({res[pos], i - pos});
        }
        return v;
    }

    vector<long long> get_all_divisors(long long x, bool need_sort = false) {
        auto v = factorize_count(x);
        vector<long long> res;
        generate_all_divisors(0, 1, v, res);
        if (need_sort) {
            sort(res.begin(), res.end());
        }
        return res;
    }

private:
    static const int max_p = 1000000;

    int d[max_p];
    mt19937_64 generator;
    vector<long long> p;

    static bool miller_simple(long long a, long long b) {
        long long c = a - 1;
        int k = __builtin_ctzll(c);
        c >>= k;

        b = power(b, c, a);
        if (b == 1) {
            return true;
        }
        for (int i = 0; i < k; ++i) {
            if (b + 1 == a) {
                return true;
            }
            b = mul(b, b, a);
        }
        return false;
    }

    long long polard_rho(long long a) {
        const int max_v = 25;
        while (true) {
            long long p0 = generator() % a;
            long long p1 = p0;
            long long c = generator() % a;
            long long ans = 1;
            int t = 0;
            int pos = 0;
            vector<long long> arr{p1};

            while (true) {
                for (int i = 0; i < 2; ++i) {
                    p1 = mul(p1, p1, a) + c;
                    if (p1 >= a) {
                        p1 -= a;
                    }
                    arr.push_back(p1);
                }
                p0 = arr[pos++];
                if (p0 == p1) {
                    break;
                }
                ans = mul(ans, llabs(p1 - p0), a);
                if (ans == 0) {
                    return __gcd(llabs(p1 - p0), a);
                }
                if ((++t) == max_v) {
                    t = 0;
                    ans = __gcd(ans, a);
                    if (ans > 1 && ans < a) {
                        return ans;
                    }
                }
            }

            ans = __gcd(ans, a);
            if (ans > 1 && ans < a) {
                return ans;
            }
        }
    }

    void factorize(long long a, vector<long long> &res) {
        while (a > 1 && a < max_p) {
            res.push_back(d[a]);
            a /= d[a];
        }
        for (long long x : p) {
            if (a % x == 0) {
                a /= x;
                res.push_back(x);
            }
        }
        if (a < 2) {
            return;
        }
        if (is_prime(a)) {
            p.push_back(a);
            res.push_back(a);
        } else {
            long long d = polard_rho(a);
            assert(d > 1 && d < a);
            factorize(d, res);
            factorize(a / d, res);
        }
    }

    void generate_all_divisors(int pos, long long x, const vector<pair<long long, int>> &v, vector<long long> &res) {
        if (pos == v.size()) {
            res.push_back(x);
            return;
        }
        for (int i = 0; i <= v[pos].second; ++i) {
            generate_all_divisors(pos + 1, x, v, res);
            x *= v[pos].first;
        }
    }
};

Factorizer f;

ll ans=-1;
ll X;

bool is_real_power(ll a,ll b,ll want)
{
    LOG(a,b,want);
    if (a==1){
        return want==1;
    }
    else{
        ll buf=a;
        a=1;
        while (b--){
            ll prev_a=a;
            a=a*buf;
            if (a/buf!=prev_a){
                return false;
            }
        }

        LOG(a);

        return a==want;
    }
}

void rec(int pos, long long x, long long my_divs, const vector<pair<long long, int>> &v) {
    if (pos == v.size()) {
        if (is_real_power(x,my_divs,X)){
            if (ans==-1 || ans>x){
                ans=x;
            }
        }
        return;
    }
    for (int i = 0; i <= v[pos].second; ++i) {
        rec(pos + 1, x, my_divs*(i+1), v);
        x *= v[pos].first;
    }
}

int main() {
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);

    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    cin>>X;
    vector<pair<ll,int>> p=f.factorize_count(X);
    rec(0,1,1,p);
    cout<<ans<<"\n";
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 4ms
memory: 7400kb

input:

15625

output:

25

result:

ok single line: '25'

Test #2:

score: 0
Accepted
time: 4ms
memory: 7440kb

input:

64000000

output:

20

result:

ok single line: '20'

Test #3:

score: 0
Accepted
time: 6ms
memory: 7532kb

input:

65536

output:

-1

result:

ok single line: '-1'

Test #4:

score: 0
Accepted
time: 6ms
memory: 7552kb

input:

1

output:

1

result:

ok single line: '1'

Test #5:

score: 0
Accepted
time: 6ms
memory: 7476kb

input:

10

output:

-1

result:

ok single line: '-1'

Test #6:

score: 0
Accepted
time: 9ms
memory: 7532kb

input:

100

output:

-1

result:

ok single line: '-1'

Test #7:

score: 0
Accepted
time: 4ms
memory: 7480kb

input:

10000

output:

10

result:

ok single line: '10'

Test #8:

score: 0
Accepted
time: 4ms
memory: 7476kb

input:

1000000000000000000

output:

100

result:

ok single line: '100'

Test #9:

score: 0
Accepted
time: 9ms
memory: 7428kb

input:

10372926089038969

output:

218089

result:

ok single line: '218089'

Test #10:

score: 0
Accepted
time: 3ms
memory: 7520kb

input:

10642944803293201

output:

10157

result:

ok single line: '10157'

Test #11:

score: 0
Accepted
time: 3ms
memory: 7644kb

input:

10646534823110209

output:

103182047

result:

ok single line: '103182047'

Test #12:

score: 0
Accepted
time: 6ms
memory: 7480kb

input:

1073741824

output:

32

result:

ok single line: '32'

Test #13:

score: 0
Accepted
time: 10ms
memory: 7440kb

input:

121

output:

11

result:

ok single line: '11'

Test #14:

score: 0
Accepted
time: 5ms
memory: 7468kb

input:

1296

output:

6

result:

ok single line: '6'

Test #15:

score: 0
Accepted
time: 6ms
memory: 7464kb

input:

16

output:

-1

result:

ok single line: '-1'

Test #16:

score: 0
Accepted
time: 9ms
memory: 7488kb

input:

16277421889

output:

127583

result:

ok single line: '127583'

Test #17:

score: 0
Accepted
time: 5ms
memory: 7536kb

input:

169

output:

13

result:

ok single line: '13'

Test #18:

score: 0
Accepted
time: 4ms
memory: 7536kb

input:

1985984

output:

-1

result:

ok single line: '-1'

Test #19:

score: 0
Accepted
time: 15ms
memory: 7484kb

input:

2

output:

-1

result:

ok single line: '-1'

Test #20:

score: 0
Accepted
time: 10ms
memory: 7484kb

input:

205891132094649

output:

243

result:

ok single line: '243'

Test #21:

score: 0
Accepted
time: 10ms
memory: 7472kb

input:

25

output:

5

result:

ok single line: '5'

Test #22:

score: 0
Accepted
time: 5ms
memory: 7484kb

input:

2626114239841

output:

1273

result:

ok single line: '1273'

Test #23:

score: 0
Accepted
time: 4ms
memory: 7520kb

input:

26269395104446321

output:

12731

result:

ok single line: '12731'

Test #24:

score: 0
Accepted
time: 6ms
memory: 7544kb

input:

3

output:

-1

result:

ok single line: '-1'

Test #25:

score: 0
Accepted
time: 6ms
memory: 7520kb

input:

3596345248055296

output:

88

result:

ok single line: '88'

Test #26:

score: 0
Accepted
time: 4ms
memory: 7408kb

input:

36

output:

-1

result:

ok single line: '-1'

Test #27:

score: 0
Accepted
time: 4ms
memory: 7436kb

input:

4

output:

2

result:

ok single line: '2'

Test #28:

score: 0
Accepted
time: 14ms
memory: 7432kb

input:

4096

output:

8

result:

ok single line: '8'

Test #29:

score: 0
Accepted
time: 5ms
memory: 7548kb

input:

49

output:

7

result:

ok single line: '7'

Test #30:

score: 0
Accepted
time: 15ms
memory: 7484kb

input:

5

output:

-1

result:

ok single line: '-1'

Test #31:

score: 0
Accepted
time: 7ms
memory: 7484kb

input:

576460752303423488

output:

-1

result:

ok single line: '-1'

Test #32:

score: 0
Accepted
time: 7ms
memory: 7612kb

input:

581431415926321

output:

24112889

result:

ok single line: '24112889'

Test #33:

score: 0
Accepted
time: 5ms
memory: 7432kb

input:

6

output:

-1

result:

ok single line: '-1'

Test #34:

score: 0
Accepted
time: 10ms
memory: 7544kb

input:

64

output:

4

result:

ok single line: '4'

Test #35:

score: 0
Accepted
time: 4ms
memory: 7440kb

input:

656100000000

output:

30

result:

ok single line: '30'

Test #36:

score: 0
Accepted
time: 5ms
memory: 7484kb

input:

7

output:

-1

result:

ok single line: '-1'

Test #37:

score: 0
Accepted
time: 3ms
memory: 7480kb

input:

729

output:

9

result:

ok single line: '9'

Test #38:

score: 0
Accepted
time: 6ms
memory: 7480kb

input:

8

output:

-1

result:

ok single line: '-1'

Test #39:

score: 0
Accepted
time: 7ms
memory: 7484kb

input:

81

output:

-1

result:

ok single line: '-1'

Test #40:

score: 0
Accepted
time: 6ms
memory: 7440kb

input:

8527674378686464

output:

452

result:

ok single line: '452'

Test #41:

score: 0
Accepted
time: 9ms
memory: 7428kb

input:

9

output:

3

result:

ok single line: '3'

Test #42:

score: 0
Accepted
time: 6ms
memory: 7548kb

input:

982134461213542729

output:

994009

result:

ok single line: '994009'

Test #43:

score: 0
Accepted
time: 4ms
memory: 7552kb

input:

9992002399680016

output:

9998

result:

ok single line: '9998'

Test #44:

score: 0
Accepted
time: 4ms
memory: 7548kb

input:

999269311525198921

output:

-1

result:

ok single line: '-1'

Test #45:

score: 0
Accepted
time: 5ms
memory: 7576kb

input:

999949000866995087

output:

-1

result:

ok single line: '-1'

Test #46:

score: 0
Accepted
time: 4ms
memory: 7488kb

input:

999995482005103081

output:

-1

result:

ok single line: '-1'

Test #47:

score: 0
Accepted
time: 8ms
memory: 7740kb

input:

999999969999997921

output:

-1

result:

ok single line: '-1'

Test #48:

score: 0
Accepted
time: 4ms
memory: 7432kb

input:

999999999999999989

output:

-1

result:

ok single line: '-1'

Test #49:

score: 0
Accepted
time: 4ms
memory: 7492kb

input:

999999999999999999

output:

-1

result:

ok single line: '-1'