当前位置:网站首页>AtCoder Beginner Contest 077 D - Small Multiple
AtCoder Beginner Contest 077 D - Small Multiple
2022-08-10 12:05:00 【Snow_raw】
AtCoder Beginner Contest 077 D - Small Multiple
题意: 给定一个 K K K ,要求你从 K K K 的所有倍数中包括 K K K 自身,找到一个数,他的所有数位和是所有 K K K 的倍数中最小的。
思路: 该题本质是一个图论问题,我们需要建立起模型。首先观察他的性质,我们从位上来观察,发现无论 K K K 是什么,我们都可以从 1 1 1 开始通过 x + 1 x+1 x+1 或者 x ∗ 10 x*10 x∗10 两个操作使得 1 1 1 成功到达 K K K ,而一旦 x > = k x>=k x>=k 之后会发生什么呢?很容易又发现重新进入了从 1 1 1 到 K K K 的循环 。因此我们可以从 1 1 1 开始枚举到 K − 1 K-1 K−1 ,每个数可以建立两条边,分别是 i + 1 i+1 i+1 m o d mod mod K K K 权值为 1 1 1 和 i + 10 i+10 i+10 m o d mod mod K K K 权值为 0 0 0 。因此总边数最多为 2 K 2K 2K 条,最后跑一遍 1 1 1 ⇒ \Rightarrow ⇒ 0 0 0 的短路即可。
代码:
#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int,int>PII;
int n;
const int N = 1e5+10;
int h[N],e[N<<1],ne[N<<1],idx,w[N<<1];
int d[N];
bool st[N];
void add(int a,int b,int c){
e[idx]=b;
w[idx]=c;
ne[idx]=h[a];
h[a]=idx++;
}
int dijkstra(){
priority_queue<PII,vector<PII>,greater<PII>>heap;
memset(d,0x3f,sizeof d);
d[1]=0;
heap.push({
0,1});
while(heap.size()){
auto x=heap.top();
heap.pop();
int ver=x.second,distance=x.first;
if(st[ver])continue;
st[ver]=1;
for(int i=h[ver];i!=-1;i=ne[i]){
int j=e[i];
if(d[j]>d[ver]+w[i]){
d[j]=d[ver]+w[i];
if(!st[j]){
heap.push({
d[j],j});
}
}
}
}
return d[0];
}
signed main(){
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin>>n;
memset(h,-1,sizeof h);
for(int i=1;i<n;i++){
//建图
add(i%n,(i+1)%n,1);
add(i%n,(i*10)%n,0);
}
cout<<1+dijkstra()<<endl;
return 0;
}
边栏推荐
- IP地址分类以及网络地址的计算(子网划分、超网划分)[通俗易懂]
- CLIP还能做分割任务?哥廷根大学提出一个使用文本和图像prompt,能同时作三个分割任务的模型CLIPSeg,榨干CLIP能力...
- Custom filters and interceptors implement ThreadLocal thread closure
- 可视化服务编排在金融APP中的实践
- Threshold-based filtering buffer management scheme in a shared buffer packet switch core part of the paper
- 十八、一起学习Lua 调试(Debug)
- Mysql—— 内连接、左连接、右连接以及全连接查询
- You have a Doubaqiong thesaurus, please check it
- 22年BATJ大厂必问面试题(复盘):JVM+微服务+多线程+锁+高并发
- search--09
猜你喜欢
加密游戏:游戏的未来
IM即时通讯开发WebSocket从入门到精通
可视化服务编排在金融APP中的实践
mSystems | 中农汪杰组揭示影响土壤“塑料际”微生物群落的机制
第5章 虚拟存储器
CV复习:空洞卷积
你是怎么知道数据库 Htap 能力强弱的?怎么能看出来
「网络架构」网络代理第一部分: 代理概述
阿里云贾朝辉:云XR平台支持彼真科技呈现国风科幻虚拟演唱会
Can CLIP also do segmentation tasks?The University of Göttingen proposed a model CLIPSeg that uses text and image prompts to perform three segmentation tasks at the same time, draining CLIP capabiliti
随机推荐
MySQL面试题——MySQL常见查询
So delicious!Since using this interface artifact, my team efficiency has increased by 60%!
[List merge] Combine multiple lists into one list
娄底石油化工实验设计、建设规划概述
培训机构学习费用是多少呢?
A detailed explanation of implementation api embed
Color map and depth map to point cloud
dedecms supports one-click import of Word content
LeetCode 86. Delimited Linked List
wirshark 常用操作及 tcp 三次握手过程实例分析
如何培养ui设计师的设计思维?
iTextSharp 使用详解
Twikoo腾讯云函数部署转移到私有部署
Excel函数公式大全—LOOKUP函数
The god-level Alibaba "high concurrency" tutorial - basic + actual combat + source code + interview + architecture is all-inclusive
第5章 虚拟存储器
实践为主,理论为辅!腾讯大佬MySQL高阶宝典震撼来袭!
7. Instant-ngp
Microchip launched a high-performance 77GHz millimeter-wave radar chip, and has received tens of thousands of orders before mass production
An enhanced dynamic packet buffer management.论文核心部分