/* http://projecteuler.net/
 *
 * Problem 38
 * What is the largest 1 to 9 pandigital 9-digit number that can be
 * formed as the concatenated product of an integer with (1,2, ... , n)
 * where n > 1?
 *
 * Solution by Melkor (Filip Niksic, fniksic@gmail.com)
 *
 **/

#include <iostream>
#include <bitset>

using namespace std;

// expects n to be a 9-digit number
bool pandigital(long long n) {
    bitset<9> digits;
    while (n) {
	if (n % 10 == 0 || digits[n % 10 - 1])
	    return false;
	digits.set(n % 10 - 1);
	n /= 10;
    }
    return true;
}
	
int main() {
    /* Given that the concatenated product of 9 with (1, 2, 3, 4, 5)
     * is 918273645, it's obvious that we only need to check integers
     * which begin with 9. Considering this, we can rule out 2-digit
     * and 3-digit integers, since they cannot generate a 9-digit
     * concatenated product. All that remains to check are integers
     * m such that 9000<=m<10000. (We could also limit ourselves to
     * integers with distinct digits, but that doesn't make much
     * difference in speed.) */

    long long m = 9999;
    for ( ; m >= 9000 && !pandigital(m * 100000 + 2 * m); --m);
    cout << m * 100000 + 2 * m << endl;

    return 0;
}

