[atARC084D]Small Multiple
构造一张图:$\forall x$,向$10x$连一条边权为0的边,向$x+1$连1条边权为1的边,那么0到$i$的代价即为$i$各位数字之和
考虑到我们只关心于当前点的两个特征:1.模$n$的余数(判定是否是$n$的倍数);2.所对应的出边,那么根据模运算的性质,可以将$x\equiv y(mod\ n)$缩为一个点,即求0到0最小的环
然后我们可以枚举最后一个点,因此即求0到任意点的最短路
可以使用Dijkstra求,也可以用01bfs,即bfs时维护双向队列,将0边拓展的加到队首,将1边拓展的加到队尾,正确性只需要归纳队列中至多仅有两种距离的点即可

1 #include<bits/stdc++.h>
2 using namespace std;
3 #define N 100005
4 deque<int>q;
5 int n,ans,d[N],vis[N];
6 int main(){
7 scanf("%d",&n);
8 memset(d,0x3f,sizeof(d));
9 q.push_front(0);
10 d[0]=0;
11 while (!q.empty()){
12 int k=q.front();
13 q.pop_front();
14 if (vis[k])continue;
15 vis[k]=1;
16 if (d[10*k%n]>d[k]){
17 d[10*k%n]=d[k];
18 q.push_front(10*k%n);
19 }
20 if (d[(k+1)%n]>d[k]+1){
21 d[(k+1)%n]=d[k]+1;
22 q.push_back((k+1)%n);
23 }
24 }
25 ans=d[n-1]+1;
26 for(int i=1;i<n;i++)
27 if (10*i%n==0)ans=min(ans,d[i]);
28 printf("%d",ans);
29 }
[atARC084D]Small Multiple的更多相关文章
- Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define ...
Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define ... 这个错误是因为有两个相 ...
- POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理
Find a multiple Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7192 Accepted: 3138 ...
- [LeetCode] Read N Characters Given Read4 II - Call multiple times 用Read4来读取N个字符之二 - 多次调用
The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the actu ...
- SharePoint "System.Data.SqlClient.SqlException (0x80131904): Parameter '@someColumn' was supplied multiple times.“
最近在处理SharePoint Office365的相关开发的时候发现了这样一个奇怪的现象: 无法通过API更新Editor field,只要已更新就会throw Exception,由于是Offic ...
- 2012Chhengdu K - Yet Another Multiple Problem
K - Yet Another Multiple Problem Time Limit:20000MS Memory Limit:65536KB 64bit IO Format:%I6 ...
- JAVA实现AES 解密报错Input length must be multiple of 16 when decrypting with padded cipher
加密代码 /**解密 * @param content 待解密内容 * @param password 解密密钥 * @return */ public static byte[] decrypt(b ...
- scala - multiple overloaded alternatives of method bar define default arguments
同名同位置默认参数不能overload def bar(i:Int,s:String="a"){} def bar(i:String,s:String="b") ...
- 基于Picture Library创建的图片文档库中的上传多个文件功能(upload multiple files)报错怎么解决?
复现过程 首先,我创建了一个基于Picture Library的图片文档库,名字是 Pic Lib 创建完毕后,我点击它的Upload 下拉菜单,点击Upload Picture按钮 在弹出的对话框中 ...
- 多文档上传(upload multiple documents)功能不能使用怎么办?
问题描述: 在SharePoint 2010的文档库里选择documents标签,然后选择upload document下拉菜单,你会发现upload multiple documents那个按钮是灰 ...
随机推荐
- Linux Bash命令杂记(tr col join paste expand)
Linux Bash命令杂记(tr col join paste expand) tr命令 tr命令可以将输入的数据中的某些字符做替换或者是作删除 tr [-ds] STR d: 删除输入数据的中的S ...
- sqlite3 c++使用以及提高速率(一万条每秒左右)
参考来源: sqlite3的C语言使用(三):https://www.leavesongs.com/C/sqlite3_3.html sqlite插入和查询效率提高方法及测试结果: http://bl ...
- Java标识符和关键字的区别!java基础 java必学
任何计算机语言都离不开标识符和关键字,那我们就来简单讲一下他们两者的区别,希望有助于大家的的理解!本篇文章干货满满,如果你觉得难懂的话可以看下高淇老师讲的Java300集的教学视频,分选集,深度剖析了 ...
- FFT&原根&NTT&MTT
FFT bilibili 3b1b视频讲解 核心过程: 原根 Definition 若 \(a\) 模 \(m\) 的阶等于 \(\varphi(m)\),则称 \(a\) 为模 \(m\) 的一个原 ...
- props&attrs provide inject
defineComponent({ props: {// 1 } setup (props, {attrs, emit}) { } }) 一,组件传值: 父传子: 1.如果没有在定义的props中声明 ...
- Java:volatile笔记
Java:volatile笔记 本笔记是根据bilibili上 尚硅谷 的课程 Java大厂面试题第二季 而做的笔记 1. volatile 和 JMM 内存模型的可见性 JUC 下的三个包 java ...
- mybatis学习笔记(1)基本环境
1.pom引入 <dependencies> <dependency> <groupId>org.mybatis</groupId> <artif ...
- [调试笔记] 10.8模拟赛11 T4 甜圈
这题正解线段树维护哈希,同机房神犇已经讲的很明白了.这里只说sbwzx在调试的时候犯的sb错误. 1.关于pushdown和update 众所周知,sbwzx一写带lazy的线段树,就必在pushdo ...
- TVS管相关知识
在设计中,使用到了TVS管,在之前的设计中没有特别关注TVS管.今天查了一些资料,算是简单的有个了解. TVS管是一种保护器件.它的英文全称为 transient voltage suppressor ...
- 单片机STM32开发中常用库函数分析
1.GPIO初始化函数 用法: voidGPIO_Configuration(void) { GPIO_InitTypeDefGPIO_InitStructure;//GPIO状态恢复默认参数 GPI ...