# 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 $l_i$ and cost $c_i$. If she pays $c_i$ dollars then she can apply $i$-th card. After applying $i$-th card she becomes able to make jumps of length $l_i$, i.e. from cell $x$ to cell $(x-l_i)$ or cell $(x+l_i)$.
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.

## 算法分析

$$f_{(k, l_i)}=\min(f_k+c_i)$$

## 代码

#include <iostream>
#include <map>
using namespace std;
int n, l[301], c[301];
map<int, int> f;
int gcd(int a, int b) {
return b ? gcd(b, a % b) : a;
}
int main()
{
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> l[i];
}
for (int i = 1; i <= n; ++i) {
cin >> c[i];
}
f[0] = 0;
for (int i = 1; i <= n; ++i) {
for (map<int, int>::iterator iter = f.begin(); iter != f.end(); ++iter) {
int t = gcd(iter->first, l[i]);
if (!f.count(t)) f[t] = iter->second + c[i];
else f[t] = min(f[t], iter->second + c[i]);
}
}
if (!f[1]) cout << -1 << endl;
else cout << f[1] << endl;
return 0;
}


418 I'm a teapot