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
  1. 3
    100 99 9900
    1 1 1
Output
  1. 2
Input
  1. 5
    10 20 30 40 50
    1 1 1 1 1
Output
  1. -1
Input
  1. 7
    15015 10010 6006 4290 2730 2310 1
    1 1 1 1 1 1 10
Output
  1. 6
Input
  1. 8
    4264 4921 6321 6984 2316 8432 6120 1026
    4264 4921 6321 6984 2316 8432 6120 1026
Output
  1. 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真好用

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef int ll;
  4. inline ll read()
  5. {
  6. ll s=;
  7. bool f=;
  8. char ch=' ';
  9. while(!isdigit(ch))
  10. {
  11. f|=(ch=='-'); ch=getchar();
  12. }
  13. while(isdigit(ch))
  14. {
  15. s=(s<<)+(s<<)+(ch^); ch=getchar();
  16. }
  17. return (f)?(-s):(s);
  18. }
  19. #define R(x) x=read()
  20. inline void write(ll x)
  21. {
  22. if(x<)
  23. {
  24. putchar('-'); x=-x;
  25. }
  26. if(x<)
  27. {
  28. putchar(x+''); return;
  29. }
  30. write(x/);
  31. putchar((x%)+'');
  32. return;
  33. }
  34. #define W(x) write(x),putchar(' ')
  35. #define Wl(x) write(x),putchar('\n')
  36. const int N=;
  37. int n;
  38. struct Kapian
  39. {
  40. int Len,Cost;
  41. }Card[N];
  42. map<int,int>dp;
  43. inline int gcd(int a,int b)
  44. {
  45. return (!b)?a:(gcd(b,a%b));
  46. }
  47. int main()
  48. {
  49. int i;
  50. map<int,int>::iterator it;
  51. R(n);
  52. for(i=;i<=n;i++) R(Card[i].Len);
  53. for(i=;i<=n;i++) R(Card[i].Cost);
  54. dp.clear();
  55. dp[]=;
  56. for(i=;i<=n;i++)
  57. {
  58. for(it=dp.begin();it!=dp.end();it++)
  59. {
  60. int oo=it->first;
  61. int tmp=gcd(Card[i].Len,oo),CC=Card[i].Cost+it->second;
  62. if(dp[tmp]&&dp[tmp]<CC) continue;
  63. dp[tmp]=CC;
  64. }
  65. }
  66. if(!dp[]) puts("-1");
  67. else Wl(dp[]);
  68. return ;
  69. }
  70. /*
  71. Input
  72. 3
  73. 100 99 9900
  74. 1 1 1
  75. Output
  76. 2
  77.  
  78. Input
  79. 5
  80. 10 20 30 40 50
  81. 1 1 1 1 1
  82. Output
  83. -1
  84.  
  85. Input
  86. 7
  87. 15015 10010 6006 4290 2730 2310 1
  88. 1 1 1 1 1 1 10
  89. Output
  90. 6
  91.  
  92. Input
  93. 8
  94. 4264 4921 6321 6984 2316 8432 6120 1026
  95. 4264 4921 6321 6984 2316 8432 6120 1026
  96. Output
  97. 7237
  98. */

codeforces510D的更多相关文章

  1. CodeForces-510D

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

随机推荐

  1. msql分区

    确认mysql服务器是否支持分区表: show plugins; 如果能看到partition则表示能分区. HASH分区的特点: 根据MOD(分区键,分区数)的值把数据行存储到表的不同分区中 数据可 ...

  2. 微信公众号开发C#系列-7、消息管理-接收事件推送

    1.概述 在微信用户和公众号产生交互的过程中,用户的某些操作会使得微信服务器通过事件推送的形式通知到开发者在开发者中心处设置的服务器地址,从而开发者可以获取到该信息.其中,某些事件推送在发生后,是允许 ...

  3. 图像检索(3):BoW实现

    在上一篇文章中图像检索(2):均值聚类-构建BoF中,简略的介绍了基于sift特征点的BoW模型的构建,以及基于轻量级开源库vlfeat的一个简单实现. 本文重新梳理了一下BoW模型,并给出不同的实现 ...

  4. GoLang simple-project-demo-03

    变量的定义 package main import "fmt" func main() { var a = "initial" fmt.Println(a) v ...

  5. Springboot 系列(五)Spring Boot web 开发之静态资源和模版引擎

    前言 Spring Boot 天生的适合 web 应用开发,它可以快速的嵌入 Tomcat, Jetty 或 Netty 用于包含一个 HTTP 服务器.且开发十分简单,只需要引入 web 开发所需的 ...

  6. GOF23种设计模式概括

    GOF23种设计模式分为三种: 创建型模式[工厂方法模式]结构型模式[(类)适配器模式]行为型模式[ 解释器模式,模板方法模式]   创建型模式Creational Patterns抽象工厂模式abs ...

  7. Java web的一些面试题

    1.Tomcat 的优化经验 答:去掉对 web.xml 的监视,把 jsp 提前编辑成 Servlet. 有富余物理内存的情况,加大 tomcat 使用的 jvm 的内存 2.HTTP 请求的 GE ...

  8. 使用 MSIX 打包 DotNetCore 3.0 客户端程序

    如何你希望你的 WPF 程序能够以 Windows 的保护机制保护起来,不被轻易反编译的话,那么这篇文章应该能帮到你. 介绍 MSIX 是微软于去年的 Windows 开发者日峰会 上推出的全新应用打 ...

  9. 用mapreduce 处理气象数据集

    用mapreduce 处理气象数据集 编写程序求每日最高最低气温,区间最高最低气温 气象数据集下载地址为:ftp://ftp.ncdc.noaa.gov/pub/data/noaa 按学号后三位下载不 ...

  10. git创建分支并提交到远程分支

    来自:https://www.cnblogs.com/bluestorm/p/6252900.html 侵删 git branch(分支命令的使用http://hbiao68.iteye.com/bl ...