AtCoder Regular Contest 084 (upc 6616) Small Multiple

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

Copy

6

Sample Output 1

Copy

3

12=6×2 yields the smallest sum.


Sample Input 2

Copy

41

Sample Output 2

Copy

5

11111=41×271 yields the smallest sum.


Sample Input 3

Copy

79992

Sample Output 3

Copy

题目:找一个k的倍数而且这个数的所有数的和是他的倍数里面的最小的。

思维:就是你XJB想想对于一个数来说,你可以考虑0-K-1最为一个数的开头,如果是选择K开头的话而且他是一个数的倍数的话,那么你肯定可以缩减成0开头的一个数同理k+1可以变成1 这样XJB乱想,就变成了0 - k-1开头的数通过+1 *102各种操作来求最小的一个位数和。(我不知道为啥跟SB一样纠结10%p 跟100%p为啥不相等)。然后就bfs贪心呗,最后别忘了1是从0过来的所以需要+1.

#include<bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
int n,cnt; 
struct node{
    int u;
    int v;
    int valu;
    int next;
}no[1000005];
int dist[100005];
int head[100005];
void add(int u,int v,int valu)
{
    no[cnt].u=u;
    no[cnt].v=v;
    no[cnt].valu=valu;
    no[cnt].next=head[u];
    head[u]=cnt++;
}
void bfs()
{
    memset(dist,INF,sizeof(dist));
    dist[1]=0;
    queue<int>q;
    q.push(1);
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        for(int i=head[u];i!=-1;i=no[i].next)
        {
            int v=no[i].v;
            if(dist[v] > dist[u] + no[i].valu)
            {
                dist[v]=dist[u] + no[i].valu;
                q.push(v);
            }
        }
    }
    dist[0]=dist[0]+1;//0 - 1 需要加 1  
    printf("%d\n",dist[0]);
}
int main()
{
    memset(head,-1,sizeof(head));
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        add(i,(i+1)%n,1);//+1
        add(i,(i*10)%n,0);//乘以10 
    }
    bfs();
    return 0; 
}

猜你喜欢

转载自blog.csdn.net/passer__/article/details/81394248