510D. Fox And Jumping(dp加map来优化)

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
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.

题目思路:最开始我打算用区间DP来维护在某一个区间里面的使得GCD为1的最小费用,但是这里使用区间DP有对于一个区间的两边的数GCD为1,但是会算到他们中间的数的情况,这个情况我无法处理。
正确的思路是用dp来维护获得每一个GCD值的时候的最小费用,但是由于gcd的值过大,直接开数组是一定不行的,而且GCD的值一定是不连续的,这时候用MAP直接来存可能的GCD的值,可以做到边算边存。

具体思路见代码:

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <string>
#include <set>
#include <cmath>
#include <map>
using namespace std;
typedef long long ll;
const int maxn = 310;
const int maxn2= 6010;
const int inf =0x3f3f3f3f;
int n;
map<int,int>dp;
int c[maxn],t[maxn];
int gcd(int a,int b){
    if(b==0) return a;
    return gcd(b,a%b);
}
int main()
{
    while(~scanf("%d",&n)){
        for(int i=1;i<=n;i++){
            scanf("%d",&t[i]);
        }for(int i=1;i<=n;i++){
            scanf("%d",&c[i]);
        }
        dp.clear();
        dp[0]=0;
        for(int i=1;i<=n;i++){
            map<int,int>::iterator it=dp.begin();
            for(;it!=dp.end();it++){
                int a=(*it).first;
                int b=gcd(t[i],a);
                if(dp.count(b)){
                    dp[b]=min(dp[b],(*it).second+c[i]);
                }else{
                    dp[b]=(*it).second+c[i];
                }
            }
        }
        if(dp.count(1)) printf("%d\n",dp[1]);
        else printf("-1\n");
    }
    return 0;
}
发布了35 篇原创文章 · 获赞 25 · 访问量 813

猜你喜欢

转载自blog.csdn.net/qq_43816599/article/details/103947212
Fox