Small Multiple

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.

题意:给你一个数n,找到n的倍数各个位上加起来的最小值。

思路:

因为是n的倍数,对与数x,只需要取x属于[1,k]之间,在数后加0~9来构成一个新数y,保证y%k==0,即是各个

位数取和的最小的k的倍数,为了保证不必要的位数加进来,导致结果不是最优,所以采用取模处理,所

以只需要看对k取模的余数即可,

在每一个数之间建立边权,每个节点代表该数对k取模的结果,两节点之间的边权代表各个位数和的差值

x  -----x+1    各个位数的差值之和相差为1,所以边权为1;

x------x*10  各个位数的和加个0之后,其值不变,所以边权为0;

然后进行最短路,从取模为1,的节点,到取模为0 的节点的最短路的值,及时所求答案

/*Du Jinzhi*/
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define pi acos(-1.0)
#define inf 0x3f3f3f3f
#define ll long long
#define linf 0x3f3f3f3f3f3f3f3fLL
using namespace std;
const int N = 1e5+5;
const ll mod = 1e9+7;
const ll INF = 1e18;
int head[N];
int ne;
struct edge{
    int to,nt,w;
}e[500004];
queueq;
int dis[100004];
void spfa()
{
    memset(dis,127/3,sizeof(dis));
    dis[1]=0;
    while(!q.empty()){
        int x=q.front();q.pop();
        for(int i=head[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);
            }
        }
    }
}
void add(int u,int v,int w){
     e[++ne].to = v;e[ne].nt = head[u];
     e[ne].w=w;
     head[u]=ne;
}

int main()
{
    int n;
    scanf("%d",&n);
    for(int i=0;i

 

你可能感兴趣的:(Small Multiple)