寒假222_codeforces 290 div 2 D
序号5:
想了很久的DP ,应该很简单,但是。。
题目直接转化为求n个数中选一些数GCD=1且花费最小
数比较大
map HASH
还有一点 我们知道 GCD(X,X*Y)==X;
所以我的代码里不用考虑这点了
pasting
#include<stdio.h>
#include<algorithm>
#include<string>
#include<iostream>
#include<queue>
#include<cmath>
#include<string.h>
#include <vector>
#include<map>
using namespace std;
typedef long long ll;
map<int,int>mp;
int a[333],b[333];
#define inf 0x3f3f3f3f map<int,int>::iterator it; int gcd(int x,int y)
{
if (x%y==0) return y;
return gcd(y,x%y);
} void dfs(int x,int y)
{
if (!mp[x]) mp[x]=inf;
mp[x]=min(mp[x],y);
for (it=mp.begin();it!=mp.end();it++)
{
int tmp=(*it).first;
int v= (*it).second;
int nw=gcd(tmp,x);
if (!mp[nw]) mp[nw]=inf;
mp[nw]=min(mp[nw],v+y);
}
} int main()
{
int n;
cin>>n;
for (int i=1;i<=n;i++) cin>>a[i];
for (int i=1;i<=n;i++) cin>>b[i]; for (int i=1;i<=n;i++)
dfs(a[i],b[i]);
if (mp[1]==0) mp[1]=-1;
cout<<mp[1];
return 0;
}
寒假222_codeforces 290 div 2 D的更多相关文章
- [CF#290 Div.1 C]Fox And Dinner(最大流)
题目:http://codeforces.com/contest/512/problem/C 题目大意:给你若干个数,让你分成k组,每组围成一个圆,使得相邻两个数和均为素数,且每组人数应>=3个 ...
- DFS Codeforces Round #290 (Div. 2) B. Fox And Two Dots
题目传送门 /* DFS:每个点四处寻找,判断是否与前面的颜色相同,当走到已走过的表示成一个环 */ #include <cstdio> #include <iostream> ...
- 找规律 Codeforces Round #290 (Div. 2) A. Fox And Snake
题目传送门 /* 水题 找规律输出 */ #include <cstdio> #include <iostream> #include <cstring> #inc ...
- 拓扑排序 Codeforces Round #290 (Div. 2) C. Fox And Names
题目传送门 /* 给出n个字符串,求是否有一个“字典序”使得n个字符串是从小到大排序 拓扑排序 详细解释:http://www.2cto.com/kf/201502/374966.html */ #i ...
- Codeforces Round #290 (Div. 2) D. Fox And Jumping dp
D. Fox And Jumping 题目连接: http://codeforces.com/contest/510/problem/D Description Fox Ciel is playing ...
- Codeforces Round #290 (Div. 2) C. Fox And Names dfs
C. Fox And Names 题目连接: http://codeforces.com/contest/510/problem/C Description Fox Ciel is going to ...
- Codeforces Round #290 (Div. 2) B. Fox And Two Dots dfs
B. Fox And Two Dots 题目连接: http://codeforces.com/contest/510/problem/B Description Fox Ciel is playin ...
- Codeforces Round #290 (Div. 2) A. Fox And Snake 水题
A. Fox And Snake 题目连接: http://codeforces.com/contest/510/problem/A Description Fox Ciel starts to le ...
- CodeForces Round #290 Div.2
A. Fox And Snake 代码可能有点挫,但能够快速A掉就够了. #include <cstdio> int main() { //freopen("in.txt&quo ...
随机推荐
- WebBrowser里网页根据文字判断来点击链接 无Name及ID时
uses ActiveX, ComObj, MSHTML; 根据连接文字点击连接- 一般情况下的连接 Procedure HTMLClinkByText(text:string;Wbr:TWebBro ...
- C++不完整的类型
今天写C++primer 5th中文版第422页的程序时,出现了”不允许使用不完整的类型“的错误,下面我就用类A 与 类B 作为代表,重现一下该错误,并且提出解决方案. 一.带问题的类设计A: 1.类 ...
- 3.css中的颜色
css中颜色的设置形式主要有三种方式:颜色名称.十六进制代码和十进制代码. 在古老的 HTML4 时,颜色名称只有 16 种. 颜色名称 十六进制代码 十进制代码 含义 black #000000 ...
- [.NET]程序在线更新,力求通用
@微微一笑 :貌似是我的第一篇博客,在外流浪了很久很久,最终还是驻留在博客园混日子吧. 在线更新,想想自己做过的项目中都有这么一个功能.虽然比较简单,但是功能还是比较重要的.抽点时间写了一个在线更新程 ...
- hashCode()和toString()
hashCode函数和toString函数也在Object类中,同样,所有的类都继承了这2个函数. hashCode函数用于生成哈希码,没有参数,返回值为整型 把u的值作为键存入map中,使用get方 ...
- Eclipse常用快捷键使用
Eclipse的编辑功能非常强大,掌握了Eclipse快捷键功能,能够大大提高开发效率.Eclipse中有如下一些和编辑相关的快捷键. 1. [ALT+/] 此快捷键为用户编辑的好帮手 ...
- Tutorial: Facebook analytics using Power BI Desktop
In this tutorial you learn how to import and visualize data from Facebook. During the tutorial you'l ...
- meteor 实现 微信分享
Template.hello.events({ 'click button': function () { // increment the counter when button is clicke ...
- Spring Roo
Spring Roo 是SpringSource新的开放源码技术,该技术主要面向企业中的Java开发者,使之更富有成效和愉快的进行开发工作,而不会牺牲工程完整或灵活性.无论你是一个新的Java开发人员 ...
- 实体框架 (EF) 入门 => 六、性能注意事项
这个还真是复杂,看了看微软的文档,有些根本就看不懂,有些能看懂,但对我这种菜鸟也不会去用. 无从下手啊,前面放了几个链接,挨个试试吧. 一.显式打开连接 这个我测试过,有些时候,需要我们显示打开连接, ...