結果

問題 No.2125 Inverse Sum
ユーザー umezoumezo
提出日時 2022-11-18 23:09:28
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,384 bytes
コンパイル時間 2,204 ms
コンパイル使用メモリ 212,692 KB
最終ジャッジ日時 2025-02-08 22:22:13
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 19 WA * 11
権限があれば一括ダウンロードができます

ソースコード

diff #

#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define ALL(v) v.begin(),v.end()
typedef long long ll;
 
#include<bits/stdc++.h>
using namespace std;

vector<ll> divisor(ll x){
  set<ll> s;
  for(ll i=1;i*i<=x;i++){
    if(x%i==0){
      s.insert(i);
      s.insert(x/i);
    }
  }
  vector<ll> res;
  for(auto y:s) res.push_back(y);
  return res;
}
 
int main(){
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  
  ll p,q;
  cin>>p>>q;
  
  if(p==2*q){
    cout<<1<<endl;
    cout<<1<<" "<<1<<endl;
    return 0;
  }
  if(p==q){
    cout<<1<<endl;
    cout<<2<<" "<<2<<endl;
    return 0;
  }
  if(3*q==2*p){
    cout<<2<<endl;
    cout<<1<<" "<<2<<endl;
    cout<<2<<" "<<1<<endl;
    return 0;
  }
  if(p==q+1){
    cout<<2<<endl;
    cout<<1<<" "<<q<<endl;
    cout<<q<<" "<<1<<endl;
    return 0;
  }
  if(p>q){ 
    cout<<0<<endl;
    return 0;
  }
  
  ll g=gcd(p,q);
  p/=g,q/=g;
  
  vector<ll> A=divisor(q);
  sort(ALL(A));
  
  vector<pair<ll,ll>> ANS;
  if((2*q)%p==0){
    ll k=2*q/p;
    if(q%k) ANS.push_back({k,k});
  }
  
  int a=A.size();
  rep(i,a){
    if(p-A[i]<=0) continue;
    auto t=lower_bound(ALL(A),p-A[i])-A.begin();
    if(t==a) continue;
    if(A[t]+A[i]==p){
      ANS.push_back({q/A[i],q/A[t]});
    }
  }
  int n=ANS.size();
  sort(ALL(ANS));
  cout<<n<<endl;
  rep(i,n){
    cout<<ANS[i].first<<" "<<ANS[i].second<<endl;
  }
    
  return 0;
}
0