题目描述
Find the smallest possible sum of the digits in the decimal notation of a positive multiple of K.
Constraints
2≤K≤105
K is an integer.
Constraints
2≤K≤105
K is an integer.
输入
Input is given from Standard Input in the following format:
K
K
输出
Print the smallest possible sum of the digits in the decimal notation of a positive multiple of K.
样例输入
6
样例输出
3
提示
12=6×2 yields the smallest sum.
#include<bits/stdc++.h> #define ll long long #define inf 0x3f3f3f3f using namespace std; const int N=3e5+50; const int p=1e9+7; int k,cnt; int dis[N],last[N]; bool vis[N]; struct orz{ int v,nex,s;}e[N*5]; queue<int>q; void add(int x,int y,int z) { cnt++; e[cnt].v=y; e[cnt].nex=last[x]; last[x]=cnt; e[cnt].s=z; } void spfa() { for (int i=0;i<k;i++) dis[i]=inf; dis[1]=0; vis[1]=1; q.push(1); while (!q.empty()) { int now=q.front(); q.pop(); for (int i=last[now];i;i=e[i].nex) { if (dis[e[i].v]>dis[now]+e[i].s) { dis[e[i].v]=dis[now]+e[i].s; if (!vis[e[i].v]) { vis[e[i].v]=1; q.push(e[i].v); } } } vis[now]=0; } } int main() { scanf("%d",&k); for (int i=0;i<k;i++) { add(i,(i+1)%k,1); add(i,i*10%k,0); } spfa(); printf("%d ",dis[0]+1); return 0; }