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

题干:

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 ;}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440938.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

db2 linux 平台下迁移_Linux 下的 DB2数据库的迁移

前言&#xff1a;DB2 Universal Database™(DB2 UDB) 有一对非常有用的工具&#xff0c;可以帮助您实现这种跨平台的备份与恢复功能。 db2move 工具利用了 DB2 的数据移动工具(export 和 import 或 load)来移动数据库表。然而&#xff0c;由于数据库的内容远远不止于用户表&…

加拿大红河学院计算机专业,加拿大红河学院

加拿大红河学院是加拿大曼省最大的由曼省财政支持的公立高等专科学院&#xff0c;是曼省规模最大、课程最全面的学院&#xff0c;也是加拿大最大的学院之一。近年来&#xff0c;加拿大红河学院受到国人狂热追捧&#xff0c;申请人数逐年增长。加拿大红河学院专长于商业管理与国…

【CodeForces - 471C】MUH and House of Cards (思维,找规律)

题干&#xff1a; Polar bears Menshykov and Uslada from the zoo of St. Petersburg and elephant Horace from the zoo of Kiev decided to build a house of cards. For that theyve already found a hefty deck of n playing cards. Lets describe the house they want t…

哪个读书app可以导入txt_QQ阅读iphone版怎么导入电子书 三种手机QQ阅读器导入本地图书图文教程...

QQ阅读iphone版是一款比较方便的移动终端阅读软件&#xff0c;除了从电子书城下载或者购买电子书外&#xff0c;我们也可以把自己电脑中的电子书上传到QQ阅读软件中&#xff0c;不过果粉们都知道苹果手机没有文件管理器&#xff0c;无法使用qq阅读直接点击查看本地小说&#xf…

【CodeForces - 471B】MUH and Important Things (模拟,细节)

题干&#xff1a; Its time polar bears Menshykov and Uslada from the zoo of St. Petersburg and elephant Horace from the zoo of Kiev got down to business. In total, there are ntasks for the day and each animal should do each of these tasks. For each task, t…

大一计算机绩点3算什么水平,绩点只有3?我可以解释一下

放张图文无关压压惊最近开始申请学业奖学金了&#xff0c;大家开始计算自己的绩点&#xff0c;我也算了一下自己的成绩&#xff0c;结果是比3多一点点(如果没有算错的话)。我觉得这是一个比较合适的数字&#xff0c;没有比3小已经很满足了&#xff0c;毕竟学的并不好&#xff0…

不能用了 重装系统git_怎么用光盘重装系统?

身边没有U盘&#xff0c;电脑无法进入操作系统&#xff0c;只有一个系统光盘如何给电脑重装系统呢&#xff1f;受条件限制不能通过小白在线安装和U盘重装&#xff0c;今天教大家怎么用光盘重装系统吧。光盘重装系统准备工作1、保证电脑带有光驱功能&#xff0c;并且光驱处于正常…

【CodeForces - 472A】Design Tutorial: Learn from Math (tricks,思维,数论,打表)

题干&#xff1a; One way to create a task is to learn from math. You can generate some random math statement or modify some theorems to get something new and build a new task from that. For example, there is a statement called the "Goldbachs conject…

浙江经济职业技术学院计算机排名,浙江经济职业技术学院排名第几

关于浙江经济职业技术学院排名的问题考生问&#xff1a; 关于浙江经济职业技术学院的排名&#xff0c;我想抛给小编姐姐几个问题哦。一、浙江经济职业技术学院今年排名第几&#xff1f;对&#xff0c;指的是全国千余所专科院校当中的排名哦&#xff1b;二、浙江经济职业技术学院…

webform计算某几列结果_WebForm获取checkbox选中的值(几个简单的示例)

PS&#xff1a;最近在做权限管理这个模块&#xff0c;发现用checkbox的地方挺多的&#xff0c;于是写了个简单的例子&#xff0c;以供以后学习和使用。1.前端页面&#xff1a;张三李四王五赵六孙琦猪八2.后台方法&#xff1a;#region 获取从前端页面回传过来的 CheckBox 的值 v…

