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多校第六场1005 (hdu6638) Snowy Smilel 线段树/区间最大和

    题意: 给定一个矩阵,矩阵上有若干点,每个点有正或负的权值,找一个方框框住一些点使得方框中点权值最大. 题解: 离散化横纵坐标,容易将这个问题转化为在矩阵上求最大和子矩阵的问题. 普通的n*n的矩阵的 ...

  2. iOS开发之UIMenuController

    1.简介 用于剪切.复制.粘贴.选择.选择ALL和DELETE命令的菜单界面. UITextField.UIWebView.UITextView自带有这种UIMenuController效果: 2.相 ...

  3. 极限学习机(Extreme Learning Machine)学习笔记

    最近研究上了这个一个东西--极限学习机. 在很多问题中,我大多会碰到两个问题,一个是分类,另一个就是回归.简单来说,分类是给一串数打个标签,回归是把一串数变为一个数. 在这里我们需要处理的数据一般维度 ...

  4. Bootstrap3的响应式缩略图幻灯轮播效果设计

    在线演示1 本地下载 HTML <div class="container">  <div class="col-md-12">  &l ...

  5. spring boot junit test

    这里分三种,1.测普通方法或通过原生java API接口调用 2.基于spring依赖注入调用 3.controller层调用 需要引入依赖:默认springboot已经引入 <dependen ...

  6. oracle中的decode函数的使用

    含义解释:decode(条件,值1,返回值1,值2,返回值2,...值n,返回值n,缺省值) 该函数的含义如下:IF 条件=值1 THEN RETURN(翻译值1)ELSIF 条件=值2 THEN R ...

  7. 2018-8-10-WPF-使用-Direct2D1-画图-绘制基本图形

    title author date CreateTime categories WPF 使用 Direct2D1 画图 绘制基本图形 lindexi 2018-08-10 19:16:53 +0800 ...

  8. 关于python merge后数据行数增加的问题

    其中一个可能的原因是 join 的 data 里面的列不唯一,也就是要匹配的表里面有些一行数据对应了被匹配表多条数据,这样出来可能会增加行数,可以再查一下被匹配表里的数据是否去重

  9. Leetcode241.Different Ways to Add Parentheses为运算表达式设计优先级

    给定一个含有数字和运算符的字符串,为表达式添加括号,改变其运算优先级以求出不同的结果.你需要给出所有可能的组合的结果.有效的运算符号包含 +, - 以及 * . 示例 1: 输入: "2-1 ...

  10. pdfkit

    官方文档 0.准备 需要引入两个包,首先要npm install pdfkit安装pdfkit包 const PDF = require('pdfkit'); const fs = require(' ...