Problem:
Score : 700 points

Problem Statement

Find the smallest possible sum of the digits in the decimal notation of a positive multiple of K.

Constraints

2 \leq K \leq 10^5

K is an integer.

Input

Input is given from Standard Input in the following format:

K

Output

Print the smallest possible sum of the digits in the decimal notation of a positive multiple of K.

Sample Input 1

6

Sample Output 1

3

12=6×2 yields the smallest sum.

Sample Input 2

41

Sample Output 2

5

11111=41×271 yields the smallest sum.

Sample Input 3

79992

Sample Output 3

36

Solution in C++14 (Clang 3.8.0) (judge verdict: Wrong Answer)

#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <algorithm>
#include <set>
#include <map>
#include <bitset>
#include <cmath>
#include <functional>
#include <iomanip>
#define vll vector<ll>
#define vvv vector<vvl>
#define vvi vector<vector<int> >
#define vvl vector<vector<ll> >
#define vv(a, b, c, d) vector<vector<d> >(a, vector<d>(b, c))
#define vvvl(a, b, c, d) vector<vvl>(a, vvl(b, vll (c, d)));
#define rep(c, a, b) for(ll c=a;c<b;c++)
#define re(c, b) for(ll c=0;c<b;c++)
#define all(obj) (obj).begin(), (obj).end()
typedef long long int ll;
typedef long double ld;
using namespace std;

int main(int argc, char const *argv[]) {
  ll n;std::cin >> n;
  ll keta=40, score = 46;
  vll z(30, 1);rep(i, 1, 30) z[i] = (z[i-1]*10)%n;
  vector<vector<bitset<100001>>> dp(keta, vector<bitset<100001>>(score, 0));
  dp[0][0][0] = 1;
  bitset<100001> mask;for(int i=0;i<n;i++) mask[i] = 1;
  for(int i=1;i<keta;i++){
    for(int j=0;j<score;j++){
      for(int k=0;k<10;k++){
        if(j<k) continue;
        ll t = (k * z[i-1])%n;
        dp[i][j] = (dp[i][j] |(((dp[i-1][j-k]>>(n-t)|(dp[i-1][j-k]<<t)))&mask));
      }
    }
  }
  ll ans = 100;
  for(int i=1;i<keta;i++){
    for(int j=1;j<score;j++){
      if(dp[i][j][0]) ans = min(ans, (ll)j);
    }
  }
  std::cout << ans << '\n';
  return 0;
}