QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#682054#9248. An Easy Math ProblemOldMemory#TL 0ms3628kbC++203.7kb2024-10-27 13:39:542024-10-27 13:39:55

Judging History

This is the latest submission verdict.

  • [2024-10-31 22:36:43]
  • hack成功,自动添加数据
  • (/hack/1098)
  • [2024-10-31 22:13:58]
  • hack成功,自动添加数据
  • (/hack/1096)
  • [2024-10-31 22:00:43]
  • hack成功,自动添加数据
  • (/hack/1095)
  • [2024-10-27 13:39:55]
  • Judged
  • Verdict: TL
  • Time: 0ms
  • Memory: 3628kb
  • [2024-10-27 13:39:54]
  • Submitted

answer

#include<bits/stdc++.h>
#define int long long
using namespace std;
const double eps = 1e-8;

int mul(int a, int b, int m) {
    return static_cast<__int128_t> (a) * b % m;
}

int power(int a, int b, int m) {
    int res = 1 % m;  a %= m;
    while(b) {
        if(b & 1)  res = mul(res, a, m);
        a = mul(a, a, m);
        b >>= 1;
    }
    return res;
}

bool isprime(int n) {
    if(n < 2)  return false;
    static constexpr int A[] = {2, 3, 5, 7, 11, 13, 17, 19, 23};
    int s = __builtin_ctzll(n - 1);
    int d = (n - 1) >> s;
    for (auto  a : A) {
        if(a == n)  return true;
        int x = power(a, d, n);
        if(x == 1 || x == n - 1)  continue;
        bool ok = false;
        for (int i = 0; i < s - 1; ++i) {
            x = mul(x, x, n);
            if(x == n - 1) {
                ok = true;
                break;
            }
        }
        if(!ok)  return false;
    }
    return true;
}

vector<int> factorize(int n) {
    vector<int> p;
    function<void(int)> f = [&](int n) {
        if(n <= 10000) {
            for (int i =2; i * i <= n; ++i) {
                for ( ; n % i == 0; n /= i) {
                    p.push_back(i);
                }
            }
            if(n > 1)  p.push_back(n);
            return ;
        }
        if(isprime(n)) {
            p.push_back(n);
            return ;
        }
        auto g = [&](int x) {
            return (mul(x, x, n) + 1) % n;
        };
        int x0 = 2;
        while(true) {
            int x = x0;
            int y = x0;
            int d = 1;
            int power = 1, lam = 0;
            int v = 1;
            while(d == 1) {
                y = g(y);
                ++lam;
                v = mul(v, abs(x - y), n);
                if(lam % 127 == 0) {
                    d = __gcd(v, n);
                    v = 1;
                }
                if(power == lam) {
                    x = y;
                    power *= 2;
                    lam = 0;
                    d = __gcd(v, n);
                    v = 1;
                }
            }
            if(d != n) {
                f(d);
                f(n / d);
                return ;
            }
            ++x0;
        }
    };
    f(n);
    sort(p.begin(), p.end());
    return p;
}

void solve() {
    int n, res = 0;  cin >> n;
    vector<int> p = factorize(n), fc;
    vector<pair<int, int>> fac;
    int sz = p.size();
    for (int i = 0; i < sz; ++i) {
        if(!i || p[i] != p[i - 1])  fac.push_back({p[i], 1});
        else {
            int k = fac.size();
            fac[k - 1].second++;
        }
    }
    int k = fac.size();
    vector<int> s(k + 1);
    for (int i = k - 1; i >= 0; --i)  s[i] = s[i + 1] + fac[i].second;
    // for (auto [u, v] : fac)  cout << u << ' ' << v << '\n';

    auto dfs = [&](auto self, int dep, int cur) -> void {
        if(dep == k) {
            fc.push_back(cur);
            return;
        }
        for (int i = 0, tmp = 1; i <= fac[dep].second; ++i, tmp *= fac[dep].first)  self(self, dep + 1, cur * tmp);
    };

    dfs(dfs, 0, 1);
    sort(fc.begin(), fc.end());
    k = fc.size();
    map<pair<int, int>, bool> vis;
    for (int i = 0; i < k; ++i) {
        for (int j = i; j < k; ++j) {
            int a = fc[i], b = fc[j], d = __gcd(a, b);
            a /= d;  b /= d;
            if(vis.count({a, b}))  continue;
            vis[{a, b}] = true;
            res++;
        }
    }
    cout << res << '\n';
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);  cout.tie(0);
    int t;  cin >> t;
    while(t--) {
        solve();
    }
    return 0;
}

詳細信息

Test #1:

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

input:

10
1
2
3
4
5
6
7
8
9
10

output:

1
2
2
3
2
5
2
4
3
5

result:

ok 10 lines

Test #2:

score: -100
Time Limit Exceeded

input:

2000
6469693230
6469693230
6469693230
6469693230
6469693230
6469693230
6469693230
6469693230
6469693230
6469693230
6469693230
6469693230
6469693230
6469693230
6469693230
6469693230
6469693230
6469693230
6469693230
6469693230
6469693230
6469693230
6469693230
6469693230
6469693230
6469693230
646969323...

output:


result: