Skip to content

Instantly share code, notes, and snippets.

@WindAzure
Last active October 28, 2019 16:15
Show Gist options
  • Save WindAzure/5fb62acadda05b4fe7c8d6a00ee6436c to your computer and use it in GitHub Desktop.
Save WindAzure/5fb62acadda05b4fe7c8d6a00ee6436c to your computer and use it in GitHub Desktop.
UVa 1591
#include <limits>
#include <stdio.h>
int main()
{
auto N = 0, Sp = 0, Sq = 0;
while (~scanf("%d%d%d", &N, &Sp, &Sq))
{
auto baseLengthOfP = static_cast<long long>(Sp) * (N - 1);
auto baseLengthOfQ = static_cast<long long>(Sq) * (N - 1);
auto A = 0, B = 0;
auto K = std::numeric_limits<long long>::max();
for (auto currentA = 0; currentA <= 31; currentA++)
{
auto QofsBase = baseLengthOfP + (baseLengthOfP << currentA);
for (auto currentB = 0; currentB <= 31; currentB++)
{
auto Qofs = QofsBase >> currentB;
if (Qofs >= baseLengthOfQ && Qofs < K)
{
K = Qofs;
A = currentA;
B = currentB;
}
}
}
printf("%lld %d %d\n", K + Sq, A, B);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment