結果
問題 | No.195 フィボナッチ数列の理解(2) |
ユーザー |
![]() |
提出日時 | 2019-04-16 20:39:25 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
RE
|
実行時間 | - |
コード長 | 2,815 bytes |
コンパイル時間 | 1,902 ms |
コンパイル使用メモリ | 202,392 KB |
最終ジャッジ日時 | 2025-01-07 02:16:39 |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 RE * 1 |
other | AC * 13 WA * 2 RE * 7 |
ソースコード
#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 = 100; 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){ assert(0); 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; }