結果

問題 No.195 フィボナッチ数列の理解(2)
ユーザー hashiryohashiryo
提出日時 2019-04-10 20:42:40
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,752 bytes
コンパイル時間 697 ms
コンパイル使用メモリ 94,576 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-22 08:28:42
合計ジャッジ時間 1,739 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘void init()’:
main.cpp:41:15: warning: iteration 48 invokes undefined behavior [-Waggressive-loop-optimizations]
       F[i][j] = F[i][j-1]+F[i][j-2];
       ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
main.cpp:40:18: note: within this loop
     for(int j=2;j<=50;j++){
                 ~^~~~

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <limits.h>
#include <math.h>
#include <functional>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <float.h>
#include <random>

#define repeat(i,n) for (int i = 0; (i) < (n); ++ (i))
#define debug(x) cerr << #x << ": " << x << '\n'
#define debugArray(x,n) for(long long hoge = 0; (hoge) < (n); ++ (hoge)) cerr << #x << "[" << hoge << "]: " << x[hoge] << '\n'
#define debugArrayP(x,n) for(long long hoge = 0; (hoge) < (n); ++ (hoge)) cerr << #x << "[" << hoge << "]: " << x[hoge].first<< " " << x[hoge].second << '\n'

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> Pii;
typedef vector<int> vint;
typedef vector<ll> vll;
const long long INF = INT_MAX;
const ll MOD = 1e9+7;

ll F[2][50];
void init(){
  repeat(i,2){
    F[i][0]=!i;
    F[i][1]=i;
    for(int j=2;j<=50;j++){
      F[i][j] = F[i][j-1]+F[i][j-2];
    }
  }
}

int main(){
  init();
  ll X,Y,Z;cin>>X>>Y>>Z;
  ll A=INF,B=INF;
  if(X==Y)swap(Y,Z);
  if(X==Y)X=1;
  for(int p=0;F[0][p]<=X;p++){
    for(int q=0;F[0][q]<=Y;q++){
      ll d=F[0][p]*F[1][q]-F[0][q]*F[1][p];
      ll a=F[1][q]*X-F[1][p]*Y;
      ll b=F[0][p]*Y-F[0][q]*X;
      if(!d||a/d<=0||b/d<=0)continue;
      d = abs(d);
      a = abs(a);
      b = abs(b);
      if(a%d==0&&b%d==0&&(A>(a/d)||((A==a/d)&&(B>b/d)))){
        for(int r=0;F[0][r]<=Z;r++){
          if(a/d*F[0][r]+b/d*F[1][r]==Z){
            A=a/d;
            B=b/d;
          }
        }
      }
    }
  }
  if(A==INF){
    cout<<-1<<endl;
  }else{
    cout << A <<" "<< B <<endl;
  }
  return 0;
}
0