Problem Description

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.

Example

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,求这个数的倍数,使得其各位数的和最小,并输出这个和

思路:bfs

设答案为 x,由于要求 x 的各位数之和最小,那么可以从 1 开始,当搜索到的第一个值 %k 为 0 即为答案

那么有 2 种入队方式:

  • x+1:此时各位数字和比之前的多了一个 1,那么代价是 1
  • x*10:此时各位数字和比之前没有改变,那么代价就是 0

因此问题转换为用 bfs 找到满足条件的 x,由于要找各位和最小的,也就是说代价为 0 的首先出队,因此可以使用双端队列,将代价为 0 的放在队首,代价为 1 的放在队尾

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD = 1E9+7;
const int N = 100000+5;
const int dx[] = {-1,1,0,0,-1,-1,1,1};
const int dy[] = {0,0,-1,1,-1,1,-1,1};
using namespace std;
int vis[N];
int main() {int n;scanf("%d",&n);deque<pair<int,int> > Q;Q.push_front(make_pair(1,1));while(!Q.empty()){pair<int,int> temp=Q.front();Q.pop_front();if(!vis[temp.first]){vis[temp.first]=true;if(temp.first==0){printf("%d\n",temp.second);break;}Q.push_front(make_pair((temp.first*10)%n,temp.second));Q.push_back(make_pair((temp.first+1)%n,temp.second+1));}}return 0;
}

Small Multiple(AtCoder-3621)相关推荐

  1. POJ 2356 Find a multiple (抽屉原理)

    感觉这个题很不错,至少开始真的没想道可以用抽屉原理推出一个结论,然后把这题秒掉.... 已知有n个元素,sum[i]表示从1到i所有数的和...sum[i]%n可以得到一个剩余系,如果出现0,那么结果 ...

  2. 杭电1019 Least Common Multiple(已解决)

    以下代码不知道错哪了? #include <cstdlib> #include <iostream>using namespace std;int main(int argc, ...

  3. 愤怒的小鸟游戏开发教程(持续更新)

    @elevateSober 1.项目实施资源 1.1学习视频推荐(包含项目所需资源) http://www.sikiedu.com/course/134 2.项目实施过程 2.1工程的建立 新建一个2 ...

  4. AtCoder Beginner Contest 198 (A ~ F)题解

    目录 A. Div B. Palindrome with leading zeros C. Compass Walking D. Send More Money E. Unique Color F. ...

  5. Find The Multiple POJ - 1426(只包含01的十进制倍数(同余模定理))

    Find The Multiple POJ - 1426 为了避免各位没心情看我的TLE 的过程,直接告诉你,最后两个代码是AC代码 bfs一位一位枚举,知道会爆LONG LONG 还是怀着侥幸心理试 ...

  6. pandas使用groupby函数按照多个分组变量进行分组聚合统计、使用agg函数计算分组的多个统计指标(grouping by multiple columns in dataframe)

    pandas使用groupby函数按照多个分组变量进行分组聚合统计.使用agg函数计算分组的多个统计指标(grouping by multiple columns in dataframe) 目录

  7. R语言基于多字段(多数据列、multiple columns)对dataframe的行数据进行排序(Ordering rows)实战:使用R原生方法、data.table、dplyr等方案

    R语言基于多字段(多数据列.multiple columns)对dataframe的行数据进行排序(Ordering rows)实战:使用R原生方法.data.table.dplyr等方案 目录

  8. UPC个人训练赛第十五场(AtCoder Grand Contest 031)

    传送门: [1]:AtCoder [2]:UPC比赛场 [3]:UPC补题场 参考资料 [1]:https://www.cnblogs.com/QLU-ACM/p/11191644.html B.Re ...

  9. Mynavi Programming Contest 2021(AtCoder Beginner Contest 201)题解

    文章目录 A - Tiny Arithmetic Sequence B - Do you know the second highest mountain? C - Secret Number D - ...

  10. KYOCERA Programming Contest 2021(AtCoder Beginner Contest 200)题解

    文章目录 A - Century B - 200th ABC-200 C - Ringo's Favorite Numbers 2 D - Happy Birthday! 2 E - Patisser ...

最新文章

  1. 在Ubuntu 14.04 64bit上使用dig
  2. 停电后程序员怎么写代码 | 每日趣闻
  3. JZOJ 5697. 【gdoi2018 day1】农场(farm)
  4. Android EditText的设置
  5. android:persistent (非系统app失效)
  6. JQuery Datatables 实现对某一列的数据合计汇总
  7. 渗透攻击红队百科全书
  8. [论文阅读] End-to-End Incremental Learning
  9. 各类曲线的参数方程_三、常见曲线的参数方程.ppt
  10. 计算机英语词汇 沪江,常用计算机英语词汇学习
  11. 如何用excel做正交分析_excel表格分析正交数据处理-excel中怎么对正交试验进行F值检验...
  12. 儿童编程软件python-一款儿童编程入门的理想工具——PythonTurtle
  13. Three 之 three.js (webgl)shader 中 Texture 贴图 uv 坐标的相关简单说明,并简单测试 UV 重复旋转偏移效果
  14. 流水线计算公式及吞吐量
  15. 开发3D游戏建模都需要哪些软件?软件繁多,如何从中挑选学习?
  16. 【无标题】水泥稳定层施工
  17. 基于阻抗控制的工业机器人轨迹跟踪系统 Simulink/Simscape 仿真
  18. 从什么地方获取大数据_在哪里找大数据
  19. 【关于为什么要刷力扣的思考】记第二次周赛AK
  20. OpenStack挂载ISO,安装原版windows

热门文章

  1. .NET 简单的smtp协议发邮件
  2. csdn的blog后台程序的导航菜单的实现
  3. 干货:4个小技巧助你搞定缺失、混乱的数据(附实例代码)
  4. 为什么“消费降级”突然火了?数字基尼系数给你一点理论支撑
  5. 秦始皇:请叫我数据治理专家
  6. 信号处理与信号产生电路
  7. 垃圾oracle_第 14 章 垃圾回收概述
  8. 五分钟,手撸一个Spring容器!
  9. 从崩溃的选课系统,论为什么更安全的 HTTPS 协议没有被全面采用
  10. 卖身字节跳动的互动百科或被改名