东明县计算机学校,东明县职业中等专业学校2021年招生信息

一、2021年东明县职业中等专业学校招生计划(一) 职教高考提前批招生计划学校代码专业名称招收人数学制学费备注说明863化学工艺40三年免学费从2022年起&#xff0c;山东省开始实施“职教高考”制度&#xff0c;采 取“文化素质职业技能”考试招生办法&#xff0c;职教高考的 本…

python去除图像光照不均匀_低光照环境下图像增强相关

Low-Light Image Enhancement via a Deep Hybrid Network [TIP2019]Underexposed Photo Enhancement using Deep Illumination Estimation[CVPR2019]---------Low-Light Image Enhancement via a Deep Hybrid Network [TIP2019]作者提出一个混合的网络来同时学习内容&#xff0…

【CodeForces - 523C】Name Quest (模拟)

题干&#xff1a; A Martian boy is named s — he has got this name quite recently from his parents for his coming of age birthday. Now he enjoys looking for his name everywhere. If he sees that he can obtain his name from some string by removing zero or mo…

w10计算机无法打印,老司机解答win10系统电脑无法打印的详细技巧

大家在使用电脑工作的时候会遇到win10系统电脑无法打印的问题&#xff0c;于是就有一些朋友到本站咨询win10系统电脑无法打印问题的解决步骤。解决win10系统电脑无法打印的问题非常简单&#xff0c;只需要你依照1、请确保打印机已打开并连接到你的电脑。 2、如果仍然无法工作&…

vb外部调用autocad_AutoCAD教程之图块的各种相关操作和概念

制图过程中&#xff0c;有时常需要插入某些特殊符号供图形中使用&#xff0c;此时就需要运用到图块及图块属性功能。利用图块与属性功能绘图&#xff0c;可以有效地提高作图效率与绘图质量。也是绘制复杂图形的重要组成部分。一、图块的特点图块是一组图形实体的总称&#xff0…

【CodeForces - 520C】DNA Alignment (快速幂,思维)

题干&#xff1a; Vasya became interested in bioinformatics. Hes going to write an article about similar cyclic DNA sequences, so he invented a new method for determining the similarity of cyclic sequences. Lets assume that strings s and t have the same l…

华北电力大学计算机考研大纲,2015年华北电力大学(保定)085211计算机技术考研大纲...

《529数据库原理及应用》一、考试内容范围&#xff1a;1.绪论1)数据管理技术的发展历史2)数据库管理系统的概念和功能3)数据库系统的特点4)数据库的三级模式结构、两级映像功能和数据独立性5)数据模型的组成要素和典型的数据模型6)概念模型的基本概念和概念模型的常用表示方法(…

echart 动画 饼图_echarts构建关系图,节点可收缩和展开,可添加点击事件

echarts下载及使用ECharts&#xff0c;一个使用 JavaScript 实现的开源可视化库&#xff0c;可以流畅的运行在 PC 和移动设备上&#xff0c;兼容当前绝大部分浏览器&#xff08;IE8/9/10/11&#xff0c;Chrome&#xff0c;Firefox&#xff0c;Safari等&#xff09;&#xff0c;…

【牛客 - 317C】小a与星际探索(背包dp 或 线性基)

题干&#xff1a; 小a正在玩一款星际探索游戏&#xff0c;小a需要驾驶着飞船从11号星球出发前往nn号星球。其中每个星球有一个能量指数pp。星球ii能到达星球jj当且仅当pi>pjpi>pj。 同时小a的飞船还有一个耐久度tt&#xff0c;初始时为11号点的能量指数&#xff0c;若小…

计算机及数控编程仿真软件exsl-win7,数控编程实验.doc

文档介绍&#xff1a;现代制造技术综合实验中心数控铣编程与仿真实验指导书1.实验目的:(1)通过上机实验巩固课堂所讲述的数控铣指令,掌握数控铣手工编程方法。(2)掌握EXSL-WIN7软件的编程及仿真等主要功能。2.实验设备或软件:计算机及数控编程仿真软件EXSL-WIN7。3.实验原理:根…