题意:求$K$的倍数中数位和的最小值。

一开始有一种思路:由于产生答案的数字可能非常大,不便枚举,考虑转化为构造一个数字可以有$x\mod k=0$。然后二分答案数位和,数位DP检验是否存在,但是由于数位DP还是局限于有限大小的数字,所以并不可行。`````

套路见少了。实际上,这种构造数字满足某些条件的,一定可以把目标数字看成是一位一位写下来的,比如$114514$,可以看成逐步写下$1,1,4,5,1,4$不断$\times 10$做加法的结果。所以考虑设$dis_i$为所有$x\mod k=i$的同余类中数位和最小的,然后考虑从当前的这个状态开始写下一位,即连边$i\to (10i+j)\mod k$,边权$j$,然后最初从$dis_{1\sim 9}$(首位)为源点跑最短路,求$dis_0$即可。

atcoder的公式题解给的思路差不多,对于每个数,可以选择加$1$或者乘$10$,前者连边权为$1$,后者连边权$0$,和上面差不多,然后跑$01$最短路。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#define dbg(x) cerr << #x << " = " << x <<endl
#define dbg2(x,y) cerr<< #x <<" = "<< x <<" "<< #y <<" = "<< y <<endl
using namespace std;
typedef long long ll;
typedef double db;
typedef pair<int,int> pii;
template<typename T>inline T _min(T A,T B){return A<B?A:B;}
template<typename T>inline T _max(T A,T B){return A>B?A:B;}
template<typename T>inline bool MIN(T&A,T B){return A>B?(A=B,):;}
template<typename T>inline bool MAX(T&A,T B){return A<B?(A=B,):;}
template<typename T>inline void _swap(T&A,T&B){A^=B^=A^=B;}
template<typename T>inline T read(T&x){
x=;int f=;char c;while(!isdigit(c=getchar()))if(c=='-')f=;
while(isdigit(c))x=x*+(c&),c=getchar();return f?x=-x:x;
}
const int N=1e5+;
struct thxorz{int nxt,to,w;}G[N*];
int Head[N],tot;
int n;
inline void Addedge(int x,int y,int z){G[++tot].to=y,G[tot].nxt=Head[x],Head[x]=tot,G[tot].w=z;}
int dis[N];
priority_queue<pii,vector<pii>,greater<pii> > pq;
#define y G[j].to
inline void dij(){
memset(dis,0x3f,sizeof dis);for(register int i=;i<;++i)MIN(dis[i%n],i),pq.push(make_pair(dis[i%n],i%n));
while(!pq.empty()){
int d=pq.top().first,x=pq.top().second;pq.pop();
if(d^dis[x])continue;//dbg2(x,d);
if(x==)return;
for(register int j=Head[x];j;j=G[j].nxt)if(MIN(dis[y],d+G[j].w))pq.push(make_pair(dis[y],y));
}
}
#undef y
int main(){//freopen("test.in","r",stdin);//freopen("test.ans","w",stdout);
read(n);
for(register int i=;i<n;++i)for(register int j=;j<;++j)Addedge(i,(*i+j)%n,j);
dij();
return printf("%d\n",dis[]),;
}

总结:构造数字题常用思路or最短路常用思路:逐步拆分、分解“代价”为每一步的边的边权。

ABC007D Small Multiple[最短路]的更多相关文章

  1. AtCoder Beginner Contest 077 D Small Multiple(最短路)

    水过前三道题之后,一直在写这个题,做不对.总有那么几组数据过不去... 看了看题解是最短路,这思路感觉很神奇.看了下唯一做出来这题的那人的代码,是搜索做的. 标程: 对每个数字x,向x+1建一条花费为 ...

  2. [USACO14OPEN] Dueling GPS's[最短路建模]

    题目描述 Farmer John has recently purchased a new car online, but in his haste he accidentally clicked t ...

  3. 【CodeForces 567E】President and Roads(最短路)

    Description Berland has n cities, the capital is located in city s, and the historic home town of th ...

  4. hdu 4960 Another OCD Patient (最短路 解法

    http://acm.hdu.edu.cn/showproblem.php?pid=4960 2014 Multi-University Training Contest 9 Another OCD ...

  5. CF449B Jzzhu and Cities (最短路)

    CF449B CF450D http://codeforces.com/contest/450/problem/D http://codeforces.com/contest/449/problem/ ...

  6. HDU 5876 Sparse Graph BFS 最短路

    Sparse Graph Problem Description   In graph theory, the complement of a graph G is a graph H on the ...

  7. hdu-5521 Meeting(最短路)

    题目链接: Meeting Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) ...

  8. HDU 5521 Meeting(虚拟节点+最短路)

    Meeting Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total ...

  9. LightOJ1002 分类: 比赛 最短路 2015-08-08 15:57 15人阅读 评论(0) 收藏

    I am going to my home. There are many cities and many bi-directional roads between them. The cities ...

随机推荐

  1. pramfs安装

    1. 拷贝 linux-3.11 pramfs-1.5.0-3.11 pramfs-Tools 到电脑中(我是新建目录linux中) 2.解压 3.cd pramfs-1.5.0-3.11/ 4.将p ...

  2. 第二周Java课堂作业

    演示一: public class EnumTest { public static void main(String[] args) { Size s=Size.SMALL; Size t=Size ...

  3. JavaScript--QuckStudy

    Day1: 初识JS: https://www.liaoxuefeng.com/wiki/1022910821149312 >打印: alert('我要学JavaScript!'); >J ...

  4. java进程CPU高分析

    JVM导致系统CPU高的常见场景: 内存不足,JVM gc频繁,一般会伴随OOMJVM某个线程死循环或者递归调用 定位和解决1.内存不足,gc频繁可参考我的这遍文章解决.https://blog.cs ...

  5. 怎样在 Vue 里面使用自定义事件将子组件的数据传回给父组件?

    首先, Vue 里面的组件之间的数据流动是 单向 的, 数据可以从父组件传递给子组件, 但不能从子组件传递给父组件, 因为组件和组件之间是 隔离 的. 就像两个嵌套的 黑盒子 . 能通过 props ...

  6. 怎样使用 v-bind 绑定 html 标签的属性值?

    1. 在 Vue 中可是使用 v-bind 对 html 中的 属性 进行绑定, 如下所示, 我们想给这个 a 标签绑定一个 title 值: <!DOCTYPE html> <ht ...

  7. 非常简约学生管理系统——HashSet进行编写

    很小的一个练习,可以参考一下啊~~~~~~ 1:注意:学生类中进行多个重要方法的重写 package com.xt.homework; public class Student { private S ...

  8. C#Linq之求和,平均值,最大值,最小值

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  9. C# 文件过滤器

    首先说明一个示例,分析一下Filter属性的构成:“ Excel文件|*.xls ”,前面的“Excel文件”成为标签,是一个可读的字符串,可以自定定义,“|*.xls”是筛选器,表示筛选文件夹中后缀 ...

  10. 查询SQL Server版本号

    一.查看SQL Server 2005版本号 SELECT @@VERSION 显示以下信息: Microsoft SQL Server 2005 - 9.00.3042.00 (Intel X86) ...