https://vjudge.net/problem/CodeForces-510D
题目可以转化为花最小代价选一些数,然后这些数可以经过加减运算得到1或-1,不然1你就凑不出来,一旦凑出来1,其他的都有了。由贝祖定理,ax+by=gcd(a,b)=1,贝祖定理能推广到n个,ax+by+cz+…=gcd(a,b,c,…)。令m[i]为得到的gcd为i的最小代价,spfa暴力更新,最后输出m[1]

#include <iostream>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <unordered_map>
#define inf 2147483647
#define N 1000010
#define p(a) putchar(a)
#define For(i,a,b) for(int i=a;i<=b;++i) using namespace std;
int n,k;
int L[N],c[N];
queue<int>q;
unordered_map<int,int>m;
void in(int &x){
int y=;char c=getchar();x=;
while(c<''||c>''){if(c=='-')y=-;c=getchar();}
while(c<=''&&c>=''){ x=(x<<)+(x<<)+c-'';c=getchar();}
x*=y;
}
void o(int x){
if(x<){p('-');x=-x;}
if(x>)o(x/);
p(x%+'');
} int gcd(int a,int b){
return (a%b==?b:gcd(b,a%b));
} void spfa(){
while(!q.empty()){
int t=q.front();q.pop();
For(i,,n){
k=gcd(L[i],t);
if(!m[k]){
q.push(k);
m[k]=m[t]+m[L[i]];
}
else
m[k]=min(m[k],m[t]+m[L[i]]);
}
}
} signed main(){
in(n);
For(i,,n)
in(L[i]);
For(i,,n)
in(c[i]);
For(i,,n)
if(!m[L[i]])
m[L[i]]=min(m[L[i]],c[i]);
else{
q.push(L[i]);
m[L[i]]=c[i];
}
spfa();
if(m[])
o(m[]);
else
o(-);
return ; }

CodeForces-510D的更多相关文章

  1. 【codeforces 510D】Fox And Jumping

    [题目链接]:http://codeforces.com/contest/510/problem/D [题意] 你可以买n种卡片; 每种卡片的花费对应c[i]; 当你拥有了第i种卡片之后; 你可以在任 ...

  2. codeforces510D

    Fox And Jumping CodeForces - 510D Fox Ciel is playing a game. In this game there is an infinite long ...

  3. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  4. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  5. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  6. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  7. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  8. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

  9. CodeForces - 261B Maxim and Restaurant

    http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...

  10. CodeForces - 696B Puzzles

    http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...

随机推荐

  1. hdu多校第八场 1009 (hdu6665) Calabash and Landlord 计算几何/dfs

    题意: 给定两个矩形,输出这两个矩形把平面分成了多少块. 题解: 本来是道计算几何的大讨论,被我生生写成了bfs. 离散化边,注意不重合的边中间要空出来一格,四周也要空出来一圈,然后暴力bfs计算一共 ...

  2. Codeforces786B

    传送门 n个节点且固定起点最短路,三种加边方法 1.u->v, 边权为w:2. u->[l, r], 边权为w:3. [l, r]->u, 边权为w AC_Code #include ...

  3. docker实用参数

    docker images:查看docker镜像docker ls:查看运行中的docker 镜像docker run -d -p 80:80 -v /home/xxxxx/nginx-conf/ht ...

  4. jdk linux配置

    用文本编辑器打开/etc/profile 在profile文件末尾加入: export JAVA_HOME=/usr/share/jdk1.6.0_14 export PATH=$JAVA_HOME/ ...

  5. The packaging for this project did not assign a file to the build artifact

    当进行mvn install时,遇到以下错误 The packaging for this project did not assign a file to the build artifact 在网 ...

  6. word 文献标题自动编号

    来自:word中自动编号和多级编号的使用 选中标题或段落,点击鼠标右键,在编号菜单内选择适合的自动编号样式.或者在窗口上方的“开始”选项卡中选择编号样式.如果对已选的编号样式不满意,可以照以上方法直接 ...

  7. scala中闭包的使用

    闭包的实质就是代码与用到的非局部变量的混合,即: 闭包 = 代码 + 用到的非局部变量 实例1: 匿名函数中引入闭包 val multiplier = (i:Int) => i * factor ...

  8. HTML_案例(首页制作)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. flink支持的数据类型讲解(可序列化) 和 内置累加器的运用

    flink支持的数据类型Flink对DataSet和DataStream中可使用的类型加了一些约束.原因是系统可以通过分析这些类型来确定有效的执行策略和选择不同的序列化方式.有7种不同的数据类型:1. ...

  10. django 项目分析

    项目要点 一.功能制定 1.用户功能 #.登陆 #.权限组功能 2.数据展示功能 #.列表展示 #.详细信息展示 #.图标展示 3.资源管理功能 #远程管理 #对远程服务器上的进程具有 #开启 #关闭 ...