CF510 D - Fox And Jumping(GCD问题)
生活随笔
收集整理的這篇文章主要介紹了
CF510 D - Fox And Jumping(GCD问题)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
題意:n個數(shù)中任取幾個數(shù),使之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 }?
轉載于:https://www.cnblogs.com/usedrosee/p/4386259.html
總結
以上是生活随笔為你收集整理的CF510 D - Fox And Jumping(GCD问题)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 模拟 Codeforces Round
- 下一篇: SharePoint2010沙盒解决方案