QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#757750#9705. Multiplypropane#AC ✓100ms5176kbC++203.4kb2024-11-17 13:06:232024-11-17 13:06:24

Judging History

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

  • [2024-11-17 13:06:24]
  • 评测
  • 测评结果:AC
  • 用时:100ms
  • 内存:5176kb
  • [2024-11-17 13:06:23]
  • 提交

answer

#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
#include<random>
#include<map>
#include<climits>
using namespace std;
using LL = long long;

const int s = 9;
// n < 2^78
const LL p[9] = {2, 3, 5, 7, 11, 13, 17, 19, 23};
mt19937_64 rnd(random_device{}());

LL mul(LL a, LL b, LL p){
    // return __int128(a) * b % p;
    a %= p, b %= p;
    LL c = (long double)a * b / p;
    LL ans = a * b - c * p;
    if (ans < 0)
        ans += p;
    else if (ans >= p)
        ans -= p;
    return ans;
}

LL qpow(LL a, LL n, LL p){
    LL ans = 1;
    a %= p;
    while (n){
        if (n & 1) ans = mul(ans, a, p);
        a = mul(a, a, p);
        n >>= 1;
    }
    return ans;
}

bool check(LL a, LL n, LL x, LL t){
    LL ret = qpow(a, x, n);
    LL last = ret;
    for (int i = 1; i <= t; i++){
        ret = mul(ret, ret, n);
        if (ret == 1 && last != 1 && last != n - 1)
            return true;
        last = ret;
    }
    if (ret != 1) return true;
    else return false;
}

bool Miller_Rabin(LL n){
    if (n < 2) return false;
    for(auto x : p) if (n == x) return true;
    if ((n & 1) == 0) return false;

    LL x = n - 1;
    LL t = 0;
    while ((x & 1) == 0){
        x >>= 1;
        t++;
    }

    for (int i = 0; i < s; i++){
        // LL a = uniform_int_distribution<LL>(1, n - 1)(rnd);
        // if (check(a, n, x, t))
        if (check(p[i], n, x, t)) return false;
    }
    return true;
}


LL Pollard_rho(LL x){
    LL s = 0, t = 0, c = uniform_int_distribution<LL>(1, x - 1)(rnd);
    LL step = 0, goal = 1;
    LL val = 1;
    for (goal = 1;; goal <<= 1, s = t, val = 1){
        for (step = 1; step <= goal; ++step){
            t = (mul(t, t, x) + c) % x;
            val = mul(val, abs(t - s), x);
            if ((step % 127) == 0){
                LL d = __gcd(val, x);
                if (d > 1)
                    return d;
            }
        }
        LL d = __gcd(val, x);
        if (d > 1) return d;
    }
}
LL fac[200], tot;

void findfac(LL n){
    if (n == 1) return;
    if (Miller_Rabin(n)){
        fac[++tot] = n;
        return;
    }
    LL p = n;
    while (p >= n) p = Pollard_rho(n);
    while (n % p == 0) n /= p;
    findfac(n);
    findfac(p);
}

auto go_fac(LL n){ 
   tot = 0;
   if (n > 1) findfac(n);     
   map<LL, int> mp;
   vector<pair<LL, int> > pri;
   for(int i = 1; i <= tot; i++){
       if (mp.contains(fac[i])) continue;
       int c = 0;
       while(n % fac[i] == 0) c += 1, n /= fac[i];
       mp[fac[i]] = c;
   }
   for(auto x : mp) pri.push_back(x);
   return pri;
}

int main(){

#ifdef LOCAL
    freopen("data.in", "r", stdin);
    freopen("data.out", "w", stdout);
#endif

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

    int T;
    cin >> T;
    while(T--){
        int n; LL x, y;
        cin >> n >> x >> y;
        vector<LL> a(n);
        for(int i = 0; i < n; i++) cin >> a[i];
        auto pri = go_fac(x);
        LL ans = LLONG_MAX;

        auto get = [&](LL p, LL x){
            LL sum = 0;
            while(p){
                sum += p / x;
                p /= x;
            }
            return sum;
        };

        for(auto [p, c] : pri){
            LL tot = get(y, p);
            for(auto x : a) tot -= get(x, p);
            ans = min(ans, tot / c);
        }
        cout << ans << '\n';
    }

}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3820kb

input:

2
3 10 10
2 3 4
2 2 10
1 1

output:

2
8

result:

ok 2 number(s): "2 8"

Test #2:

score: 0
Accepted
time: 2ms
memory: 3640kb

input:

8
929 98210021061137 164832982985885580
43576998167336 157303878397705 212661169553039 169068044677212 17733912750082 101059786177542 56528418806042 170741049344189 128297164019222 208810463591190 96264177952672 70816863413347 116985928070432 56330014332760 10006522486360 110959002803542 15298525649...

output:

1059
95837140
1761303730724
3810060773695
8961243000749
8657430203778550
2603387692898890
569502267311933

result:

ok 8 numbers

Test #3:

score: 0
Accepted
time: 100ms
memory: 5116kb

input:

8
92894 80454414905270281 520643152573491735
2064229122797 4223622787947 1054260245418 4094316313084 3929142530824 6452342289094 3762455615113 3157146960681 5603173442583 1875814573143 1801348242678 2409547278342 6854531398370 1240913563145 1848446319539 1493011800303 5389461335879 7286083232997 579...

output:

6
114168802
81596535601
11028882122096
100316204821427
4718268084920428
394167331265621
539500856199383

result:

ok 8 numbers

Test #4:

score: 0
Accepted
time: 98ms
memory: 4836kb

input:

8
92894 8280090210874177 543856067505017676
7628166265475 4448095856140 3732480525951 6624251584927 2217648228673 2129611741353 2848644172912 8103647146535 1467047865398 2185292600211 1765086497170 6371594269098 8613841584311 6848101874651 718312212561 4093427071182 2289683844966 6915866934586 51966...

output:

65
1246786758
333319010645
13129729242598
84397513456572
1419008292818811
145866895461700
594315405335288

result:

ok 8 numbers

Test #5:

score: 0
Accepted
time: 96ms
memory: 5176kb

input:

8
92894 98210021061137 164832982985885580
437808801937 1580398501813 2136561393792 1698590570197 178168838012 1015326106916 567928960914 1715398889850 1288974230710 2097874172186 967145654868 711481916793 1175332657008 565935634477 100533395596 1114781424652 1537010227806 201374141170 2002549530277 ...

output:

1678
15138363549
3851961323533
9546266194484
65456023237176
50284070499384881
2136489879131768
343921703953617

result:

ok 8 numbers

Test #6:

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

input:

8
929 904648320997198759 857077283552821319
576128640757999 1022489209332927 342306048548590 328717138574533 439703699384584 1250841949052893 226446805904869 337311781446902 272450687310201 983490180331727 1329593231427121 1057041717229744 110875391163525 631842700541257 353425137200360 106750162246...

output:

0
14963454
29504132475
203878226275
8778367031870
15079682243266455
149351201237842
2430883872230178

result:

ok 8 numbers

Test #7:

score: 0
Accepted
time: 87ms
memory: 4884kb

input:

8
92894 904648320997198759 857077283552821319
5796497585331 10287383430483 3443981158080 3307261546850 4423910306892 12584867031801 2278307777449 3393733253885 2741158205233 9895009642558 13377192887408 10635020251022 1115530268804 6357043250803 3555851608183 10740258578761 8070377462103 13134968899...

output:

0
21583598
4114016689
5953125168816
9610340743247
133189637386353298
124668826875053
21617048982826

result:

ok 8 numbers