Codeforces510D Fox And Jumping gcd, jumping
Question: n cards, skip length l [I], and cost c [I]
The start point is 0. If you select a card, you can jump to each point. The minimum cost is.
Idea: the essence is to select some cards. The length of gcd is 1 and the cost is the minimum. Then we map the gcd value with map and the corresponding minimum cost. Finally, output mp [1. (Ps: The card length may be the same.) For details, see the code:
/*************************************** * ****************** File name: codeforces510D. cpp author: kereo create time: ******************************** * ***********************/# include <iostream> # include <cstdio> # include <cstring> # include <queue> # include <set> # include <map> # include <vector> # include <stack> # include <cmath> # include <string> # include <algorithm> using namespace std; typedef Long ll; const int sigma_size = 26; const int N = 100 + 50; const int MAXN = 300 + 50; const int inf = 0x3fffffff; const double eps = 1e-8; const int mod = 100000000 + 7; # define L (x) (x <1) # define R (x) (x <1 | 1) # define PII pair <int, int> # define mk (x, y) make_pair (x), (y) int n; int a [MAXN], c [MAXN]; map <int, int> mp; // record that the gcd value is the smallest cost of x vector <int> vec; int gcd (int a, int B) {return B = 0? A: gcd (B, a % B);} int main () {while (~ Scanf ("% d", & n) {mp. clear (); vec. clear (); for (int I = 1; I <= n; I ++) scanf ("% d", & a [I]); for (int I = 1; I <= n; I ++) scanf ("% d", & c [I]); int res = a [1]; for (int I = 2; I <= n; I ++) res = gcd (res, a [I]); if (res! = 1) {printf ("-1 \ n"); continue;} for (int I = 1; I <= n; I ++) {if (mp [a [I] = 0) {mp [a [I] = c [I]; vec. push_back (a [I]);} else if (mp [a [I]> c [I]) mp [a [I] = c [I];} for (int I = 1; I <= n; I ++) {for (int j = 0; j <vec. size (); j ++) {int tmp = gcd (a [I], vec [j]); if (mp [tmp] = 0) {vec. push_back (tmp); mp [tmp] = mp [vec [j] + mp [a [I];} else if (mp [tmp]> mp [vec [j] + mp [a [I]) mp [tmp] = mp [vec [j] + mp [a [I] ;}} printf ("% d \ n", mp [1]);} return 0 ;}