結果

問題 No.195 フィボナッチ数列の理解(2)
ユーザー 👑 NachiaNachia
提出日時 2021-02-06 18:50:39
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 931 bytes
コンパイル時間 2,005 ms
コンパイル使用メモリ 193,656 KB
最終ジャッジ日時 2025-01-18 13:40:53
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 22
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using LL=long long;
using ULL=unsigned long long;
#define rep(i,n) for(int i=0; i<(n); i++)

LL X,Y,Z;
LL F[45];
int main(){
  cin>>X>>Y>>Z;
  if(X!=Z) swap(Y,Z);
  if(Y!=Z) swap(X,Z);
  F[0]=1;F[1]=0;
  for(int i=2; i<45; i++) F[i]=F[i-1]+F[i-2];
  pair<LL,LL> ans={1000000000000,-1};
  if(X!=Y) rep(i,44) rep(j,44){
    LL D=F[j+1]*F[i]-F[i+1]*F[j];
    if(D==0) continue;
    LL A=F[j+1]*X-F[i+1]*Y;
    LL B=F[i]*Y-F[j]*X;
    if(A%D) continue; A/=D; if(A<=0) continue;
    if(B%D) continue; B/=D; if(B<=0) continue;
    bool ok=false;
    rep(k,44) if(Z==A*F[k]+B*F[k+1]) ok=true;
    if(!ok) continue;
    ans=min(ans,make_pair(A,B));
  }
  else for(int i=1; i<44; i++){
    LL B=X-F[i]; if(B%F[i+1]) continue; B/=F[i+1]; if(B<=0) continue;
    ans=min(ans,make_pair(1ll,B));
  }
  if(ans.second==-1) cout<<-1<<endl;
  else cout<<ans.first<<" "<<ans.second<<endl;
  return 0;
}
0