CF510 D - Fox And Jumping(GCD问题)

题意:n个数中任取几个数,使之GCD = 1,且权值最小

思路:暴力。。 这种方法挺好。。 学习了。。

 1 #include<iostream>

 2 #include<cstdio>

 3 #include<cstdlib>

 4 #include<cstring>

 5 #include<string>

 6 #include<queue>

 7 #include<algorithm>

 8 #include<map>

 9 #include<iomanip>

10 #include<climits>

11 #include<string.h>

12 #include<numeric>

13 #include<cmath>

14 #include<stdlib.h>

15 #include<vector>

16 #include<stack>

17 #include<set>

18 #define FOR(x, b, e)  for(int x=b;x<=(e);x++)

19 #define REP(x, n)     for(int x=0;x<(n);x++)

20 #define INF 1e7

21 #define MAXN 100010

22 #define maxn 1000010

23 #define Mod 1000007

24 #define N 110

25 using namespace std;

26 typedef long long LL;

27 

28 int main()

29 {

30     int n, tmp;

31     cin >> n;

32     vector<int> l, c;

33     REP(i, n) {

34         cin >> tmp;

35         l.push_back(tmp);

36     }

37     REP(i, n) {

38         cin >> tmp;

39         c.push_back(tmp);

40     }

41     map<int, int> Gcd;

42     Gcd[0] = 0;

43     REP(i, n) {

44         for (map<int, int> :: iterator it = Gcd.begin();it != Gcd.end(); ++ it) {

45             int g = __gcd((*it).first,l[i]);

46             Gcd[g] = min(Gcd[g] ? Gcd[g]:1<<29, (*it).second + c[i]);

47         }

48     }

49     if (Gcd[1] == 0) 

50         cout << -1 << endl;

51     else 

52         cout << Gcd[1] << endl;

53     return 0;

54 }

 

你可能感兴趣的:(ping)