Fox And Jumping

CodeForces - 510D

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.

sol:首先容易发现题目就是让我们凑出一个1来,但是直接凑感觉很 蛋疼

然后有一个引理就是若干个数a,b,c...能凑出的最小数字就是gcd(a,b,c...),证明就不用了,自己XJByy一下就可以了,其实很容易证明,这样就可以轻松dp辣

Ps:STL真好用

#include <bits/stdc++.h>
using namespace std;
typedef int ll;
inline ll read()
{
ll s=;
bool f=;
char ch=' ';
while(!isdigit(ch))
{
f|=(ch=='-'); ch=getchar();
}
while(isdigit(ch))
{
s=(s<<)+(s<<)+(ch^); ch=getchar();
}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
if(x<)
{
putchar('-'); x=-x;
}
if(x<)
{
putchar(x+''); return;
}
write(x/);
putchar((x%)+'');
return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=;
int n;
struct Kapian
{
int Len,Cost;
}Card[N];
map<int,int>dp;
inline int gcd(int a,int b)
{
return (!b)?a:(gcd(b,a%b));
}
int main()
{
int i;
map<int,int>::iterator it;
R(n);
for(i=;i<=n;i++) R(Card[i].Len);
for(i=;i<=n;i++) R(Card[i].Cost);
dp.clear();
dp[]=;
for(i=;i<=n;i++)
{
for(it=dp.begin();it!=dp.end();it++)
{
int oo=it->first;
int tmp=gcd(Card[i].Len,oo),CC=Card[i].Cost+it->second;
if(dp[tmp]&&dp[tmp]<CC) continue;
dp[tmp]=CC;
}
}
if(!dp[]) puts("-1");
else Wl(dp[]);
return ;
}
/*
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
*/

codeforces510D的更多相关文章

  1. CodeForces-510D

    https://vjudge.net/problem/CodeForces-510D题目可以转化为花最小代价选一些数,然后这些数可以经过加减运算得到1或-1,不然1你就凑不出来,一旦凑出来1,其他的都 ...

随机推荐

  1. Redis哨兵机制

    Redis Sentinel物理结构图 角色 IP Port 别名 master 127.0.01 6379 主节点 slave-1 127.0.01 6380 slave-1节点 slave-2 1 ...

  2. java 并发多线程 锁的分类概念介绍 多线程下篇(二)

    接下来对锁的概念再次进行深入的介绍 之前反复的提到锁,通常的理解就是,锁---互斥---同步---阻塞 其实这是常用的独占锁(排它锁)的概念,也是一种简单粗暴的解决方案 抗战电影中,经常出现为了阻止日 ...

  3. SpringCloud系列——Bus 消息总线

    前言 SpringCloud Bus使用轻量级消息代理将分布式系统的节点连接起来.然后可以使用此代理广播状态更改(例如配置更改)或其他管理指令.本文结合RabbitMQ+GitHub的Webhook实 ...

  4. 解决IE6-IE7下li上下间距变大问题

    在IE6/7下li会向下产生大约2px的外边距 解决方法:li{vertical-align:top;}或者       li{vertical-align:bottom;} 解决问题!

  5. Web前端-CSS必备知识点

    Web前端-CSS必备知识点 css基本内容,类选择符,id选择符,伪类,伪元素,结构,继承,特殊性,层叠,元素分类,颜色,长度,url,文本,字体,边框,块级元素,浮动元素,内联元素,定位. 链接: ...

  6. harris角点检测的简要总结

    目录 1. 概述相关 2. 原理详解 1) 算法思想 2) 数学模型 3) 优化推导 3. 具体实现 1) 详细步骤 2) 最终实现 4. 参考文献 1. 概述相关 harris角点检测是一种特征提取 ...

  7. java:数据结构(二)栈的应用(括号匹配)

    一.什么是括号匹配: 括号匹配就是利用计算机辨别表达式里面的括号是否书写成功 例如: {()((a)) }这就是一个正确 (()()   这就是一个错误的 二.括号匹配的算法: 众所周知,括号分为花括 ...

  8. curl 命令-接口测试

    在linux/Unix 为代表的os上, 对后端进行测试, 模拟连接请求都会书写脚本 场景: 在Linux 上接口测试工具有ab, restClient, postman等, 最常用的方法是curl进 ...

  9. Redis笔记-集群搭建

    Redis单机版搭建上一篇已经基本介绍了,下面讨论Redis集群搭建方案和示例. 1.关于Redis常用的集群方案(三种): a.一主多从,如一个Master.两个Slave b.薪火相传,即集群中的 ...

  10. 基于nginx搭建yum源服务器

      1.首先关闭防护墙或者设置规则通过且关闭selinux 停止firewall systemctl stop firewalld 禁止firewall开机启动 systemctl disable f ...