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. mysql优化SQL语句的一般步骤及常用方法

    一.优化SQL语句的一般步骤 1. 通过show status命令了解各种SQL的执行频率 mysqladmin extended-status 或: show [session|global]sta ...

  2. java读取目录下所有csv文件数据,存入三维数组并返回

    package dwzx.com.get; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; ...

  3. js正则表达式中=s.g表示什么意思

    //g是全局匹配//中间的内容表示:匹配以=开关,后面是0或多个空格,然后是双引号括起来的任意字符,比如:= "any symble" 匹配 = " asfjaskldf ...

  4. [转]Java Web乱码过滤器

    本文转自http://blog.csdn.net/l271640625/article/details/6388690 大家都知道,在jsp里乱码是最让人讨厌的东西,有些乱码出来的莫名其妙,给开发带来 ...

  5. Spring Boot 学习笔记 - 认识Spring Boot框架

    因各种原因,.NET前端工程师重新接触JAVA,真是向全栈的路上又迈出了无奈的一步. 下面正文: Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初 ...

  6. 记录一下ORACLE 10gR2的软件下载地址,备用。

    转载自:http://www.eygle.com/archives/2008/06/10gr2_download_link.html 记录一下ORACLE 10gR2的软件下载地址,备用. 下载OTN ...

  7. (转载)OC学习篇之---Foundation框架中的NSString对象和NSMutableString对象

    在之前的一篇文章中我们说到了Foundation框架中的NSObject对象,那么今天在在来继续看一下Foundation框架中的常用对象:NSString和NSMutableString. 在OC中 ...

  8. Web服务器与Servlet容器

    今日要闻: Oracle启动了JRE7到JRE8的自动更新, JRE8发布于2014.3,于2014.10成为java.com默认版本, JRE7发布于2011.7, Oracle指定的Java生命政 ...

  9. 1、Hadoop架构

    1.Hadoop 是一个能够对大量数据进行分布式处理的软件框架,实现了Google的MapReduce编程模型和框架,能够把应用程序分割成许多小的工作单元放到任何集群节点上执行. 作业(job):一个 ...

  10. SCAU 10893 Spiral

    10893 Spiral 时间限制:1000MS  内存限制:65535K 题型: 编程题   语言: 无限制 Description Given an odd number n, we can ar ...