結果

問題 No.2125 Inverse Sum
ユーザー Nzt3Nzt3
提出日時 2022-12-24 00:23:36
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 169 ms / 2,000 ms
コード長 1,179 bytes
コンパイル時間 2,432 ms
コンパイル使用メモリ 209,592 KB
実行使用メモリ 5,540 KB
最終ジャッジ日時 2024-04-29 04:43:04
合計ジャッジ時間 4,154 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
long long P,Q;
vector<array<long long,2>>ans;
 void dfs(long long t,vector<array<long long,2>>& v,int c){
  if(c==v.size()){
    if((t+Q)%P==0&&(Q*Q/t+Q)%P==0){
      ans.push_back({(t+Q)/P,(Q*Q/t+Q)/P});
      ans.push_back({(Q*Q/t+Q)/P,(t+Q)/P});
    }
    if((-t+Q)%P==0&&(-Q*Q/t+Q)%P==0&&-t+Q!=0&&-Q*Q/t+Q!=0&&-t+Q>0&&-Q*Q/t+Q>0){
      ans.push_back({(-t+Q)/P,(-Q*Q/t+Q)/P});
      ans.push_back({(-Q*Q/t+Q)/P,(-t+Q)/P});
    }
    return;
  }
  dfs(t,v,c+1);
  for(int i=0;i<v[c][1];i++){
    t*=v[c][0];
    dfs(t,v,c+1);
  }
  for(int i=0;i<v[c][1];i++){
    t/=v[c][0];
  }
}

int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin>>P>>Q;
  long long g=gcd(P,Q);
  P/=g,Q/=g;
  vector<array<long long,2>>d;
  long long Q2=Q;
  for(long long i=2;i*i<=Q;i++){
    if(Q%i)continue;
    long long cnt=0;
    while(Q2%i==0){
      Q2/=i;
      ++cnt;
    }
    d.push_back({i,cnt});
  }
  if(Q2!=1)d.push_back({Q2,1});
  for(auto &i:d)i[1]*=2;
  dfs(1,d,0);
  sort(ans.begin(),ans.end());
  ans.erase(unique(ans.begin(),ans.end()),ans.end());
  cout<<ans.size()<<'\n';
  for(auto i:ans)cout<<i[0]<<' '<<i[1]<<'\n';
}
0