codeforces510D
Fox And Jumping
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
3
100 99 9900
1 1 1
2
5
10 20 30 40 50
1 1 1 1 1
-1
7
15015 10010 6006 4290 2730 2310 1
1 1 1 1 1 1 10
6
8
4264 4921 6321 6984 2316 8432 6120 1026
4264 4921 6321 6984 2316 8432 6120 1026
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的更多相关文章
- CodeForces-510D
https://vjudge.net/problem/CodeForces-510D题目可以转化为花最小代价选一些数,然后这些数可以经过加减运算得到1或-1,不然1你就凑不出来,一旦凑出来1,其他的都 ...
随机推荐
- Linux文本三剑客超详细教程---grep、sed、awk
awk.grep.sed是linux操作文本的三大利器,合称文本三剑客,也是必须掌握的linux命令之一.三者的功能都是处理文本,但侧重点各不相同,其中属awk功能最强大,但也最复杂.grep更适合单 ...
- 痞子衡嵌入式:开启NXP-MCUBootUtility工具的HAB加密功能 - CST(中英双语)
1 Reason for enabling HAB encryption function 为什么要开启HAB加密功能 NXP-MCUBootUtility is a tool designed fo ...
- python 题库1
1. 生成一个1到50的大字符串,每个数字之间有个空格,例如1 2 3 4 ……50 解题思路: (1)声明一个空字符串变量用于保存生成的字符串 (2)使用for循环+range()函数生成一个1到5 ...
- jQuery中的prop()和attr()的区别
1.jQuery中的prop()和attr()的区别 prop()是在jQuery1.6版本之后才有的,在之前一直都是使用attr(), prop()修复了attr()的一些小bug. 2.推荐用法: ...
- Activiti(一) activiti数据库表说明
activiti介绍: activiti是一个业务流程管理(BPM)框架.它是覆盖了业务流程管理.工作流.服务协作等领域的一个开源的.灵活的.易扩展的可执行流程语言框架.开发人员可以通过插件直接绘画出 ...
- .net 笔试面试总结(1)
趁着在放假时候,给大家总结一点笔试面试上的东西,也刚好为年后跳槽做一点小积累. 下面的参考解答只是帮助大家理解,不用背,面试题.笔试题千变万化,不要梦想着把题覆盖了,下面的题是供大家查漏补缺用的,真正 ...
- es6 for of 循环
es6 新增了 for of 循环,只要继承了Iterator 接口的数据集合都可以使用 for of 去循环 for of 循环,统一数据集合的循环方法,解决了forEach循环的不能使用break ...
- informix存储过程笔记
一.存储过程概述 存储过程是一个用户定义的函数,由存储过程语句(SPL) 和一组SQL语句组成,以可以执行代码形式存储在数据库中,和表.视图.索引等一样,是数据库的一种对象. 存储过程语言SPL(St ...
- Spring boot 入门(四):集成 Shiro 实现登陆认证和权限管理
本文是接着上篇博客写的:Spring boot 入门(三):SpringBoot 集成结合 AdminLTE(Freemarker),利用 generate 自动生成代码,利用 DataTable 和 ...
- 【代码笔记】Web-CSS-CSS Align
一,效果图. 二,代码. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...