結果

問題 No.2125 Inverse Sum
ユーザー t98slidert98slider
提出日時 2022-11-19 12:20:20
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 15 ms / 2,000 ms
コード長 1,610 bytes
コンパイル時間 1,871 ms
コンパイル使用メモリ 180,572 KB
実行使用メモリ 4,780 KB
最終ジャッジ日時 2023-10-20 17:37:42
合計ジャッジ時間 2,941 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

//素因数分解
template <typename T> vector<pair<T, T>> prime_factor(T n) {
    vector<pair<T, T>> ret;
    T tmp=0;
    if(n%2==0){
        tmp=0;while(n%2==0){tmp++;n/=2;}
        ret.push_back(make_pair(2, tmp));
    }
    if(n%3==0){
        tmp=0;while(n%3==0){tmp++;n/=3;}
        ret.push_back(make_pair(3, tmp));
    }
    for (T i=5;i*i<=n;i+=4) {
        if(n%i==0){
            tmp=0;while(n%i==0){tmp++;n/=i;}
            ret.push_back(make_pair(i,tmp));
        }
        i+=2;
        if(n%i==0){
            tmp=0;while(n%i==0){tmp++;n/=i;}
            ret.push_back(make_pair(i,tmp));
        }
    }
    if(n!=1)ret.push_back(make_pair(n,1));
    return ret;
}

int main(){
    ll P, Q, Q2, g;
    cin >> P >> Q;
    g = __gcd(P, Q);
    P /= g, Q /= g;
    auto prime = prime_factor(Q);
    for(auto &&p:prime)p.second <<= 1;
    vector<ll> y;
    function<void(int,ll)> dfs = [&](int i, ll v){
        if(i == prime.size()){
            y.push_back(v);
            return;
        }
        ll d = 1;
        for(int j = 0; j <= prime[i].second; j++){
            if(j)d *= prime[i].first;
            dfs(i + 1, v * d);
        }
    };
    dfs(0, 1);
    vector<pair<ll,ll>> ans;
    Q2 = Q * Q;
    for(auto &&v:y){
        ll v1 = Q2 / v;
        if((v + Q) % P == 0 && (v1 + Q) % P == 0)ans.emplace_back((v + Q) / P, (v1 + Q) / P);
    }
    sort(ans.begin(), ans.end());
    cout << ans.size() << '\n';
    for(int i = 0; i < ans.size(); i++){
        cout << ans[i].first << ' ' << ans[i].second << '\n';
    }
}
0