1. Title Description: Click to open the link
2. Problem-Solving ideas: The use of scanning and maintenance solutions. According to test instructions, the largest convention to be able to walk to all the squares that must be the number of cards selected is 1, which is well understood. Because ax+by=1 means that if you have a x and b y, you can step up the number of steps 1. In this way, you only need to use map to store the minimum cost for all the conventions. At the beginning of the base[0]=0, the next step is to scan the n number from the back and then update the base for each of the largest conventions is the corresponding minimum cost.
3. Code:
#define _crt_secure_no_warnings #include <iostream> #include <algorithm> #include <string> #include <sstream> #include <set> #include <vector> #include <stack> #include <map> #include < queue> #include <deque> #include <cstdlib> #include <cstdio> #include <cstring> #include < cmath> #include <ctime> #include <functional>using namespace std; #define N 300+10map<int, Int>base ; int L[n], c[n];int n;int gcd (int a, int b) {return b = = 0? a:gcd (b, a%b);} int main () {//freopen ("T.txt", "R", stdin), while (~SCANF ("%d", &n)) {base.clear (); (int i = 0; i < n; i++) scanf ("% D ", &l[i]); for (int i = 0; i < n; i++) scanf ("%d ", &c[i]); Map<int, int>::iterator it;base[0] = 0;for (int i = 0; I < n;i++)//examine each card for (it = Base.begin (); it = Base.end (); it++)//update each greatest common divisor corresponding minimum cost {int gmin = It->first, SUMV = it ->second;int g = gcd (Gmin,l[i]), if (Base.count (g))//exists greatest common divisor base[g] = min (Base[g], SUMV + c[i]);//update its minimumCost else base[g] = SUMV + c[i];//does not exist for this greatest common divisor}if (!base.count (1)) printf (" -1\n"), Else printf ("%d\n", Base[1]);} return 0;}
#290 (Div.2) D. Fox and jumping