当前位置:网站首页>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;
}
边栏推荐
- iTextSharp 使用详解
- MySQL索引的B+树到底有多高?
- CURRENT_TIMESTAMP(6) 函数是否存在问题?
- 技术人必看!数据治理是什么?它对数据中台建设重要吗?
- 制品库是什么?
- 搜索--01
- mpf6_Time Series Data_quandl_correct kernel PCA_AIC_BIC_trend_log_return_seasonal_decompose_sARIMAx_ADFull
- LeetCode 237. Delete a node in a linked list
- Data Analysis of Time Series (5): Simple Prediction Method
- Guo Jingjing's personal chess teaching, the good guy is a robot
猜你喜欢

22年BATJ大厂必问面试题(复盘):JVM+微服务+多线程+锁+高并发

「企业架构」应用架构概述

中科院深圳先进技术院合成所赵国屏院士组2022年招聘启事

Alibaba Cloud Jia Zhaohui: Cloud XR platform supports Bizhen Technology to present a virtual concert of national style sci-fi

CV复习:空洞卷积

线代 | 秒杀方法与技巧

你有一份斗破苍穹词库,请查收

Crypto Gaming: The Future of Gaming

关于flask中static_folder 和 static_url_path参数理解

如何让别人看不懂你的 JS 代码?把你当大佬!
随机推荐
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
LeetCode 83. Remove Duplicate Elements in Sorted List
协程与任务
Nanodlp v2.2/v3.0光固化电路板,机械开关/光电开关/接近开关的接法和系统状态电平设置
What are the five common data types of Redis?What is the corresponding data storage space?Take you to learn from scratch
iTextSharp 使用详解
Chapter 5 virtual memory
「网络架构」网络代理第一部分: 代理概述
LeetCode 21. Merge two ordered linked lists
制品库是什么?
MySQL面试题——MySQL常见查询
CLIP还能做分割任务?哥廷根大学提出一个使用文本和图像prompt,能同时作三个分割任务的模型CLIPSeg,榨干CLIP能力...
47Haproxy集群
解决 idea 单元测试不能使用Scanner
Dining (web stream)
蚂蚁金服+拼多多+抖音+天猫(技术三面)面经合集助你拿大厂offer
CodeForces - 628D (digital dp)
So delicious!Since using this interface artifact, my team efficiency has increased by 60%!
22年BATJ大厂必问面试题(复盘):JVM+微服务+多线程+锁+高并发
Common examples of regular expressions