D. Fox And Jumping

题目连接:

http://codeforces.com/contest/510/problem/D

Description

Fox Ciel is playing a game. In this game there is an infinite long tape with cells indexed by integers (positive, negative and zero). At the beginning she is standing at the cell 0.

There are also n cards, each card has 2 attributes: length li and cost ci. If she pays ci dollars then she can apply i-th card. After applying i-th card she becomes able to make jumps of length li, i. e. from cell x to cell (x - li) or cell (x + li).

She wants to be able to jump to any cell on the tape (possibly, visiting some intermediate cells). For achieving this goal, she wants to buy some cards, paying as little money as possible.

If this is possible, calculate the minimal cost.

Input

The first line contains an integer n (1 ≤ n ≤ 300), number of cards.

The second line contains n numbers li (1 ≤ li ≤ 109), the jump lengths of cards.

The third line contains n numbers ci (1 ≤ ci ≤ 105), the costs of cards.

Output

If it is impossible to buy some cards and become able to jump to any cell, output -1. Otherwise output the minimal cost of buying such set of cards.

Sample Input

3

100 99 9900

1 1 1

Sample Output

2

Hint

题意

给你n个数,以及选择每个数的权值,要求你花费尽量少,使得选出来的数gcd = 1

题解:

直接暴力DP,dp[i]表示gcd为i需要的最小代价,dp[gcd(x,y)] = min(dp[gcd(x,y)],dp[x]+dp[y])

代码

#include<bits/stdc++.h>
using namespace std; map<int,int> H;
int gcd(int x,int y)
{
if(y==0)return x;
return gcd(y,x%y);
}
#define maxn 350
int a[maxn];
int val[maxn];
void updata(int x,int val)
{
if(H[x]==0)H[x]=1e9;
H[x]=min(H[x],val);
}
int main()
{
int n;
scanf("%d",&n);
map<int,int>::iterator it;
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
for(int i=1;i<=n;i++)
scanf("%d",&val[i]);
for(int i=1;i<=n;i++)
{
updata(a[i],val[i]);
for(it=H.begin();it!=H.end();it++)
updata(gcd(it->first,a[i]),val[i]+it->second);
}
if(H[1]==0)return puts("-1");
else printf("%d\n",H[1]);
}

Codeforces Round #290 (Div. 2) D. Fox And Jumping dp的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. Codeforces Round #290 (Div. 2) E. Fox And Dinner 网络流建模

    E. Fox And Dinner time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  5. Codeforces Round #290 (Div. 2) B. Fox And Two Dots(DFS)

    http://codeforces.com/problemset/problem/510/B #include "cstdio" #include "cstring&qu ...

  6. DFS Codeforces Round #290 (Div. 2) B. Fox And Two Dots

    题目传送门 /* DFS:每个点四处寻找,判断是否与前面的颜色相同,当走到已走过的表示成一个环 */ #include <cstdio> #include <iostream> ...

  7. 找规律 Codeforces Round #290 (Div. 2) A. Fox And Snake

    题目传送门 /* 水题 找规律输出 */ #include <cstdio> #include <iostream> #include <cstring> #inc ...

  8. 拓扑排序 Codeforces Round #290 (Div. 2) C. Fox And Names

    题目传送门 /* 给出n个字符串,求是否有一个“字典序”使得n个字符串是从小到大排序 拓扑排序 详细解释:http://www.2cto.com/kf/201502/374966.html */ #i ...

  9. Codeforces Round #367 (Div. 2) C. Hard problem(DP)

    Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...

随机推荐

  1. 6. ActionBar详解

    ActionBar简介  隐藏和显示ActionBar 隐藏ActionBar      getActionBar().hide(); 显示ActionBar      getActionBar( ...

  2. Linux下安装Vapor

    1.官网下载Vapor软件(二进制安装文件) 注:注意版本,linux下可以在终端输入-uname -l 查看系统版本 2.cd到Vapor软件所在目录 3.解压:1)gunzip vapor-*** ...

  3. html --- VML --- javascript --- 旋转矩形

    矢量标记语言 --- Vector Markup Language 运行它的代码需要打开IE的兼容性视图 如有疑问请参考:http://msdn.microsoft.com/en-us/library ...

  4. 二.JSP开发过程中遇到的问题及解决

    一.开发环境问题 问题一:Failed to load the JNI shared library 启动Eclipse时弹出“Failed to load the JNI shared librar ...

  5. Linux 的 screen用法

    screen可以将任务挂起,即将任务放在后台,一般5个任务左右. 1.新建screen会话:直接输入screen命令或者screen -S [会话名称] 2.退出会话:按下组合键Ctrl+a并松开,此 ...

  6. make subversion时出现neon报错 及 svn其他问题汇总(3ge )

    在make subvision时,出现以下错误提示: /usr/local/src/neon-0.29.6/src/ne_auth.c:781: undefined reference to`ne__ ...

  7. LeetCode(6) - ZigZag Conversion

    这个题的要求是给你一个字符串,和一个行数,例如(s = "mysisteristhemostlovelygirl" , row = 4),每一行一个字符串,但是s却得按照zigza ...

  8. easyui datagrid 部分参数整理

    数据表格属性(DataGrid Properties) 属性继承控制面板,以下是数据表格独有的属性. 名称 类型 描述 默认值 columns array 数据表格列配置对象,查看列属性以获取更多细节 ...

  9. c++builder Color

    procedure ExtractRGB(const Color: Graphics.TColor; out Red, Green, Blue: Byte); var RGB: Windows.TCo ...

  10. <MFC_1>深入剖析MFC的WinMain和消息机制

    一.开篇引论 熟悉Win32开发的朋友,应该非常了解它的基本组成和流程 1. WinMain:书写窗口类(WNDCLASS) -> 注册窗口类 -> 创建窗口 -> 显示窗口和更新窗 ...