Codeforces 510D. Fox And Jumping By Assassin 暴力大法好

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

题意:在一条无限长的线上,给你一些牌,牌的作用是花费一定钱买,使用时可以向左或者向右走一定值。问你在给定的这些牌中怎么选择可以使花费最小且可以到任意一点。

思路:
意图很明显,想到达任何一点,就需要卡片的组合可以使移动距离为1,变成了GCD(X1,X2,…Xn)==1的问题,那么这里用的暴力实现的,用到map的迭代器。因为卡牌的步数限定在1e9的范围内,这里每次更新已经可以到达的加上当前卡片可以到最小值花费,通过map的离散化扫描加上不断更细实现任意Xi求GCD的情况,最后观察dp[1]的情况即可。

还是暴力大法好,据说这题应该是数论+状压dp,准备学习一发

#include<bits/stdc++.h>
#define input freopen("input.txt","r",stdin)
using namespace std;
int value[330][2],n;
map<int,int>dp;
int gcd(int a,int b){
    if(b==0) return a;    //这样每次求GCD(A,0)的情况直接返回A,相当于只选择一种牌 
    return a%b==0?b:gcd(b,a%b);
}
int main(){
    input;
    int i,j;
    while(scanf("%d",&n)!=EOF){
        for(i=1;i<=n;i++){
            scanf("%d",&value[i][0]);
        }
        for(i=1;i<=n;i++){
            scanf("%d",&value[i][1]);
        }
        dp.clear();
        map<int,int> ::iterator it;
        dp[0]=0;        //初始化 ,让迭代器中有数值 
        for(i=1;i<=n;i++){
            int x=value[i][0];
            for(it=dp.begin();it!=dp.end();it++){
                int y=it->first;
                int m=gcd(x,y);
                if(dp[m]!=0&&dp[it->first]+value[i][1]>dp[m]) continue;  //当选择后情况不是最优解跳过 
                dp[m]=dp[it->first]+value[i][1];
            }
        }
        if(dp[1]==0) cout<<-1<<endl;
        else cout<<dp[1]<<endl;
    }
    return 0;
} 
您提供的链接是Codeforces的一个问题,问题编号为104377。Codeforces是一个知名的在线编程竞赛平台,经常举办各种编程比赛和训练。Gym是Codeforces的一个扩展包,用于组织私人比赛和训练。您提供的链接指向了一个问题的页面,但具体的问题内容和描述无法通过链接获取。如果您有具体的问题或需要了解关于Codeforces Gym的更多信息,请提供更详细的信息。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [http://codeforces.com/gym/100623/attachments E题](https://blog.csdn.net/weixin_30820077/article/details/99723867)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [http://codeforces.com/gym/100623/attachments H题](https://blog.csdn.net/weixin_38166726/article/details/99723856)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [CodeforcesPP:Codeforces扩展包](https://download.csdn.net/download/weixin_42101164/18409501)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值