6616: Small Multiple
时间限制: 1 Sec 内存限制: 512 MB
提交: 459 解决: 98
[提交] [状态] [讨论版] [命题人:admin]
题目描述
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.
输入
Input is given from Standard Input in the following format:
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.
来源/分类
思路:从0开始对当前数进行加一建边并将权值变成1,然后再乘十建边权值为0。然后最短路搜索从1-0
# include<bits/stdc++.h>
using namespace std;
const int INF=1<<30;
struct edge
{
int to,w,nxt;
};
edge e[200005]; //前向星建边
int head[100005],dis[100005];
int t,s,d,cnt,sta[100005],ed[100005];
void add(int a,int b,int c)
{
e[cnt].to=b;
e[cnt].w=c;
e[cnt].nxt=head[a];
head[a]=cnt++;
}
void spfa(int from)
{
fill(dis,dis+100004,INF);
dis[from]=0;
queue<int>q;
q.push(from);
while(!q.empty()){
int u=q.front();
q.pop();
for(int i=head[u];i!=-1;i=e[i].nxt){
if(dis[e[i].to]>dis[u]+e[i].w){
dis[e[i].to]=dis[u]+e[i].w;
q.push(e[i].to);
}
}
}
}
int main()
{
int i,j,a,b,c;
int k;
cin>>k;
cnt=0;
memset(head,-1,sizeof(head));
for(i=0;i<k;++i){
add(i,(i+1)%k,1);
add(i,(i*10)%k,0);
}
int ans=INF;
spfa(1);
printf("%d\n",dis[0]+1);
return 0;
}