محدودیت زمان: ۱ ثانیه
محدودیت حافظه: ۲۵۶ مگابایت
درخت دودویی از کسرها داریم که روی هر راسش کسری به فرم
a
i
b
i
b
i
a
i
نوشته شده است. درخت به این شکل ساخته میشود:
a
1
a
1
b
1
b
1
1
1
a
2
i
a
2i
= (
a
i
a
i
+
b
i
b
i
) ,
b
2
i
b
2i
b
i
b
i
(بچه سمت چپ)
a
2
i
+
1
a
2i+1
a
i
a
i
,
b
2
i
+
1
b
2i+1
= (
a
i
a
i
+
b
i
b
i
) (بچه سمت راست)
فاصله دو راس در درخت را تعداد یالهای تنهای مسیر بینشان تعریف میکنیم.
به شما
Q
Q درخواست که هر کدام شامل کسری به فرم
p
i
q
i
q
i
p
i
داده میشود.
برای هر عدد موجود در درخواستها، اگر تنها یک راس در درخت وجود داشت که کسرش برابر کسر ورودی داده شده بود، فاصله راس آن کسر تا راس ۱ را در خروجی چاپ کنید و در غیر این صورت
−
1
−1 خروجی دهید.
ورودی
در خط اول
Q
Q و در
Q
Q خط بعدی
p
i
p
i
و
q
i
q
i
به شما داده شده.
1
≤
Q
≤
100
000
1≤Q≤100 000
1
≤
p
i
,
q
i
≤
1
0
18
1≤p
i
,q
i
≤10
18
خروجی
در خط
i
iام اگر کسری معادل عدد
p
i
q
i
q
i
p
i
بود و فقط این کسر معادل این عدد بود فاصلهی آن را از راس ۱ خروجی دهید در غیر این صورت
−
1
−1 خروجی دهید.
مثال
ورودی نمونه ۱
2
1 2
3 5
Plain text
خروجی نمونه ۱
1
3
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
To solve the problem of determining the distance of a given fraction ( \frac{p}{q} ) from the root ( \frac{1}{1} ) in a specific binary tree, we need to understand how the tree is constructed and how to navigate it efficiently.
Tree Construction
The tree is constructed with each node containing a fraction ( \frac{a_i}{b_i} ). The root of the tree is ( \frac{1}{1} ). For any node ( \frac{a}{b} ):
The left child is ( \frac{a+b}{b} ).
The right child is ( \frac{a}{a+b} ).
Importantly, every fraction in this tree is in its simplest form (i.e., ( \gcd(a, b) = 1 )), ensuring that each fraction appears exactly once in the tree.
Problem Approach
To determine the distance from a given fraction ( \frac{p}{q} ) to the root:
Check if the fraction is in its simplest form: If ( \gcd(p, q) \neq 1 ), the fraction cannot appear in the tree, so we should return (-1).
Calculate the number of steps to reach the root: If the fraction is in its simplest form, we can simulate the process of navigating back to the root by repeatedly subtracting the smaller part from the larger until we reach ( \frac{1}{1} ). Each subtraction corresponds to a step in the tree.
To efficiently compute the number of steps:
Use a loop that continues until ( \frac{p}{q} ) becomes ( \frac{1}{1} ).
At each step, determine whether to subtract the smaller number from the larger.
Count each subtraction as a step.
If at any point we encounter an invalid fraction (i.e., not in its simplest form), return (-1).
Implementation
The following C++ code implements the above logic efficiently, ensuring that it handles large values of ( p ) and ( q ) within the given constraints.
cpp
#include <bits/stdc++.h>
using namespace std;
// Function to compute the number of steps
long long compute_steps(long long p, long long q){
long long steps = 0;
while (!(p ==1 && q ==1)){
if (p > q){
if (q ==1){
steps += p -1;
p =1;
}
else{
long long k = p / q;
if (p % q ==0){
// p and q are coprime, so p =k*q implies q=1
// but q !=1 handled above
// Given gcd(p,q)=1 and p >q, p%q !=0
k -=1;
p = p - k * q;
steps +=k;
}
else{
steps +=k;
p = p % q;
}
}
}
else{
if (p ==1){
steps += q -1;
q =1;
}
else{
long long k = q / p;
if (q % p ==0){
// p and q are coprime, so q =k*p implies p=1
// already handled if p==1
k -=1;
q = q - k * p;
steps +=k;
}
else{
steps +=k;
q = q % p;
}
}
}
}
return steps;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int Q;
cin >> Q;
while(Q--){
long long p, q;
cin >> p >> q;
if (__gcd(p, q) !=1){
cout << "-1\n";
}
else{
long long steps = compute_steps(p, q);
cout << steps << "\n";
}
}
}
Explanation of the Code
Input Handling: We first read the number of queries ( Q ). For each query, we read the fraction ( \frac{p}{q} ).
GCD Check: We check if ( \gcd(p, q) ) is 1. If not, we output (-1).
Step Calculation: If the fraction is in its simplest form, we calculate the number of steps required to reach the root ( \frac{1}{1} ) by repeatedly subtracting the smaller value from the larger.
Output: We output the number of steps for each valid fraction or (-1) if the fraction cannot be found in the tree.
This approach ensures that we handle all possible inputs efficiently, even for large values of ( p ) and ( q ), within the problem's constraints.