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!

Each of five opaque spinning wheels has one or more wedges cut out of its edges. These wedges must be aligned quickly and correctly. Each wheel also has an alignment mark (at 0 degrees) so that the wheels can all be started in a known position. Wheels rotate in the ‘plus degrees’ direction, so that shortly after they start, they pass through 1 degree, 2 degrees, etc. (though probably not at the same time).

This is an integer problem. Wheels are never actually at 1.5 degrees or 23.51234123 degrees. For example, the wheels are considered to move instantaneously from 20 to 25 degrees during a single second or even from 30 to 40 degrees if the wheel is spinning quickly.

All angles in this problem are presumed to be integers in the range 0 <= angle <= 359. The angle of 0 degrees follows the angle of 359 degrees. Each wheel rotates at a certain integer number of degrees per second, 1 <= speed <= 180.

Wedges for each wheel are specified by an integer start angle and integer angle size (or ‘extent’), both specified in degrees. Wedges in the test data will be separated by at least one degree. The ‘extent’ also includes the original “degree” of the wedge, so ‘0 180’ means degrees 0..180 inclusive – one more than most would imagine.

At the start, which is time 0, all the wheels’ alignment marks line up. Your program must determine the earliest time (integer seconds) at or after the start that some wedge on each wheel will align with the wedges on the other wheel so that a light beam can pass through openings on all five wedges. The wedges can align at any part of the rotation.

### INPUT FORMAT

Each of five input lines describes a wheel.

The first integer on an input line is the wheel’s rotation speed. The next integer is the number of wedges, 1 <= W <= 5. The next W pairs of integers tell each wedge’s start angle and extent.

### SAMPLE INPUT (file spin.in)

``````30 1 0 120
50 1 150 90
60 1 60 90
70 1 180 180
90 1 180 60
``````

### OUTPUT FORMAT

A single line with a single integer that is the first time the wedges align so a light beam can pass through them. Print ‘none’ (lower case, no quotes) if the wedges will never align properly.

### SAMPLE OUTPUT (file spin.out)

``````9
``````

Java

C++

Pascal

## ANALYSIS

Hal Burch

The key observation for this problem is that after 360 seconds, the wheels have returned to their original locations, so if the wheels don’t line up in 360 seconds, they will never line up.

To determine if there is a location through which a light can be shine, mark, for each wheel, which angles between 0 and 359 a light can be shone through. If any location gets marked for all the wheels, then a light can be shone through the entire system. Otherwise, no light can be shone through all the wheels.

``````#include <stdio.h>
#include <assert.h>
#include <string.h>

int speed;      /* speed of each wheel */
int wedgest; /* start of each wedge (-1 == no wedge) */
int wedglen; /* length of each wedge */

int pos;        /* angular position of each wheel */
int t;             /* time (in seconds) since start */

/* (light[deg] >> wid) & 0x1 is true if and only if there
is a wedge in wheel wid that a light can shine through at
angle deg */
int light;

/* mark all the degrees we can see through wheel w */
void mark_light(int w)
{
int lv, lv2; /* loop variables */
int wpos; /* wedge position */

for (lv = 0; lv < 5; lv++)
{
if (wedglen[w][lv] < 0) /* no more wedges for this wheel */
break;

/* start of wedge */
wpos = (pos[w] + wedgest[w][lv]) % 360;

for (lv2 = 0; lv2 <= wedglen[w][lv]; lv2++)
{ /* throughout extent of wedge */
light[wpos] |= (1 << w); /* mark as hole in wheel */
wpos = (wpos + 1) % 360; /* go to the next degree */
}
}
}

int main(int argc, char **argv)
{
FILE *fp;
FILE *fout;
int w, f;
int lv, lv2;

fp = fopen("spin.in", "r");
fout = fopen("spin.out", "w");
assert(fp);
assert(fout);

/* read in the data */
for (lv = 0; lv < 5; lv++)
{
fscanf (fp, "%d %d", &speed[lv], &w);
for (lv2 = 0; lv2 < w; lv2++)
fscanf (fp, "%d %d", &wedgest[lv][lv2], &wedglen[lv][lv2]);

/* mark the rest of the wedges as not existing for this wheel */
for (; lv2 < 5; lv2++)
wedglen[lv][lv2] = -1;
}

f = 0;
while (t < 360) /* for each time step */
{
memset(light, 0, sizeof(light));

/* mark the degrees we can see through each wheel */
for (lv = 0; lv < 5; lv++)
mark_light(lv);

for (lv = 0; lv < 360; lv++)
if (light[lv] == 31) /* we can shine a light through all five wheels */
f = 1;

if (f) break; /* we found a match! */

/* make a time step */
t++;
for (lv = 0; lv < 5; lv++)
pos[lv] = (pos[lv] + speed[lv]) % 360;
}

/* after 360 time steps, all the wheels have returned to their
original location */
if (t >= 360) fprintf (fout, "none\n");
else fprintf (fout, "%i\n", t);

return 0;
}
``````