結果

問題 No.195 フィボナッチ数列の理解(2)
ユーザー beetbeet
提出日時 2019-04-16 20:47:28
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,797 bytes
コンパイル時間 2,339 ms
コンパイル使用メモリ 210,212 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-22 07:12:34
合計ジャッジ時間 3,762 ms
ジャッジサーバーID
(参考情報)
judge9 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}


using Int = __int128_t;
Int abs128(Int val){return val<0?-val:val;}

ostream &operator<<(ostream &os,Int val){
  if(ostream::sentry(os)){
    __uint128_t tmp=abs128(val);
    char buf[64];
    char *d=end(buf);
    do{
      --d;
      *d=char(tmp%10+'0');
      tmp/=10;
    }while(tmp);
    if(val<0) *--d='-';
    Int len=end(buf)-d;
    if(os.rdbuf()->sputn(d,len)!=len){
      os.setstate(ios_base::badbit);
    }
  }
  return os;
}

istream &operator>>(istream &is,Int &val){
  string s;
  is>>s;
  val=0;
  for(Int i=0;i<(Int)s.size();i++)
    if(isdigit(s[i])) val=val*10+s[i]-'0';
  if(s[0]=='-') val*=-1;
  return is;
}


//INSERT ABOVE HERE
signed main(){
  using P = pair<Int, Int>;
  vector<P> vp;
  vp.emplace_back(1,0);
  vp.emplace_back(0,1);
  const Int MAX = 60;
  for(Int i=0;i<MAX;i++){
    Int a=vp[i].first+vp[i+1].first;
    Int b=vp[i].second+vp[i+1].second;
    vp.emplace_back(a,b);
    cout<<a<<" "<<b<<endl;
  }
  Int x,y,z;
  cin>>x>>y>>z;
  P ans(1e18,-1);
  for(Int i=0;i<MAX;i++){
    for(Int j=0;j<MAX;j++){
      for(Int k=0;k<MAX;k++){
        if(i==j) continue;
        Int a=vp[i].first;
        Int b=vp[i].second;
        Int c=vp[j].first;
        Int d=vp[j].second;
        Int e=vp[k].first;
        Int f=vp[k].second;

        Int p=0,q=0;
        assert(b*c!=a*d);
        q=(x*c-y*a)/(b*c-a*d);
        if(a) p=(x-q*b)/a;
        else  p=(x-q*d)/c;
        
        if(p<=0||q<=0) continue;
        if(p*a+q*b!=x) continue;
        if(p*c+q*d!=y) continue;
        if(p*e+q*f!=z) continue;
                           
        chmin(ans,P(p,q));
      }
    }
  }
  
  for(Int i=0;i<MAX;i++){
    for(Int j=0;j<MAX;j++){
      for(Int k=0;k<MAX;k++){
        if(i==k) continue;
        Int a=vp[i].first;
        Int b=vp[i].second;
        Int c=vp[j].first;
        Int d=vp[j].second;
        Int e=vp[k].first;
        Int f=vp[k].second;

        Int p=0,q=0;
        assert(b*e!=a*f);
        q=(x*e-y*a)/(b*e-a*f);
        if(a) p=(x-q*b)/a;
        else  p=(x-q*f)/e; 
        if(p<=0||q<=0) continue;
        if(p*a+q*b!=x) continue;
        if(p*c+q*d!=y) continue;
        if(p*e+q*f!=z) continue;
                           
        chmin(ans,P(p,q));
      }
    }
  }
  
  if(x==y&&y==z){
    Int p=1;
    if(x==1) chmin(ans,P(1,1));
    for(Int i=1;i<MAX;i++){
      Int a=vp[i].first;
      Int b=vp[i].second;
      Int q=(x-p*a)/b;
      if(p*a+q*b!=x) continue;      
      if(p<=0||q<=0) continue;
      chmin(ans,P(p,q));
    }
  }
  if(~ans.second) cout<<ans.first<<" "<<ans.second<<endl;
  else cout<<-1<<endl;
  return 0;
}
0