#include <iostream>
#include <cstring>
#include <cstdlib>
#include <map>
void getdigits(char buf[], int n)
{
while (n) {
*buf++ = n % 10;
n /= 10;
}
}
int compint(const void *a, const void *b)
{
return *(const char *)a - *(const char *)b;
}
int is_vampire(char n[], const char i[], const char j[], size_t l)
{
char k[2 * l];
std::memcpy(k + 0, i, l);
std::memcpy(k + l, j, l);
std::qsort(k, 2 * l, 1, compint);
std::qsort(n, 2 * l, 1, compint);
return !std::memcmp(n, k, sizeof(k));
}
int main()
{
std::map<int, std::pair<int, int> > m;
for (int i = 100; i < 1000; i++) {
for (int j = 100; j < 1000; j++) {
int n = i * j;
if (n < 100000 || n >= 1000000)
continue;
char ndigits[6];
getdigits(ndigits, n);
char idigits[3];
char jdigits[3];
getdigits(idigits, i);
getdigits(jdigits, j);
if ((i % 10 || j % 10) && is_vampire(ndigits, idigits, jdigits, 3)) {
m[n] = std::make_pair(i, j);
}
}
}
std::cout << m.size() << " vampire numbers found:" << std::endl;
for (std::map<int, std::pair<int, int> >::iterator it = m.begin(); it != m.end(); it++)
std::cout << it->first << " = " << it->second.first << " * " << it->second.second << std::endl;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8Y3N0cmluZz4KI2luY2x1ZGUgPGNzdGRsaWI+CiNpbmNsdWRlIDxtYXA+CgoKdm9pZCBnZXRkaWdpdHMoY2hhciBidWZbXSwgaW50IG4pCnsKICAgIHdoaWxlIChuKSB7CgkJKmJ1ZisrID0gbiAlIDEwOwoJCW4gLz0gMTA7Cgl9Cn0KCmludCBjb21waW50KGNvbnN0IHZvaWQgKmEsIGNvbnN0IHZvaWQgKmIpCnsKCXJldHVybiAqKGNvbnN0IGNoYXIgKilhIC0gKihjb25zdCBjaGFyICopYjsKfQoKaW50IGlzX3ZhbXBpcmUoY2hhciBuW10sIGNvbnN0IGNoYXIgaVtdLCBjb25zdCBjaGFyIGpbXSwgc2l6ZV90IGwpCnsKCWNoYXIga1syICogbF07CglzdGQ6Om1lbW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