結果

問題 No.195 フィボナッチ数列の理解(2)
ユーザー 0w10w1
提出日時 2017-01-05 15:29:48
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 2,583 bytes
コンパイル時間 2,209 ms
コンパイル使用メモリ 178,052 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-22 12:27:29
合計ジャッジ時間 2,892 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector< int > vi;
typedef vector< vi > vvi;
typedef vector< ll > vl;
typedef vector< vl > vvl;
typedef pair< int, int > pii;
typedef vector< pii > vp;
typedef vector< double > vd;
typedef vector< vd > vvd;
typedef vector< string > vs;

template< class T1, class T2 >
int upmin( T1 &x, T2 v ){
  if( x > v ){
    x = v;
    return 1;
  }
  return 0;
}

template< class T1, class T2 >
int upmax( T1 &x, T2 v ){
  if( x < v ){
    x = v;
    return 1;
  }
  return 0;
}

const int INF = 0x3f3f3f3f;

int X, Y, Z;

void init(){
  cin >> X >> Y >> Z;
}

vvi mat_mul( vvi a, vvi b ){
  vvi c( a.size(), vi( a[ 0 ].size() ) );
  for( int i = 0; i < a.size(); ++i ){
    for( int j = 0; j < a[ 0 ].size(); ++j ){
      for( int k = 0; k < b[ 0 ].size(); ++k ){
        c[ i ][ k ] += a[ i ][ j ] * b[ j ][ k ];
      }
    }
  }
  return c;
}

const vvi identity = { { 1, 0 }, { 0, 1 } };
const vvi feature = { { 1, 1 }, { 1, 0 } };

vvi pf[ 50 ]; // powered f

void preprocess(){
  vi t = { X, Y, Z };
  sort( t.begin(), t.end() );
  X = t[ 0 ], Y = t[ 1 ], Z = t[ 2 ];
  pf[ 0 ] = identity;
  for( int i = 0; i + 1 < 50; ++i ){
    pf[ i + 1 ] = mat_mul( pf[ i ], feature );
  }
}

void solve(){
  if( X == Y and Y == Z ){
    X = 1;
  } else if( X == Y ){
    Y = Z;
  }
  vvi t = identity;
  int min_A = INF, min_B = INF;
  for( int i = 0; i < 45; ++i ){
    for( int j = i; j < 45; ++j ){
      int a = pf[ j ][ 0 ][ 0 ], b = pf[ j ][ 0 ][ 1 ], c = pf[ i ][ 1 ][ 0 ], d = pf[ i ][ 1 ][ 1 ];
      if( a * d == b * c ){
        min_A = min_B = 1;
      } else{
        int A = -1;
        if( ( c * Y - a * X ) % ( b * c - a * d ) == 0 ){
          A = ( c * Y - a * X ) / ( b * c - a * d );
        }
        int B = -1;
        if( ( d * Y - b * X ) % ( a * d - b * c ) == 0 ){
          B = ( d * Y - b * X ) / ( a * d - b * c );
        }
        if( A > 0 and B > 0 ){
          int ok = B == Z;
          for( int p = A, q = B; q < Z; ){
            if( p + q == Z ){
              ok = 1;
              break;
            }
            int tmp = p + q;
            p = q;
            q = tmp;
          }
          if( not ok ) continue;
          if( min_A == A ){
            upmin( min_B, B );
          } else if( upmin( min_A, A ) ){
            min_B = B;
          }
        }
      }
    }
  }
  if( min_A == INF ){
    cout << -1 << endl;
  } else{
    cout << min_A << " " << min_B << endl;
  }
}

signed main(){
  ios::sync_with_stdio( 0 );
  init();
  preprocess();
  solve();
  return 0;
}
0