USACO Bronze 2017 Open - The Lost Cow

Authors: Jesse Choe, Ananth Kashyap, Brad Ma


Official Analysis

Implementation

Time Complexity: O(logxy)\mathcal{O}(\log|x-y|)

C++

// Created by Jesse Choe - Bronze Template
#include <bits/stdc++.h>
using namespace std;
// Type aliases
using ll = long long;
using str = string;
using vi = vector<int>;

Python

# Take in the input data using python standard file I/O system
file_in = open("lostcow.in")
data = file_in.read().strip().split("\n")
x, y = map(int, data[0].split(" "))
# Compute the number of times Farmer John must change direction
if y > x:
p = 0
while (2**p) + x < y:
p += 2

Java

import java.io.*;
import java.util.StringTokenizer;
public class theLostCow {
public static void main(String[] args) throws IOException {
Kattio io = new Kattio("lostcow");
int x = io.nextInt();
int y = io.nextInt();
int direction = 1;

Join the USACO Forum!

Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!