結果

問題 No.2125 Inverse Sum
ユーザー KKT89KKT89
提出日時 2022-11-19 10:35:02
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 17 ms / 2,000 ms
コード長 5,090 bytes
コンパイル時間 2,451 ms
コンパイル使用メモリ 160,716 KB
実行使用メモリ 4,668 KB
最終ジャッジ日時 2023-10-20 16:26:01
合計ジャッジ時間 4,003 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,372 KB
testcase_02 AC 2 ms
4,372 KB
testcase_03 AC 2 ms
4,372 KB
testcase_04 AC 2 ms
4,372 KB
testcase_05 AC 2 ms
4,372 KB
testcase_06 AC 2 ms
4,372 KB
testcase_07 AC 7 ms
4,372 KB
testcase_08 AC 2 ms
4,372 KB
testcase_09 AC 2 ms
4,372 KB
testcase_10 AC 2 ms
4,372 KB
testcase_11 AC 2 ms
4,372 KB
testcase_12 AC 2 ms
4,372 KB
testcase_13 AC 2 ms
4,372 KB
testcase_14 AC 2 ms
4,372 KB
testcase_15 AC 2 ms
4,372 KB
testcase_16 AC 2 ms
4,372 KB
testcase_17 AC 2 ms
4,372 KB
testcase_18 AC 2 ms
4,372 KB
testcase_19 AC 2 ms
4,372 KB
testcase_20 AC 2 ms
4,372 KB
testcase_21 AC 2 ms
4,372 KB
testcase_22 AC 2 ms
4,372 KB
testcase_23 AC 2 ms
4,372 KB
testcase_24 AC 15 ms
4,372 KB
testcase_25 AC 7 ms
4,372 KB
testcase_26 AC 2 ms
4,372 KB
testcase_27 AC 17 ms
4,668 KB
testcase_28 AC 14 ms
4,584 KB
testcase_29 AC 8 ms
4,372 KB
testcase_30 AC 2 ms
4,372 KB
testcase_31 AC 13 ms
4,584 KB
testcase_32 AC 5 ms
4,372 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("Ofast")
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <cstdio>
#include <ctime>
#include <assert.h>
#include <chrono>
#include <random>
#include <numeric>
#include <set>
#include <deque>
#include <stack>
#include <sstream>
#include <utility>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <tuple>
#include <array>
#include <bitset>
using namespace std;
typedef long long int ll;

/*
ポラードのρ法
参考資料
- https://qiita.com/Kiri8128/items/eca965fe86ea5f4cbb98
- https://manabitimes.jp/math/1192
*/

// verify:https://www.acmicpc.net/problem/4149
namespace factorize{
    using u64 = uint64_t;
    using u128 = __uint128_t;
    mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

    u64 binary_gcd(u64 a, u64 b) {
        if (a == 0) return b;
        if (b == 0) return a;
        const int n = __builtin_ctzll(a | b);
        a >>= __builtin_ctzll(a);
        while (b > 0) {
            b >>= __builtin_ctzll(b);
            if (a > b) std::swap(a, b);
            b -= a;
        }
        return a << n;
    }

    u128 pow (u128 a, u64 n, u128 mod) {
        u128 res = 1;
        if (a >= mod) a %= mod;
        while (n > 0) {
            if (n & 1) {
                res *= a;
                if (res >= mod) res %= mod;
            }
            a *= a;
            if (a >= mod) a %= mod;
            n >>= 1;
        }
        return res;
    }

    bool miller_rabin (u64 n, vector<u64> v) {
        u64 d = n-1;
        while (~d & 1) d >>= 1;
        for (u64 a:v) {
            if (n <= a) break;
            u64 t = d;
            u128 y = pow(a, t, n);
            while (t != n-1 and y != 1 and y != n-1) {
                y *= y; if(y >= n) y %= n;
                t *= 2;
            }
            if (y != n-1 and t % 2 == 0) return false;
        }
        return true;
    }
    
    bool is_prime (u64 n) {
        if (n <= 1) return false;
        if (~n & 1) return (n == 2);
        if (n < (1LL << 30)) {
            return miller_rabin(n, {2, 7, 61});
        }
        else {
            return miller_rabin(n, {2, 325, 9375, 28178, 450775, 9780504, 1795265022});
        }
    }

    template <typename T>
    T pollard_rho (T n) {
        if (~n & 1) return 2;
        if (is_prime(n)) return n;

        static u128 x,y,c,d;
        auto f = [&](u128 x) {return (x * x % n + c) % n;};
        auto rnd_ = [&](T l, T r) {return rng() % (r - l + 1) + l;};

        x = rnd_(2, n);
        y = x;
        c = rnd_(1, n);
        d = 1;

        while (d == 1) {
            x = f(x);
            y = f(y); y = f(y);
            d = binary_gcd((x > y ? x-y : y-x), n);
            if ((T)d == n) {
                return pollard_rho(n);
            }
        }
        if (is_prime(d)) {
            return d;
        }
        else {
            return pollard_rho(d);
        }
    }

    template <typename T>
    vector<T> prime_factor (T n) {
        vector<T> res;
        for (T i = 2; i*i <= n;) {
            while (n % i == 0) {
                n /= i;
                res.emplace_back(i);
            }
            i += 1 + (~n & 1);
            if (i >= 101 and n >= (1<<20)) {
                while (n > 1) {
                    auto p = pollard_rho(n);
                    while (n % p == 0) {
                        n /= p;
                        res.emplace_back(p);
                    }
                }
                break;
            }
        }
        if (n > 1) res.emplace_back(n);
        sort(res.begin(), res.end());
        return res;
    }

    template <typename T>
    map<T, int> factor_count (T n) {
        map<T, int> mp;
        for (auto &x : prime_factor(n)) mp[x]++;
        return mp;
    }

    template <typename T>
    vector<T> divisors(T n) {
        if (n == 0) return {};
        vector<pair<T, int>> v;
        for(auto &p : factor_count(n)) v.push_back(p);
        vector<T> res;
        auto f = [&](auto self, int i, T x) -> void {
            if (i == (int)v.size()) {
                res.push_back(x);
                return;
            }
            for (int j = 0; j <= v[i].second; ++j) {
                self(self, i + 1, x);
                if (j+1 <= v[i].second) {
                    x *= v[i].first;
                }
            }
        };
        f(f, 0, 1);
        sort(res.begin(), res.end());
        return res;
    }

} // namespace factorize

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    ll p,q; cin >> p >> q;
    ll g = gcd(p,q);
    p /= g; q /= g;
    // MQ+NQ=PNM
    // PMQ+PNQ=P^2NM
    // (PN - Q)(PM - Q) = Q^2

    auto d = factorize::divisors(q*q);
    vector<pair<ll,ll>> res;
    for(int i=0;i<d.size();i++){
        ll a = d[i], b = q*q/d[i];
        if((a+q)%p == 0 and (b+q)%p == 0){
            res.push_back({(a+q)/p, (b+q)/p});
        }
    }
    sort(res.begin(), res.end());
    cout << res.size() << "\n";
    for(auto p:res){
        cout << p.first << " " << p.second << "\n";
    }
}
0