Codeforces 510D. Fox And Jumping By Assassin 暴力大法好
来源:互联网 发布:ACER windows激活 编辑:程序博客网 时间:2024/05/18 21:50
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 510D. Fox And Jumping By Assassin 暴力大法好
- Codeforces 510D.Fox And Jumping By Assassin 数论+状压dp
- Fox And Jumping - CodeForces 510 D
- codeforces 510D Fox And Jumping
- codeforces 510D Fox And Jumping
- codeforces 510D Fox and Jumping
- Codeforces 510D Fox And Jumping【数论+Dp+压缩空间】好题~
- codeforces 510D D. Fox And Jumping(dp+数论)
- codeforces 510D D. Fox And Jumping(dp+数论)
- Codeforce 558C. Amr and Chemistry &51nod 1483 化学变换 By Assassin 暴力大法好
- Codeforces Round #290 D. Fox And Jumping
- Codeforces D. Anton and Chess By Assassin
- 510D Fox And Jumping(dp+gcd)
- codefoces--510D. Fox And Jumping
- Codeforces Round #290 (Div. 2) D Fox And Jumping
- Codeforces Round #290 (Div. 2)D. Fox And Jumping
- Codeforces Round #290 (Div. 2)-D. Fox And Jumping
- CF D. Fox And Jumping
- 86. Partition List
- Bootstrap3 栅格系统-实例:从堆叠到水平排列
- JS判断浏览器,考虑版本
- Java基础-集合框架8 Arrays
- Thead 1 概述
- Codeforces 510D. Fox And Jumping By Assassin 暴力大法好
- matlab上使用libsvm
- 如何解决设计器无法选择授权问题
- node 登录拦截跳转,中间件使用和理解
- mybatis中的resultMap的使用
- Android 错误 :TextView中属性ellipsize的 值为start、middle可能会出现错误
- 瀑布流布局html+css+js分享
- 马士兵尚学堂Struts2笔记——以及工作机制
- C++编程入门系列之四十八(多态性:虚函数)