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. c++11新特性学习2

    noexcept 替代 throw.优点是更安全, 如果noexcept 修饰的函数抛出了异常,编辑器可以直接选择终止程序. C++ 11中析构函数默认为noexcept(true),从而阻止异常的扩 ...

  2. jquery scrollTop() 方法

    原文地址:http://www.w3school.com.cn/jquery/css_scrolltop.asp 实例 设置 元素中滚动条的垂直偏移: $(".btn1").cli ...

  3. StringUtils里的isEmpty方法和isBlank方法的区别

    原文地址:https://blog.csdn.net/a1102325298/article/details/80410740 isEmpty public static boolean isEmpt ...

  4. 把Debian 设置中文环境

    要支持区域设置,首先要安装locales软件包: apt-get install locales 然后配置locales软件包: dpkg-reconfigure locales 在界面中钩选上“zh ...

  5. 循环神经网络RNN

    转自 http://blog.csdn.net/xingzhedai/article/details/53144126 更多参考:http://blog.csdn.net/mafeiyu80/arti ...

  6. lambda x:i*x for i in range(4)

    解决方法:冒号前添加接收 i 的变量 return [lambda x,i=i: i * x for i in range(4)]

  7. 17-MySQL-Ubuntu-数据表的查询-分页(六)

    分页(limit) 注: (1)limit位于SQL语句的最后面; (2)limit 2; 2表示查询前两条数据; (3)limit 0,2;  0表示查询第1页的起始数据的下标,2表示每页有两条数据 ...

  8. 三层(3-tier architecture)基础

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/lhc2207221755/article/details/24802053     之前看过非常多关 ...

  9. 基于第三方开源库的OPC服务器开发指南(4)——后记:与另一个开源库opc workshop库相关的问题

    平心而论,我们从样例服务器的代码可以看出,利用LightOPC库开发OPC服务器还是比较啰嗦的,网上有人提出opc workshop库就简单很多,我千辛万苦终于找到一个05年版本的workshop库源 ...

  10. DataList做一个相册,并可以上传图片

    1.前台代码 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DataLis ...