【JZOJ】【数论?】capacitor

L i n k Link Link

JZOJ capacitor

D e s c r i p t i o n Description Description (大意)

对于一个 x y \frac{x}{y} yx而言,我们可以将它变为 y x + y \frac{y}{x+y} x+yy x + y y \frac{x+y}{y} yx+y,那么现在有一个最终结果 a b \frac{a}{b} ba,求从 1 1 1变为 a b \frac{a}{b} ba需要多少次变化

D e s c r i p t i o n Description Description (原题)

在这里插入图片描述

I n p u t Input Input

在这里插入图片描述

O u t p u t Output Output

在这里插入图片描述

S a m p l e Sample Sample I n p u t Input Input

在这里插入图片描述

S a m p l e Sample Sample O u t p u t Output Output

在这里插入图片描述

H i n t Hint Hint

在这里插入图片描述

T r a i n Train Train o f of of T h o u g h t Thought Thought

首先先将 a b \frac{a}{b} ba化为最简分数,然后一步步逆推就好了

C o d e Code Code

#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);
	}
}      
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值