zoukankan      html  css  js  c++  java
  • Codeforces Round #290 (Div. 2) D. Fox And Jumping dp

    D. Fox And Jumping

    题目连接:

    http://codeforces.com/contest/510/problem/D

    Description

    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.

    Sample Input

    3
    100 99 9900
    1 1 1

    Sample Output

    2

    Hint

    题意

    给你n个数,以及选择每个数的权值,要求你花费尽量少,使得选出来的数gcd = 1

    题解:

    直接暴力DP,dp[i]表示gcd为i需要的最小代价,dp[gcd(x,y)] = min(dp[gcd(x,y)],dp[x]+dp[y])

    代码

    #include<bits/stdc++.h>
    using namespace std;
    
    map<int,int> H;
    int gcd(int x,int y)
    {
        if(y==0)return x;
        return gcd(y,x%y);
    }
    #define maxn 350
    int a[maxn];
    int val[maxn];
    void updata(int x,int val)
    {
        if(H[x]==0)H[x]=1e9;
        H[x]=min(H[x],val);
    }
    int main()
    {
        int n;
        scanf("%d",&n);
        map<int,int>::iterator it;
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        for(int i=1;i<=n;i++)
            scanf("%d",&val[i]);
        for(int i=1;i<=n;i++)
        {
            updata(a[i],val[i]);
            for(it=H.begin();it!=H.end();it++)
                updata(gcd(it->first,a[i]),val[i]+it->second);
        }
        if(H[1]==0)return puts("-1");
        else printf("%d
    ",H[1]);
    }
  • 相关阅读:
    剑指offer 51.构建乘积数组
    剑指offer 50.数组中重复的数字
    股票价格涨跌预测—基于KNN分类器
    R语言-神经网络包RSNNS
    基于客户保持率的人口特性分析
    车辆碰撞位置间的关联性分析
    统计学的七大支柱[转载@谢益辉]
    AutoDesK演示项目部署
    自定义数字键盘组件化方案
    2020年大前端发展趋势
  • 原文地址:https://www.cnblogs.com/qscqesze/p/5079198.html
Copyright © 2011-2022 走看看