USACO: Runaround Numbers



/*
ID: tausiq11
PROG: runround
LANG: C++
 */

// @BEGIN_OF_SOURCE_CODE

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <bitset>
#include <utility>
#include <set>
#include <numeric>
#include <ctime>

#define Inf 2147483647
#define Pi acos(-1.0)
#define N 1000000
#define LL long long

inline LL Power(int b, int p) { LL ret = 1; for ( int i = 1; i <= p; i++ ) ret *= b; return ret; }
const int dr [] = {-1, -1, 0, 1, 1, 1, 0, -1};
const int dc [] = {0, 1, 1, 1, 0, -1, -1, -1};

#define F(i, a, b) for( int i = (a); i < (b); i++ )
#define Fs(i, sz) for( size_t i = 0; i < sz.size (); i++ )
#define Fe(i, x) for(typeof (x.begin()) i = x.begin(); i != x.end (); i++)
#define Set(a, s) memset(a, s, sizeof (a))
#define max(a, b)  (a < b ? b : a)
#define min(a, b)  (a > b ? b : a)

using namespace std;

bool isRunRound (int num) {
	char str [9 + 3];
	sprintf (str, "%d", num);

	int frequency [10];
	Set(frequency, 0);	// all digits (0 to 9) frequency 0

	int len = strlen(str);
	int currNumber = str [0] - '0';
	int currIndex = 0;

	for ( int i = 0; i < len; i++ ) {
		currIndex = (currIndex + currNumber) % len;
		currNumber = str [currIndex] - '0';
		frequency [currNumber]++;

		// checking if currNumber = 0 or currNumber frequency greater than 1
		if(!currNumber || frequency [currNumber] > 1) return false;
	}

	return true;
}

int main () {

	freopen ("runround.in", "r", stdin);
	freopen ("runround.out", "w", stdout);

	int M; scanf ("%d", &M);

	while ( true ) {
		if ( isRunRound(++M) ) { 
			printf ("%d\n", M); 
			break; 
		} 
	}

	return 0;
}

// @END_OF_SOURCE_CODE

USACO Training Grader Results


USER: Shadow King [tausiq11]
TASK: runround
LANG: C++

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.000 secs, 3232 KB]
   Test 2: TEST OK [0.011 secs, 3232 KB]
   Test 3: TEST OK [0.000 secs, 3232 KB]
   Test 4: TEST OK [0.000 secs, 3232 KB]
   Test 5: TEST OK [0.119 secs, 3232 KB]
   Test 6: TEST OK [0.065 secs, 3232 KB]
   Test 7: TEST OK [0.205 secs, 3232 KB]

All tests OK.
YOUR PROGRAM ('runround') WORKED FIRST TIME!  That's fantastic
-- and a rare thing.  Please accept these special automated
congratulations.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s