#290 (Div.2) D. Fox and jumping

Source: Internet
Author: User
Tags greatest common divisor

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.