题解:

首先将问题转化,可以发现操作改为两种

一种是s*=k,代价为k,一种是s--,代价为1

转化成图论,spfa跑最短路

然后更据一些证明,代价1的k<=13且为质数,并且不可能操作2连续5次

所以就可以优化

代码:

#include<bits/stdc++.h>
using namespace std;
const int N=,M=;
int n,a[N],b[M],f[N],in[N],dis[N];
int main()
{
scanf("%d",&n);
f[]=;
f[]=,f[]=,f[]=,f[]=,f[]=,f[]=;
int l=,r=;
memset(dis,0x3f,sizeof dis);
dis[]=;
b[]=;
while (l!=r)
{
int now=b[l++];
in[now]=;
l%=M;
if (now!=&&dis[now-]>dis[now]+)
{
dis[now-]=dis[now]+;
if (!in[now-])
{
b[r++]=now-;
in[now-]=;
r%=M;
}
}
for (int i=;f[i]*now<=n+&&i<=f[];i++)
{
int k=f[i]*now;
if (dis[k]>dis[now]+f[i])
{
dis[k]=dis[now]+f[i];
if (!in[k])
{
b[r++]=k;
in[k]=;
r%=M;
}
}
}
}
printf("%d",dis[n]);
}

51nod1693的更多相关文章

  1. 51nod1693 水群

    题目链接:51nod1693 水群 题解参考大神的博客:http://www.cnblogs.com/fighting-to-the-end/p/5874763.html 这题时限0.4秒,真的够狠的 ...

  2. 51nod1693 水群 最短路

    若A=K*B,若仅通过操作二:将B变换为A需要K步, 由算数基本定理可知:k=p1*p2*……pn(p为素数,且可能重复) 那么:将B转化为p1*B需要p1步,将p1*B转化为p1*p2*B需要p2步 ...

随机推荐

  1. 重写(override)与重载(overload)的区别

    一.重写(override) override是重写(覆盖)了一个方法,以实现不同的功能.一般是用于子类在继承父类时,重写(重新实现)父类中的方法. 重写(覆盖)的规则: 1.重写方法的参数列表必须完 ...

  2. C++写入mbr

    #include <windows.h> #include <winioctl.h> unsigned char scode[] = "\xb8\x12\x00\xc ...

  3. luogu P1880石子归并

    石子归并 luogu1880 传送门   noi1995 在一个圆形操场的四周摆放N堆石子,现要将石子有次序地合并成一堆.规定每次只能选相邻的2堆合并成新的一堆,并将新的一堆的石子数,记为该次合并的得 ...

  4. Redis Cluster集群

    一.redis-cluster设计 Redis集群搭建的方式有多种,例如使用zookeeper等,但从redis 3.0之后版本支持redis-cluster集群,Redis-Cluster采用无中心 ...

  5. Future Works on P4

    Future Works on P4 P4 and NV: MPvisor, Hyper4, HyperV, Flex4 P4 and NFV P4 and Network Cache P4 and ...

  6. ubuntu 14.04 (desktop amd 64) 安装和配置ROS Indigo

    安装ROS 配置Ubuntu的软件源 配置Ubuntu要求允许接受restricted.universe和multiverse的软件源,可以根据下面的链接配置: https://help.ubuntu ...

  7. 重新拾取的jquery

    最新JQ API学习地址:http://www.css88.com/jqapi-1.9/error/

  8. Linux更改主机名

    1.临时 # hostname newhostname 2.修改/etc/hostname文件,需重启 # vim /etc/hostname 3.查看 # hostname Ubuntu18 # h ...

  9. Codeforces D - Ithea Plays With Chtholly

    D - Ithea Plays With Chtholly 思路:考虑每个位置最多被替换c/2次 那么折半考虑,如果小于c/2,从左往右替换,大于c/2总右往左替换,只有小于这个数(从左往右)或者大于 ...

  10. javascript变量声明及作用域总结

    javascript变量声明及作用域总结 一.总结 一句话总结:还是得好好看书,光看视频是不得行的,浅学无用,要相互印证,要真正理解才有用,比如<Javascript权威指南> 书 1.j ...