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

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define ll long long
#define rep(i,l,r)for(ll i=(l);i<(r);i++)
#define START clock_t start=clock()
#define TIME printf("\n%.3f秒\n",(double)(clock()-start)/CLOCKS_PER_SEC)

ll pom(ll a,ll n,int m){ll x=1;for(a%=m;n;n/=2)n&1?x=x*a%m:0,a=a*a%m;return x;}
#define invp(a,p)pom(a,p-2,p)

#define MOD (7*(1<<26)+1)
#define SIZE (1<<18)

//非再帰fft
void fft(ll*f,int n,ll g){
	ll yy[SIZE];
	ll*x=f,*y=yy,*temp;
	for(int t=1;n>>t;t++){
		ll s=1;
		ll gg=pom(g,n>>t,MOD);
		rep(k,0,1<<(t-1)){
			rep(j,0,n>>t){
				y[(j<<t)+k           ]= (x[(j<<(t-1))+k]+s*x[(j<<(t-1))+n/2+k])%MOD;
				y[(j<<t)+k+(1<<(t-1))]=((x[(j<<(t-1))+k]-s*x[(j<<(t-1))+n/2+k])%MOD+MOD)%MOD;
			}
			s=s*gg%MOD;
		}
		temp=x;x=y;y=temp;
	}
	if(x!=f)rep(i,0,n)f[i]=x[i];
}




ll check[SIZE];
ll dp[SIZE];
ll n;

int main(){
//	START;
	scanf("%lld",&n);
	
	ll r=1;
	while(!check[r]){
		check[r]=1;
		r=r*10%n;
	}
	
	if(check[0]){
		puts("1");
		return 0;
	}
	
	//1個でできるものがcheck
	//答えは45以下なのでfftすれば解ける(TLE)
	ll g=pom(3,(MOD-1)/SIZE,MOD);
	ll ig=invp(g,MOD);
	rep(i,0,SIZE)dp[i]=check[i];
	fft(check,SIZE,g);

	for(ll cnt=2;cnt<=18;cnt++){
		//fft
		fft(dp,SIZE,g);
		//掛け算
		rep(i,0,SIZE)dp[i]=dp[i]*check[i]%MOD;
		//ifft
		fft(dp,SIZE,ig);
		//次数下げ
		rep(i,0,n)dp[i]=!!((dp[i]+dp[i+n])%MOD),dp[i+n]=0;
		//チェック
		if(dp[0]){
			printf("%lld\n",cnt);
//			TIME;
			return 0;
		}
	}
	
	ll ans=0;
	while(n){
		ans+=n%10;
		n/=10;
	}
	printf("%lld\n",ans);
	return 0;
//	TIME;
}