PROBLEM LINK:
Contest Division 1
Contest Division 2
Contest Division 3
Setter: Srikkanth R
Tester: Venkata Nikhil Medam, Tejas Pandey
Editorialist: Kanhaiya Mohan
DIFFICULTY:
Cakewalk
PREREQUISITES:
None
PROBLEM:
Kepler’s Law of Period states - “The square of the time period of the planet is directly proportional to the cube of the semimajor axis of its orbit.”
Given the Time periods (T_1, T_2) and Semimajor Axes (R_1, R_2) of two planets orbiting the same star, determine if the Law of Periods is satisfied.
QUICK EXPLANATION:
The Law of Periods is satisfied when {T_1}^2 \cdot {R_2}^3 = {T_2}^2 \cdot {R_1}^3.
EXPLANATION:
Let T denote the time period of the planet and R denote the length of the semimajor axis of its orbit. Then, according to Kepler’s Law of Periods, T^2 ∝ R^3. In other words, the ratio \frac{T^2}{R^3} = K, is a constant, also known as Kepler’s Constant.
We are given the values of (T, R) for two planets. Those are, (T_1, R_1) for planet 1 and (T_2, R_2) for planet 2. The respective Kepler’s Constants are K_1 = \frac{{T_1}^2}{{R_1}^3}, and K_2 = \frac{{T_2}^2}{{R_2}^3}.
For both the planets to follow Kepler’s Law, the value of both the constants should be same.
In other words, \frac{{T_1}^2}{{R_1}^3} = \frac{{T_2}^2}{{R_2}^3}.
Comparing the ratios: If we compare the ratios, we need to keep in mind that the data types of the variables is float/double. If we declare them as integers, the ratios are rounded down to the nearest integer which results in a wrong answer.
Another way is to cross multiply the denominators and check if {T_1}^2 \cdot {R_2}^3 = {T_2}^2 \cdot {R_1}^3. This works even when the data type is integer.
TIME COMPLEXITY:
The time complexity is O(1) per test case.
SOLUTION:
Tester's Solution
#include <iostream>
using namespace std;
int main() {
int t;
cin >> t;
while(t--) {
int t1,t2,r1,r2;
cin >> t1 >> t2 >> r1 >> r2;
int ok = 0;
ok = (t1*t1*r2*r2*r2 == t2*t2*r1*r1*r1);
cout << (ok?"YES\n":"NO\n");
}
}
Editorialist's Solution
#include <bits/stdc++.h>
using namespace std;
#define sync {ios_base ::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);}
#define rep(n) for(int i = 0;i<n;i++)
#define rep1(a,b) for(int i = a;i<b;i++)
#define int long long int
#define mod 1000000007
int t1, t2, r1, r2;
void solve()
{
cin>>t1>>t2>>r1>>r2;
int product1 = (t1*t1)*(r2*r2*r2);
int product2 = (t2*t2)*(r1*r1*r1);
if(product1 == product2){
cout<<"Yes";
}
else{
cout<<"No";
}
}
int32_t main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
sync;
int t = 1;
cin>>t;
while(t--){
solve();
cout<<"\n";
}
return 0;
}