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]);
    }
  • 相关阅读:
    html之长文本框置顶
    Red Hat Enterprise Linux Server 6.5安装GCC 4.9.2
    精通正则表达式
    解决UNION ALL合并两个结果集后排序的问题
    ELK搭建日志管理系统记录
    Spring Boot使用@ConfigurationProperties 读取自定义的properties的方法
    maven配置profile,按指定环境打包
    java自定义标签,tld文件,获取数据字典的值
    JAVA实现RSA签名、验签
    jquery.validate.js中的remote用法
  • 原文地址:https://www.cnblogs.com/qscqesze/p/5079198.html
Copyright © 2011-2022 走看看