PLRTRAVELS Editorial

PROBLEM LINK:

Contest

Author: Amul Agarwal
Tester: Ishaan Shah, Sanyam Shah, Sambasai Andem and Tejas Chaudhari
Editorialist: Rutvij Menavlikar

DIFFICULTY:

EASY

EXPLANATION:

In this problem, all the required hints were hidden in the sample test case.
For each planet, we define its base as the index of it when we write planets in increasing order of distance from the sun. For example, Mercury has base 1, Venus has base 2, Neptune has base 8.
For this problem, you had to do the base conversion of departure time to get the arrival time.

SOLUTIONS:

Setter's Solution
planets = ["mercury", "venus", "earth", "mars",
           "jupiter", "saturn", "uranus", "neptune"]


def tobase10(s, base):
    if base == 1:
        return len(str(s))
    s = int(s)
    ans, pw = 0, 1
    while s > 0:
        ans += pw * (s % 10)
        s = s // 10
        pw *= base
    return ans


def tobasek(s, k):
    s = int(s)
    if k == 1:
        return "1" * s
    digits = []
    while s:
        digits.append(str(s % k))
        s //= k
    return "".join(digits[::-1])


T = int(input())
for _ in range(T):
    dep, arr, tm = map(str, input().split())
    from_base = planets.index(dep) + 1
    to_base = planets.index(arr) + 1
    base_10 = tobase10(tm, from_base)
    base_to = tobasek(base_10, to_base)
    print(base_to)
Tester's Solution
#include <bits/stdc++.h>
using namespace std;
#define fast                      \
	ios_base::sync_with_stdio(0); \
	cin.tie(0);                   \
	cout.tie(0)
#define tc       \
	long long t; \
	cin >> t;    \
	while (t--)

long long retbase(string s)
{
	if (s == "mercury")
	{
		return 1;
	}
	else if (s == "venus")
	{
		return 2;
	}
	else if (s == "earth")
	{
		return 3;
	}
	else if (s == "mars")
	{
		return 4;
	}
	else if (s == "jupiter")
	{
		return 5;
	}
	else if (s == "saturn")
	{
		return 6;
	}
	else if (s == "uranus")
	{
		return 7;
	}
	else if (s == "neptune")
	{
		return 8;
	}
	else if (s == "pluto")
	{
		return 9;
	}
	return -1;
}

// Function to return ASCII
// value of a character
long long val(char c)
{
	if (c >= '0' && c <= '9')
		return (long long)c - '0';
	else
		return (long long)c - 'a' + 10;
}

long long toDeci(string str, long long base)
{
	// Stores the length
	// of the string
	long long len = str.size();

	// Initialize power of base
	long long power = 1;

	// Initialize result
	long long num = 0;

	// Decimal equivalent is str[len-1]*1
	// + str[len-2]*base + str[len-3]*(base^2) + ...
	for (long long i = len - 1; i >= 0; i--)
	{

		// A digit in input number must
		// be less than number's base
		if (val(str[i]) > base)
		{
			cout << val(str[i]) << endl;
			printf("Invalid Number");
			return -1;
		}

		// Update num
		num += val(str[i]) * power;

		// Update power
		power = power * base;
	}

	return num;
}

// Function to return equivalent
// character of a given value
char reVal(long long num)
{
	if (num >= 0 && num <= 9)
		return (char)(num + '0');
	else
		return (char)(num - 10 + 'A');
}

// Function to convert a given
// decimal number to a given base
string fromDeci(long long base, long long inputNum)
{
	// Store the result
	string res = "";
	if (base == 1)
	{
		while (inputNum > 0)
		{
			res += "1";
			inputNum--;
		}
	}
	else
	{
		// Repeatedly divide inputNum
		// by base and take remainder
		while (inputNum > 0)
		{

			// Update res
			res += reVal(inputNum % base);

			// Update inputNum
			inputNum /= base;
		}
	}

	// Reverse the result
	reverse(res.begin(), res.end());

	return res;
}

// Function to convert a given number
// from a base to another base
void convertBase(string s, long long a, long long b)
{
	// Convert the number from
	// base A to decimal
	long long num = toDeci(s, a);

	// Convert the number from
	// decimal to base B
	string ans = fromDeci(b, num);

	// Print the result
	cout << ans << endl;
}

signed main()
{
	fast;
	tc
	{
		long long base1, base2;
		string home, away, dep_time;
		cin >> home >> away >> dep_time;
		base1 = retbase(home);
		base2 = retbase(away);
		convertBase(dep_time, base1, base2);
	}
}
Editorialist's Solution
#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define pb push_back
#define io                            \
	ios_base::sync_with_stdio(false); \
	cin.tie(NULL);                    \
	cout.tie(NULL)
#define all(v) v.begin(), v.end()
#define tt     \
	ll tc;     \
	cin >> tc; \
	while (tc--)
#define each(x, v) for (auto x : v)

ll ind(string s)
{
	if (s == "mercury")
		return 1;
	else if (s == "venus")
		return 2;
	else if (s == "earth")
		return 3;
	else if (s == "mars")
		return 4;
	else if (s == "jupiter")
		return 5;
	else if (s == "saturn")
		return 6;
	else if (s == "uranus")
		return 7;
	else if (s == "neptune")
		return 8;
	else
		return -1;
}

ll num(string s, ll b)
{
	ll ans = 0, a = 1;
	reverse(all(s));
	each(c, s)
	{
		ans += a * (c - '0');
		a *= b;
	}
	return ans;
}

string str(ll n, ll b)
{
	string ans = "";
	if (b == 1)
	{
		for (ll i = 0; i < n; ++i)
			ans.pb('1');
		return ans;
	}
	while (n)
	{
		ans.pb('0' + (n % b));
		n /= b;
	}
	reverse(all(ans));
	return ans;
}

int main()
{
	io;
	tt
	{
		string s1, s2, s3;
		cin >> s1 >> s2 >> s3;
		ll b1 = ind(s1), b2 = ind(s2);
		cout << str(num(s3, b1), b2) << endl;
	}
	return 0;
}