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,其他的都 ...
随机推荐
- WinDbg调试C#技巧,解决CPU过高、死锁、内存爆满
软件安装 安装问题:执行 .loadby sos clr 命令无效 解决办法: .load C:\Windows\Microsoft.NET\Framework64\v4.0.30319\SOS.dl ...
- Golang struct结构
结构struct Go中的struct与C中的struct非常相似,并且Go没有class,代替了class的位置,但并没有代替class的功能 使用type struct{} 定义结构,名称遵循可见 ...
- C#的Lock
有时候在编写线程并发的时候需要考虑异步和同步的问题.有些资源只能是一个线程访问,其他的线程在这个线程没有释放资源前不能访问.类似于操作系统中临界资源的访问.C#Lock包裹的代码块具有原子操作的特性( ...
- Biorhythms(poj1006+中国剩余定理)
Biorhythms Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 117973 Accepted: 37026 Des ...
- 重写Ext中的typeOf函数
重写Ext中的typeOf函数来解决Ext JS中typeOf对字符串对象.元素节点.文本节点.空白文本节点判断并不准确的问题 重写的typeOf函数使用自己实现的TypeOf函数2中的代码 测试代码 ...
- Android Studio教程06-布局,监听器以及基本控件
目录 2. 监听器 3. 布局 3.1. 布局分类 (1). Linear Layout (2). Relative Layout (3). ListView (4). Grid View 4. 其他 ...
- Glide的 java.lang.RuntimeException: Expected instanceof GlideModule, but found:X.GlideModule@2e4554f
问题一 在添加过混淆规则后,App打包的时候,发现报错了 java.lang.RuntimeException: Expected instanceof GlideModule, but found: ...
- Puppeteer学习之小试牛刀
最近有了写文章的动力了,一方面是受到了很多前辈们的启示,另一方面也是为了记录下来更好地学以致用.闲言少叙,先说说Puppeteer是什么. Puppeteer是一个node库,提供了一些用来操作Chr ...
- MySQL, XE7使用FireDAC连接MySQL数据库
发现使用DBExpress进行MySQL连接老是有莫名其妙的问题,直接改为FireDAC 在上一篇的DataSnap服务框架程序中,将连接的数据库由MSSQL改为本文的MySQL 使用的MySQL数据 ...
- 斐波那契数列(C#)
斐波那契数,亦称之为斐波那契数列(意大利语: Successione di Fibonacci),又称黄金分割数列.费波那西数列.费波拿契数.费氏数列,指的是这样一个数列:1.1.2.3.5.8.13 ...