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 (GCC 5.4.1) (judge verdict: Accepted)


#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
typedef struct pair<int,int> P;
typedef struct pair<int,P> P2;

vector<ll> v[28];
vector<ll> v2;

int dp[100001]={};
const ll INF=1000000000000000001;
int main() {

	int k;
	cin>>k;
	for(int i=0;i<k+1;++i)dp[i]=10000000;
	queue<P> que;
	que.push(P(1,1));
	while(!que.empty()){
		P p1=que.front();que.pop();
		int cost=p1.first;
		int pos=p1.second;
		if(dp[pos]>cost){
			dp[pos]=cost;
			int np=(pos*10)%k;
			que.push(P(cost,np));
			np=(pos*10+1)%k;
			que.push(P(cost+1,np));
		}
	}
	cout<<dp[0]<<endl;

	return 0;
}