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 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的更多相关文章
- 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) E. Fox And Dinner 网络流建模
E. Fox And Dinner time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- Codeforces Round #290 (Div. 2) B. Fox And Two Dots(DFS)
http://codeforces.com/problemset/problem/510/B #include "cstdio" #include "cstring&qu ...
- 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 #367 (Div. 2) C. Hard problem(DP)
Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...
随机推荐
- C#读写文件总结
1.使用FileStream读写文件 文件头: using System; using System.Collections.Generic; using System.Text; using ...
- LoadRunner参数数组
参数数组提供了对一类参数集中存放的机制,其中LR内置的几个函数有:lr_paramarr_idx().lr_paramarr_len().lr_paramarr_random() 同时参数数组必须满足 ...
- linux中ls命令详解 (转)
-a -- 全部(all).列举目录中的全部文件,包括隐藏文件(.filename).位于这个列表的起首处的 .. 和 . 依次是指父目录和你的当前目录. -l -- 长(long).列举目 ...
- 《DevOps故障排除:Linux服务器运维最佳实践》读书笔记
首先,这本书是Linux.CN赠送的,多谢啦~ http://linux.cn/thread-12733-1-1.html http://linux.cn/thread-12754-1-1.html ...
- 将数字映射到字母上 .xml
映射成 A1------A20 B1------B20 ... Z1------Z20 这种形式 数字从0开始编 ...
- 【LeetCode】88 - Merge Sorted Array
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:Yo ...
- ajax 访问--提高安全性
首先受到struts token的启发,产生了客户端发起的ajax请求进行验证的想法,大致思路是客户端每次请求产生一个key ,然后服务端接收到key,然后解析,判断是否为合法key, 对于不带key ...
- Java与云计算有什么关系呢
在如今这个信息技术高速发展的今天,云计算已经不是一个陌生的概念了,但是,当云计算遇到java将会有什么样的问题产生呢?下面,新霸哥将会为你揭晓Java与云计算之间的关系. 众所周知,java是一种应用 ...
- BITED数学建模七日谈之三:怎样进行论文阅读
前两天,我和大家谈了如何阅读教材和备战数模比赛应该积累的内容,本文进入到数学建模七日谈第三天:怎样进行论文阅读. 大家也许看过大量的数学模型的书籍,学过很多相关的课程,但是若没有真刀真枪地看过论文,进 ...
- 扩展UIColor类
在开发中,UI给出的颜色值有时候是十六进制的,有时候给出的是RGB(255,255,255)这样的形式的,而iOS系统提供的方法中并没有直接传递这两种值返回UIColor对象的,这里自己封装了一个以便 ...