Codeforces510D Fox And Jumping gcd, jumping

Source: Internet
Author: User

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 ;}

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.