// Solution Author: Ivan Kazmenko (gassa@mail.ru) // Date: 29.04.2018 module solution; import std.algorithm, std.array, std.conv, std.range, std.stdio, std.string; immutable int base = 10; immutable int [] goodList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 89, 409, 449, 499, 6469]; auto divisors (int x) { return iota (1, x + 1).count !(d => x % d == 0); } void main () { int n; while (readf (" %s", &n) > 0) { auto counts = new int [base]; readln; readln.splitter.map !(to !(int)).each !(d => counts[d] += 1); auto isValid (int x) { auto left = counts.dup; return x.text.map !(c => c - '0').all !(d => (--left[d]) >= 0); } auto res = goodList.filter !(isValid).minElement !(divisors); res.divisors.writeln; res.writeln; } }