Skip to code
Skip to analysis

This is a explanation of this problem from USACO's training website. I have converted it to markdown. Please do not just copy code; you will not learn anything; at least type it out and understand so you can do it yourself in the future!

It is a well-known fact that behind every good comet is a UFO. These UFOs often come to collect loyal supporters from here on Earth. Unfortunately, they only have room to pick up one group of followers on each trip. They do, however, let the groups know ahead of time which will be picked up for each comet by a clever scheme: they pick a name for the comet which, along with the name of the group, can be used to determine if it is a particular group’s turn to go (who do you think names the comets?). The details of the matching scheme are given below; your job is to write a program which takes the names of a group and a comet and then determines whether the group should go with the UFO behind that comet.

Both the name of the group and the name of the comet are converted into a number in the following manner: the final number is just the product of all the letters in the name, where “A” is 1 and “Z” is 26. For instance, the group “USACO” would be 21 * 19 * 1 * 3 * 15 = 17955. If the group’s number mod 47 is the same as the comet’s number mod 47, then you need to tell the group to get ready! (Remember that “a mod b” is the remainder left over after dividing a by b; 34 mod 10 is 4.)

Write a program which reads in the name of the comet and the name of the group and figures out whether according to the above scheme the names are a match, printing “GO” if they match and “STAY” if not. The names of the groups and the comets will be a string of capital letters with no spaces or punctuation, up to 6 characters long.


Input Output


This means that you fill in your header with:
PROG: ride
WARNING: You must have ‘ride’ in this field or the wrong test data (or no test data) will be used.


Line 1: An upper case character string of length 1..6 that is the name of the comet.
Line 2: An upper case character string of length 1..6 that is the name of the group.

NOTE: The input file has a newline at the end of each line but does not have a “return”. Sometimes, programmers code for the Windows paradigm of “return” followed by “newline”; don’t do that! Use simple input routines like “readln” (for Pascal) and, for C/C++, “fscanf” and “fid»string”.

NOTE 2: Because of the extra characters, be sure to leave enough room for a ‘newline’ (also notated as ‘\n’) and an end of string character (‘\0’) if your language uses it (as C and C++ do). This means you need eight characters of room instead of six.




A single line containing either the word “GO” or the word “STAY”.

SAMPLE OUTPUT (file ride.out)



Converting the letters to numbers:

3 15 13 5 20 17
8 22 14 7 1 20

then calculate the product mod 47:

3 * 15 * 13 * 5 * 20 * 17 = 994500 mod 47 = 27

8 * 22 * 14 * 7 * 1 * 20 = 344960 mod 47 = 27

Because both products evaluate to 27 (when modded by 47), the mission is ‘GO’.






Hal Burch

This is probably the easiest problem in the entire set of lessons. An ‘ad hoc’ problem, no particular algorithms or tricks are needed save one: one must be careful to get the input without processing the newline on the end!

Here is a prototype solution:

#include <stdio.h>
#include <ctype.h>

hash(char *s)
	int i, h;

	h = 1;
	for(i=0; s[i] && isalpha(s[i]); i++)
		h = ((s[i]-'A'+1)*h) % 47;
	return h;

	FILE *in, *out;
	char comet[100], group[100];  /* bigger than necessary, room for newline */

	in = fopen("input.txt", "r");
	out = fopen("output.txt", "w");

	fgets(comet, sizeof comet, in);
	fgets(group, sizeof group, in);

	if(hash(comet) == hash(group))
		fprintf(out, "GO\n");
		fprintf(out, "STAY\n");
	exit (0);

Solutions generally run in under 0.01 seconds.

Iain Found

Canada’s Iain Found created this straightforward solution (which has been slightly modified to make the highest quality presentation here):

with open('', 'r') as fin:
    comet1 = fin.readline().strip()
    comet2 = fin.readline().strip()
prod1, prod2 = 1, 1
for c in comet1: prod1 *= ord(c) - ord('A') + 1
for c in comet2: prod2 *= ord(c) - ord('A') + 1
with open('ride.out', 'w') as fout:
    if prod1 % 47 == prod2 % 47: fout.write('GO\n')
    else: fout.write('STAY\n')

Note that a couple important features are included:

  • Newlines are removed.
  • The with construct automatically closes files upon the block exit.

Tyler Adams

USACO alumnus Tyler Adams created this more functional-programming-oriented solution:

def char_to_num(c):
    return ord(c) - ord('A') + 1

def line_to_code(s):
    nums = map(char_to_num, s) 
    code = reduce(lambda x,y: x*y, nums) % 47 
    return code

with open("", "r") as input_file:
    raw_lines = input_file.readlines()
lines = map(lambda s: s.strip(), raw_lines)
codes = map(line_to_code, lines)
should_go = codes[0] == codes[1]

with open("ride.out", "w") as output_file:
    if should_go: output_file.write("GO\n") 
    else: output_file.write("STAY\n")

Note the good choice of variable names in both of these Python solutions. Don’t miss that even though some lines could be combined, that compression is not performed in interest of creating a program that is debuggable and maintainable.

Back to top