USACO 2.2.3 – Runaround Numbers

#include <cstdio>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
typedef unsigned long long int LL;
using namespace std;

int main() {
	LL M, v, i, d, l, n;
	freopen("runround.in", "r", stdin), freopen("runround.out", "w", stdout);
	cin >> M;
	for (LL m = M + 1;; m++) {
		for (i = 0, v = 0, n = m; n; v |= (1 << (n % 10)), n /= 10, i++)
			if (!(n % 10) || (v & (1 << (n % 10))))
				break;
		if (!(v = 0) && n) continue;

		stringstream ss; ss << m ; string s = ss.str();

		for (d = (s[0] - '0') % s.size(), l = 0; l <= i; l++, v |= (1 << (s[d] - '0')), d = (d + (s[d] - '0')) % s.size())
			if (v & (1 << (s[d] - '0')))
				break;

		if (l >= i) {
			cout << m << endl;
			break;
		}
	}
	return 0;
}
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

%d bloggers like this: