UVa : 12290 (Counting Game)



// http://uva.onlinejudge.org/external/122/12290.html
// Tag: Adhoc
// Runtime: 0.044s

#include <cstdio>
#include <cstring>
#include <set>
using namespace std;

#define N 100000
#define LL long long

int main ()
{
    char a [10];
    set <int> s;

    for ( int i = 7; i < N; i++ ) {
        sprintf (a, "%d", i);
        if ( strchr (a, '7') ) s.insert (i);
        if ( i % 7 == 0 ) s.insert (i);
    }

    // printf ("%d", s.size ());

    int n, m, k;

    while ( scanf ("%d %d %d", &n, &m, &k) ) {
        if ( n == 0 && m == 0 && k == 0 ) break;
        int person = 1;
        int action = 1;
        bool increase = true;
        set <int>::iterator it = s.begin ();

        while ( true ) {
            if ( *it == action ) {
                it++;
                if ( person == m ) k--;
                if ( !k ) { printf ("%d\n", action); break; }
            }

            action++;
            if ( person == n ) increase = false;
            else if ( person == 1 ) increase = true;
            if ( increase ) person++;
            else person--;
        }
    }

    return 0;
}
Advertisements

2 thoughts on “UVa : 12290 (Counting Game)

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