QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#469202#7230. Fraction FactorypropaneWA 0ms3864kbC++204.8kb2024-07-09 16:03:312024-07-09 16:03:31

Judging History

This is the latest submission verdict.

  • [2024-07-09 16:03:31]
  • Judged
  • Verdict: WA
  • Time: 0ms
  • Memory: 3864kb
  • [2024-07-09 16:03:31]
  • Submitted

answer

#include<bits/stdc++.h> 
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);
}

// void go_fac(LL n){ 
//     tot = 0;
//     if (n > 1) findfac(n); 
// }

//void dfs(int u, LL val, const vector<pair<LL, int> > &pri, vector<LL> &f){
//    if (u == pri.size()){
//        f.push_back(val);
//        return;
//    }
//    int c = pri[u].second;
//    for(int i = 0; i <= c; i++){
//        dfs(u + 1, val, pri, f);
//        val *= pri[u].first;
//    }
//}

auto go_fac(LL n){ 
   tot = 0;
   if (n > 1) findfac(n);     
   map<LL, int> mp;
   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;
   }
   return mp;
}

template<typename T>
T exgcd(T a, T b, T &x, T &y){
    if (!b){
        x = 1, y = 0;
        return a;
    }
    T r = exgcd(b, a % b, y, x);
    y -= a / b * x;
    return r;
}

// a * x = b (mod m) or b / a (mod m)
template<typename T>
T modequ(T a, T b, T m){
    T x, y;
    T g = exgcd(a, m, x, y);
    if (b % g) return -1;
    m /= g, a /= g, b /= g;
    x = __int128_t(x) * b % m;
    if (x < 0) x += m;
    return x;
}


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 n, m;
    cin >> n >> m;
    map<LL, int> mp1, mp2;
    for(int i = 0; i < n; i++){
        LL x;
        cin >> x;
        for(auto [x, y] : go_fac(x)){
            mp1[x] += y;
        }
    }
    for(int i = 0; i < m; i++){
        LL x;
        cin >> x;
        for(auto [x, y] : go_fac(x)){
            mp2[x] += y;
        }
    }
    vector<pair<LL, int> > p;
    for(auto [x, y] : mp1){
        if (mp2.contains(x)){
            p.push_back({x, min(y, mp2[x])});
        }
    }
    for(auto [x, y] : p){
        mp1[x] -= y;
        if (mp1[x] == 0) mp1.erase(x);
        mp2[x] -= y;
        if (mp2[x] == 0) mp2.erase(x);
    }
    int q;
    cin >> q;
    while(q--){
        LL mod;
        cin >> mod;
        auto mp = go_fac(mod);
        bool ok = true;
        for(auto [x, y] : mp){
            if (mp2.contains(x)){
                ok = false;
                break;
            }
        }
        if (!ok){
            cout << "DIVISION BY ZERO" << '\n';
            continue;
        }
        LL ans = 1;
        for(auto [x, y] : mp1){
            ans = ans * qpow(x, y, mod) % mod;
        }
        for(auto [x, y] : mp2){
            LL inv = modequ(x, 1LL, mod);
            ans = ans * qpow(inv, y, mod) % mod;
        }
        cout << ans << '\n';
    }

}

詳細信息

Test #1:

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

input:

3 2
8 11 14
9 12
4
8
9
10
11

output:

4
DIVISION BY ZERO
4
0

result:

ok 4 lines

Test #2:

score: 0
Accepted
time: 0ms
memory: 3864kb

input:

2 2
1 2
4 3
10
5
7
11
13
17
19
23
29
31
37

output:

1
6
2
11
3
16
4
5
26
31

result:

ok 10 lines

Test #3:

score: -100
Wrong Answer
time: 0ms
memory: 3564kb

input:

4 6
111111111111111111 1000000000000000000 1000000000000000 135792468013579246
10000000000000000 100000000000000 999999999999999999 123456789123456789 239239239932932932 1357924680
10
1161978962132362
539566136338457734
758923339452654441
655309120486827715
84306884606421160
911731869459297905
82243...

output:

DIVISION BY ZERO
DIVISION BY ZERO
DIVISION BY ZERO
206573139178251145
DIVISION BY ZERO
-148200486544937598
DIVISION BY ZERO
DIVISION BY ZERO
DIVISION BY ZERO
DIVISION BY ZERO

result:

wrong answer 4th lines differ - expected: '5719954405760135', found: '206573139178251145'