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 (Clang 3.8.0) (judge verdict: Wrong Answer)

#include <stdio.h>
#include <memory.h>
typedef long long ll;

#define mx 100001
#define MX 125

int n,dp[MX+1][mx][2];

int min(int a,int b){
    if(a>b) return b;
    return a;
}

int fn(int pos,int rem,int fl){
    if(rem==0 && fl){
        return 0;
    }
    if(pos==MX) return mx;
    int ans = dp[pos][rem][fl];
    if(ans!=-1) return ans;
    ans = mx;

    for(int i = 0; i<=9; i++){
        int F = fl;
        if(i)   F = 1;
        if(fl==0 && i==0)   continue;
        ans = min(ans, i + fn(pos+1,(rem*10 + i)%n,F ) );
    }
    return dp[pos][rem][fl] = ans;
}


main(){
//read;
//IO;
scanf("%d",&n);
int p = n,q = 0;
while(p){
    q+=p%10;
    p/=10;
}
if(p==1){
    puts("1");
    return 0;
}
memset(dp,-1,sizeof dp);
int ans = fn(0,0,0);
printf("%d",ans);


return 0;
}