usaco runround

1663 ワード

純粋なシミュレーション問題です.
/*
ID: stormdp1
LANG: C++
TASK: runround
*/
#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

bool judge[10];
int digit[20];
long long n;
int len;

bool isOk()
{
    memset(judge, false, sizeof(judge));
    int times = digit[len-1];
    int done = 1;
    int place = len - 1;

    judge[times] = true;
    while(done <= len) {
        place = (place - times + 10 * len) % len;   //   len  10,  len             
        times = digit[place];
        done++;
        judge[times] = true;
    }
    
    for(int i = 0; i < len - 1; i++) {
        if(!judge[digit[i]])
            return false;
    }
    if(place != len - 1)
        return false;
    return true;
}

int main()
{
    freopen("runround.in", "rb", stdin);
    freopen("runround.out", "wb", stdout);    
    scanf("%lld", &n);
    for(long long t = n + 1; ; t++) {
        bool flag = true;
        long long tmp = t;
        len = 0;
        memset(judge, false, sizeof(judge));
        while(tmp) {      //  0     
            int a = tmp % 10;
            if(a == 0) {
                flag = false;
                break;
            }
            if(judge[a]) {
                flag = false;
                break;
            }
            else {
                judge[a] = true;
            }
            digit[len] = a;     //       
            len++;
            tmp /= 10;
        }
        if(flag && isOk()) {
            printf("%lld
", t); return 0; } } fclose(stdin); fclose(stdout); }