GCJ 2016 Qualification Round A: Counting Sheep

解法

愚直

コード

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

ll solve(ll M) {
  vector<bool> check(10, false);
  for (int i = 1; i < 100000000; ++i) {
    ll N = M * i;
    while (N > 0) {
      int digit = N - N / 10 * 10;
      check[digit] = true;
      N /= 10;
    }
    for (int j = 0; j < check.size(); ++j) {
      if (!check[j]) break;
      if (j == check.size() - 1) return M * i;
    }
  }
  return -1;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int T;
  cin >> T;
  for (int i = 0; i < T; ++i) {
    int N;
    cin >> N;
    cout << "Case #" << (i + 1) << ": ";
    if (N == 0) {
      cout << "INSOMNIA";
    } else {
      ll ans = solve(N);
      if (ans == -1) {
        cout << "INSOMNIA";
      } else {
        cout << ans;
      }
    }
    cout << endl;
  }
}