题干:

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.

Examples

Input

3
100 99 9900
1 1 1

Output

2

Input

5
10 20 30 40 50
1 1 1 1 1

Output

-1

Input

7
15015 10010 6006 4290 2730 2310 1
1 1 1 1 1 1 10

Output

6

Input

8
4264 4921 6321 6984 2316 8432 6120 1026
4264 4921 6321 6984 2316 8432 6120 1026

Output

7237

Note

In first sample test, buying one card is not enough: for example, if you buy a card with length 100, you can't jump to any cell whose index is not a multiple of 100. The best way is to buy first and second card, that will make you be able to jump to any cell.

In the second sample test, even if you buy all cards, you can't jump to any cell whose index is not a multiple of 10, so you should output -1.

题目大意:

给出n张卡片,每张带有两个权值l[i]和c[i]。在一条无限长的纸带上,你可以选择花c[i]的钱来购买卡片i,从此以后可以向左或向右跳l[i]个单位。购买其他卡片后,可以获得更多的跳跃单位。先要求至少花多少元钱才能够任意跳到纸带上任意一个位置。若不行,输出-1。

解题报告:

首先你要知道既然要可以跳到所有位置,充要条件是可以跳到1这个位置。换句话说,要想完成任务必须可以跳到1,而跳到1之后就可以跳到所有的位置,所以问题转化为挑选k张卡片来凑出 他们的 GCD 为1。

然后我们用map进行滚动数组dp,,还有一个问题就是每次比较不用所有的都比较,只用管比较的两个数的gcd就行了,,因为只要gcd可以出来了,其他的都可以出来。(之前cf有一个凑等差数列的也是运用这个思想)

至于时间复杂度问题,可以证明1e9范围内的每个数字,所具有的质因子个数<=9。所以在时间复杂度上是说的过去的。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e5 + 5;
ll l[MAX];
ll c[MAX];
map<ll , ll > mp;
map<ll , ll > :: iterator it;
int main()
{int n;cin>>n;for(int i = 1; i<=n; i++) scanf("%lld",l+i);for(int i = 1; i<=n; i++) scanf("%lld",c+i);mp[0]=0;for(int i = 1; i<=n; i++) {//if(mp.find(l[i]) == mp.end()) mp[l[i]]=c[i];for(it = mp.begin(); it!= mp.end(); ++it) {ll g = __gcd(it->fi,l[i]);//printf("%lld\n",mp[1]);if(mp.find(g) == mp.end()) {mp[g] = it->se + c[i];}else {mp[g] = min(mp[g],it->se + c[i]);//printf("%lld\n",mp[1]);}}}
//  if(mp.size() > 500000) printf("hahahaha");if(mp.find(1) == mp.end()) puts("-1");else printf("%lld\n",mp[1]); return 0 ;}

【CodeForces - 510D】Fox And Jumping(dp,stlmap,数论的性质)相关推荐

  1. CF510D Fox And Jumping(动态规划转换为最短路,O(n^2×2^9) -> O(nlogn),裴蜀定理应用)

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 以下内容摘自 我的文章:算法竞赛中的数论问题 - 数论全家桶(信奥 / 数竞 / ACM)作者孟繁宇, ...

  2. 麦香牛肉(dp 、数论)

    麦香牛肉(dp .数论) 麦香牛肉 时间限制: 1 Sec  内存限制: 128 MB 题目描述 农夫约翰的奶牛几乎要武装暴动,因为他们听说麦当劳要推出新产品麦香牛肉.奶牛们要尽力阻止这种产品的上市. ...

  3. CodeForces - 1312E Array Shrinking(区间dp)(通俗易懂)

    CodeForces - 1312E Array Shrinking(区间dp) 题目链接: 没做出来,看了一下别人的题解,才A掉.但网上没发现一篇讲得比较易懂的题解,所以就准备写一篇再加上我自己的理 ...

  4. 【CodeForces 1253C --- Sweets Eating】DP

    [CodeForces 1253C --- Sweets Eating]DP Description Tsumugi brought n delicious sweets to the Light M ...

  5. 【Codeforces - 977F】Consecutive Subsequence(STLmap,输出路径,dp)

    题干: You are given an integer array of length nn. You have to choose some subsequence of this array o ...

  6. codeforces Palindromic characteristics(hash或者dp)

    1.动态规划 用dp(l,r)表示子串s[l..r]的回文串阶数.对于长度len为1的有dp(l,r)=1.对于长度len等于2的,看字符串左右是否相等即可.当r-l>1时,如果s[l]不等于s ...

  7. codeforces 808 E. Selling Souvenirs (dp+二分+思维)

    题目链接:http://codeforces.com/contest/808/problem/E 题意:最多有100000个物品最大能放下300000的背包,每个物品都有权值和重量,为能够带的最大权值 ...

  8. Codeforces 429B Working out:dp【枚举交点】

    题目链接:http://codeforces.com/problemset/problem/429/B 题意: 给你一个n*m的网格,每个格子上有一个数字a[i][j]. 一个人从左上角走到右下角,一 ...

  9. CodeForces - 1551F Equidistant Vertices(暴力+dp)

    题目链接:点击查看 题目大意:给出一棵 nnn 个节点组成的树,问选出 kkk 个节点满足相互之间距离相等的方案数有多少 题目分析:n=100n=100n=100,感觉数据范围越小的题目越难发现 ta ...

最新文章

  1. oracle date 转换 timestamp,Oracle timestamp类型转换成date类型
  2. 智能猫窝是如何诞生的?详解百度大脑的开放生态
  3. php 对数据转换成tree,PHP 把返回的數據集轉換成Tree樹
  4. Redis Monitor命令 - 实时打印出Redis服务器接收到的命令,调试用
  5. linux操作系统基本配置
  6. 基于华为云对话机器人技能平台的规则模板概述
  7. java在捕获异常并弹窗_Java捕获异常的问题
  8. swiper在微信端滑动效果不友好(滑动不了)的解决方案
  9. tensorflow Image 解码函数
  10. Codevs 2800 送外卖(状压DP)
  11. 对侯捷《Word 排版艺术》的期待
  12. 易语言5.4一键破解工具
  13. 关于websocket兼容IE版本
  14. 前端之ps的基本操作
  15. 点击子元素却也触发父元素的点击事件
  16. 如何查看自己已连接WiFi的密码
  17. linux蓝牙打印机驱动安装失败怎么办,打印机驱动安装失败怎么办?
  18. v4l2框架—申请缓存(VIDIOC_REQBUFS)
  19. 基于RWEQ模型的土壤风蚀模数估算及其变化归因分析
  20. 2021 CNSS招新赛 WEB WP

热门文章

  1. [密码学基础][每个信息安全博士生应该知道的52件事][Bristol52]48.TPM的目的和使用方法
  2. c语言 用一维数组存储二叉树,用一维数组存储二叉树时,总是以前序遍历顺序存储结点。( ? )...
  3. ssdp协议_Cotopaxi:使用指定IoT网络协议对IoT设备进行安全测试
  4. java服务注册中心有哪些_Spring Cloud服务注册中心简述
  5. python django部署docker_centos利用docker部署django项目
  6. android怎么ota升级,Android OTA升级过程
  7. Air Data System
  8. Qt+VS2005(配置步骤)
  9. 如何使用autotools工具
  10. 一个数据包大小是多少k_算法交流: 6046 数据包的调度机制 【2.6基本算法之动态规划】...