Fox And Jumping(裴蜀定理 + dp)

D. Fox And Jumping

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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
inputCopy
3
100 99 9900
1 1 1
outputCopy
2
inputCopy
5
10 20 30 40 50
1 1 1 1 1
outputCopy
-1
inputCopy
7
15015 10010 6006 4290 2730 2310 1
1 1 1 1 1 1 10
outputCopy
6
inputCopy
8
4264 4921 6321 6984 2316 8432 6120 1026
4264 4921 6321 6984 2316 8432 6120 1026
outputCopy
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 张卡片,分别有 li 和 ci。在一条无限长的纸带上,你可以选择花 ci 的钱来购买卡片 i ,从此以后可以向左或向右跳 li 个单位。问你至少花多少元钱才能够跳到纸带上全部位置。若不行,输出 −1。

裴蜀定理

若 a,b 不全为零,则存在 x,y ,使得 ax+by=gcd(a,b)
本题中应用 : 若a,b互质,则a,和b通过x次和y次变换可以得到绝对值1

思路

先考虑两个数情况:想要跳到每一个格子上,必须使得这些数通过数次相加/减得出的绝对值为 1 ,进而想到了裴蜀定理。
推出:如果 a 与 b 互质,那么一定存在两个整数 x,y ,使得 ax+by=1。
若选择的卡牌通过数次相加或相减得出的绝对值为 1 ,那么这些数一定互质.
因此问题转化成:选择若干个数使得 gcd 是 1 的最小代价。
设f(i,j)为前i个数gcd = j的最小代价
f(i,j) = min(f[i - 1][j],f[i][k] + c[i]);
f[i][k] + c[i]满足gcd(k,l[i]) == j;

因为li数据范围是1e9,所以我们用map优化

AC代码

#include<bits/stdc++.h>
using namespace std;

map<int,int> mp;
int a[1005],b[1005];
int n;
int main(){
    cin>>n;
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    for(int i=1;i<=n;i++) scanf("%d",&b[i]);
    for(int i=1;i<=n;i++){
        for(pair<int,int> x:mp){
            int num=__gcd(a[i],x.first);
            int val=x.second+b[i];
            if(mp[num]==0) mp[num]=val;
            else mp[num]=min(mp[num],val);
        }
        if(mp[a[i]]==0) mp[a[i]]=b[i];
        else mp[a[i]]=min(mp[a[i]],b[i]);
    }
    if(mp[1]==0) puts("-1");
    else printf("%d\n",mp[1]);
    system("pause");
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值