Atcoder arc 084 D

来源:互联网 发布:分布式系统 云计算 编辑:程序博客网 时间:2024/06/01 21:25

题目链接:http://arc084.contest.atcoder.jp/tasks/arc084_b


D - Small Multiple


Time limit : 2sec / Memory limit : 256MB

Score : 700 points

Problem Statement

Find the smallest possible sum of the digits in the decimal notation of a positive multiple of K.

Constraints

  • 2K105
  • 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
36

解析:原来用暴力一直WA,比赛结束后看别人的代码,才知道可以用到抽屉原理,还是第一次接触双向队列(既可以当栈又可以当队列)

代码:

#include<stdio.h>#include<algorithm>#include<string.h>#include<queue>using namespace std; int vis[100009];typedef pair<int, int> P;  int main(){    int k;    scanf("%d", &k);    deque<P> q;    memset(vis, 0, sizeof(vis));    q.push_back(make_pair(1, 1));    while(!q.empty())    {        P x = q.front(); q.pop_front();        if(vis[x.first]) continue;        vis[x.first] = 1;        if(x.first == 0)        {            printf("%d\n", x.second);            break;        }        q.push_front(make_pair(x.first*10 % k, x.second));        q.push_back(make_pair((x.first+1) % k, x.second+1));     }    return 0;}



原创粉丝点击