結果

問題 No.2125 Inverse Sum
ユーザー ripityripity
提出日時 2022-11-18 23:03:53
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 75 ms / 2,000 ms
コード長 1,560 bytes
コンパイル時間 3,957 ms
コンパイル使用メモリ 219,836 KB
実行使用メモリ 4,796 KB
最終ジャッジ日時 2023-10-20 08:02:45
合計ジャッジ時間 5,539 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
unordered_map<long long, int> factorize(long long x) {
    unordered_map<long long, int> mp;
    for( long long i = 2LL; i*i <= x; i++ ) {
        while( x%i == 0LL ) {
            mp[i]++;
            x /= i;
        }
    }
    if( x != 1LL ) mp[x]++;
    return mp;
}
int main() {
    // int N;
    // P*N*M=Q*(N+M)
    // P * N * M - Q * N - Q * M = 0
    // ( P * M - Q ) * N - Q * M = 0
    // ( P * M - Q ) * N + ( P * M - Q ) * ( - Q / P ) = Q * Q / P
    // ( P * M - Q ) * ( N - ( Q / P ) ) = Q * Q / P
    "( P * M - Q ) * ( P * N - Q ) = Q * Q";
    long long P, Q;
    constexpr long long INF = 1LL<<61;
    cin >> P >> Q;
    vector<pair<long long, long long>> ans;
    unordered_map<long long, int> fact = factorize(Q);
    deque<long long> que1, que2;
    que1.emplace_back(1LL);
    for( auto& [p, t] : fact ) {
        long long pw = 1LL;
        for( int i = 0; i <= 2*t; i++, pw *= p ) {
            for( long long& crr : que1 ) {
                if( crr <= INF/pw ) que2.emplace_back(crr*pw);
            }
        }
        swap(que1, que2);
        que2.clear();
    }
    for( long long& d : que1 ) {
        long long d2 = Q*Q/d;
        if( (d+Q)%P == 0LL && (d2+Q)%P == 0LL ) ans.emplace_back(make_pair((d+Q)/P, (d2+Q)/P));
        if( (-d+Q)%P == 0LL && (-d2+Q)%P == 0LL && -d+Q > 0 && -d2+Q > 0 ) ans.emplace_back(make_pair((-d+Q)/P, (-d2+Q)/P));
    }
    sort(ans.begin(), ans.end());
    cout << ans.size() << endl;
    for( auto& [fi, se] : ans ) cout << fi << ' ' << se << endl;
}
0