【JZOJ】【數論?】capacitor

LinkLink

JZOJ capacitor

DescriptionDescription (大意)

對於一個xy\frac{x}{y}而言,我們可以將它變爲yx+y\frac{y}{x+y}x+yy\frac{x+y}{y},那麼現在有一個最終結果ab\frac{a}{b},求從11變爲ab\frac{a}{b}需要多少次變化

DescriptionDescription (原題)

在這裏插入圖片描述

InputInput

在這裏插入圖片描述

OutputOutput

在這裏插入圖片描述

SampleSample InputInput

在這裏插入圖片描述

SampleSample OutputOutput

在這裏插入圖片描述

HintHint

在這裏插入圖片描述

TrainTrain ofof ThoughtThought

首先先將ab\frac{a}{b}化爲最簡分數,然後一步步逆推就好了

CodeCode

#include<cstdio>
#include<iostream>
#define ll long long
using namespace std;
int T;
long long a, b, ans;

ll gcd(ll x, ll y)
{
	if (!y) return x;
	return gcd (y, x % y);
}//求最大公因數

int main()
{
//	freopen("capacitor.in", "r", stdin);
//	freopen("capacitor.out", "w", stdout);
	
	scanf("%d", &T);
	
	for (int i = 1; i <= T; ++i)
	{
		ans = 0;
		scanf("%lld%lld", &a, &b);
		
		ll t = gcd (a, b);
		a /= t;
		b /= t;
		
		if (a < b) swap(a, b);
		while (b != 1)
		{
			a -= b; ans++;
			if (a < b) swap(a, b); 
		}//逆推
		
		printf("%lld\n", a + ans);
	}
}      
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章