CodeForces-510D
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的更多相关文章
- 【codeforces 510D】Fox And Jumping
[题目链接]:http://codeforces.com/contest/510/problem/D [题意] 你可以买n种卡片; 每种卡片的花费对应c[i]; 当你拥有了第i种卡片之后; 你可以在任 ...
- codeforces510D
Fox And Jumping CodeForces - 510D Fox Ciel is playing a game. In this game there is an infinite long ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
- CodeForces - 261B Maxim and Restaurant
http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...
- CodeForces - 696B Puzzles
http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...
随机推荐
- hdu多校第五场1006 (hdu6629) string matching Ex-KMP
题意: 给你一个暴力匹配字符串公共前缀后缀的程序,为你对于某个字符串,暴力匹配的次数是多少. 题解: 使用扩展kmp构造extend数组,在扩展kmp中,设原串S和模式串T. extend[i]表示T ...
- Lvs+keepalived+mysql(主从复制)
1.准备环境 操作系统:centos 6.5 2台机器主机名为node53.node54 Mysql Lvs keepalived node2 Y Y Y node3 Y Y Y ...
- [USACO11OPEN]玉米田迷宫Corn Maze
题目描述 This past fall, Farmer John took the cows to visit a corn maze. But this wasn't just any corn m ...
- NuGet包介绍
Antlr 各种语言的语法识别器.解析器.编译和翻译器 Microsoft.AspNet.Web.Optimization 绑定优化CSS和JavaScript文件,也就是App_Start下的Bun ...
- postman报InvalidArgumentException
解决方案:
- leetcode-157周赛-5214-最长定差子序列
题目描述: class Solution: def longestSubsequence(self, arr: List[int], difference: int) -> int: dp = ...
- Kibana启动报错 server is not ready yet的解决方案
前言: 今天在搭建elasticsearch集群的时候,再次使用Kibana操作elasticsearch的时候报告Kibana server is not ready yet的问题, 通过在 ...
- 有关axios的request与response拦截
// http request 拦截器 axios.interceptors.request.use( config => { var token = localStorage.getItem( ...
- php数据结构课程---7、队列实战
php数据结构课程---7.队列实战 一.总结 一句话总结: 注意条件:注意循环的条件(比如while循环打印队列元素时),注意if的条件 把问题想清楚:比如链表操作初次插入元素和后面再插,效果是不一 ...
- System.Web.Mvc.HttpPatchAttribute.cs
ylbtech-System.Web.Mvc.HttpPatchAttribute.cs 1.程序集 System.Web.Mvc, Version=5.2.3.0, Culture=neutral, ...