結果

問題 No.195 フィボナッチ数列の理解(2)
ユーザー antaanta
提出日時 2015-04-26 23:12:29
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 3,023 bytes
コンパイル時間 668 ms
コンパイル使用メモリ 80,404 KB
実行使用メモリ 4,508 KB
最終ジャッジ日時 2023-09-18 09:41:20
合計ジャッジ時間 1,802 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <set>
#include <map>
#include <queue>
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <cctype>
#include <cassert>
#include <limits>
#include <functional>
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
#if defined(_MSC_VER) || __cplusplus > 199711L
#define aut(r,v) auto r = (v)
#else
#define aut(r,v) __typeof(v) r = (v)
#endif
#define each(it,o) for(aut(it, (o).begin()); it != (o).end(); ++ it)
#define all(o) (o).begin(), (o).end()
#define pb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))
#define mset(m,v) memset(m,v,sizeof(m))
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
using namespace std;
typedef vector<int> vi; typedef pair<int,int> pii; typedef vector<pair<int,int> > vpii; typedef long long ll;
template<typename T, typename U> inline void amin(T &x, U y) { if(y < x) x = y; }
template<typename T, typename U> inline void amax(T &x, U y) { if(x < y) x = y; }

ll exgcd(ll a, ll b, ll &g) {
	ll u = 1, v = 0;
	while(b) {
		ll t = a / b;
		a -= t * b; swap(a, b);
		u -= t * v; swap(u, v);
	}
	g = a;
	return u;
}

int main() {
	//F_{A,B}(n+1) = F(n) A + F(n-1) B
	//F(i) A + F(i-1) B = X
	//F(j) A + F(j-1) B = Y

	//F(k) A + F(k-1) B = Z
	//
	int X, Y, Z;
	scanf("%d%d%d", &X, &Y, &Z);
	vector<int> F;
	F.push_back(0);
	F.push_back(1);
	while(F.back() <= (int)1e9)
		F.push_back(F.end()[-1] + F.end()[-2]);
	pair<ll,ll> ans(INFL,INFL);
	reu(i, 1, F.size()) reu(j, 1, F.size()) {
		ll a = F[i], b = F[i-1];
		ll c = F[j], d = F[j-1];
		//a x + b y = X
		//c x + d y = Y
		ll A, B;
		ll det = (ll)a * d - (ll)b * c;
		if(det == 0) {
			if(b == 0) {
				if(a == 0) continue;
				A = X / a, B = 1;
			}else {
				ll g, t = exgcd(a, b, g);
				assert(g == 1);
				if(X % g != 0) continue;
				//a t + b u = g
				ll u = (g - a * t) / b;
				ll tt = (ll)t * (X / g), uu = (ll)u * (X / g);
				if(tt <= 0) {
					ll l = b / g;
					//a (tt + n l) + b (uu - n l a / b) = X
					ll n = ((-tt+1) + l - 1) / l;
					tt += n * l;
					uu -= n * ((ll)l * a / b);
				}else if(uu <= 0) {
					ll l = a / g;
					//a (tt - n l b / a) + b (uu + n l) = X
					ll n = ((-uu+1) + l - 1) / l;
					tt -= n * ((ll)l * b / a);
					uu += n * l;
				}
				if(tt <= 0 || uu <= 0) continue;
				A = tt, B = uu;
				if(a * A + b * B != X) {
					cerr << "err" << endl;
				}
				if(c * A + d * B != Y) continue;
			}
		}else {
			ll xN = (ll)d * X - (ll)b * Y;
			ll yN = (ll)a * Y - (ll)c * X;
			if(xN % det != 0 || yN % det != 0) continue;
			A = xN / det, B = yN / det;
			if(A <= 0 || B <= 0) continue;
		}
		bool ok = false;
		reu(k, 1, F.size())
			ok |= F[k] * A + F[k-1] * B == Z;
		if(ok) {
			amin(ans, mp(B, A));
		}
	}
	if(ans.first == INFL)
		puts("-1");
	else
		cout << ans.first << " " << ans.second << endl;
	return 0;
}
0