[AtCoder Beginner Contest 077]D-Small Multiple

Time limit : 2sec
Memory limit : 256MB

Problem Statement

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

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

题意:给定数字k,求问:在十进制下,各个位数字的和最小的k*i是多少(i是正整数,k<=100000)

题解:题解太神了,先膜
从数字0开始,对于每一个数字x,向x+1连一条权值为1的单向边,向x*10连一条权值为0的边,如果x+1或者x *10大于k的话,那就对k取模。
神奇的结论,答案就是1+点1到点0的最短路。
贴题解以后慢慢看:https://img.atcoder.jp/arc084/editorial.pdf (page8)

#include<bits/stdc++.h>
#define LiangJiaJun main
using namespace std;
int k,ne,h[100004];
struct edge{
    int to,nt,w;
}e[500004];
queue<int>q;
int dis[100004];
void add(int u,int v,int w){
     e[++ne].to = v;e[ne].nt = h[u];
     e[ne].w=w;
     h[u]=ne;
}
int LiangJiaJun(){
    scanf("%d",&k);
    for(int i=0;i<k;i++){
        add(i,(i+1)%k,1);
        add(i,(i*10)%k,0);
    }
    q.push(1);
    memset(dis,127/3,sizeof(dis));
    dis[1]=0;
    while(!q.empty()){
        int x=q.front();q.pop();
        for(int i=h[x];i;i=e[i].nt){
            if( dis[e[i].to]>dis[x]+e[i].w){
                dis[e[i].to]=dis[x]+e[i].w;
                q.push(e[i].to);
            }
        }
    }
    printf("%d\n",dis[0]+1);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值