Codeforces Round #290 (Div. 2)D. Fox And Jumping

//题目要求出现任意整数,换句话说就是是的出现cd为1
//所以题目的意思就是求( ⊙ o ⊙ )使得gcd为1所需要花费的最小的钱
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf -0x3f3f3f3f
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
typedef long long ll;
map<ll,ll>  mp;
ll gcd(ll a,ll b){
    if(a<b)
        swap(a,b);
    return b==0?a:gcd(b,a%b);
}
ll l[301];
ll c[301];

int main(){
    int n;
    while(scanf("%d",&n)!=EOF){
        for(int i=1;i<=n;i++)
            scanf("%I64d",&l[i]);
        for(int i=1;i<=n;i++)
            scanf("%I64d",&c[i]);
        mp.clear();
        mp[0]=0;
        map<ll,ll>::iterator it;
        for(int i=1;i<=n;i++)   //保证这个数只出现一次
            for(it=mp.begin();it!=mp.end();it++){       //遍历
                ll a=gcd(l[i],it->first);
                if(mp.count(a))                         //出现重复则取较小值
                    mp[a]=min(mp[a],it->second+c[i]);
                else
                    mp[a]=it->second+c[i];              //否则直接插入
            }
        if(!mp[1])
            printf("-1\n");
        else
            printf("%I64d\n",mp[1]);
    }
    return 0;
}

你可能感兴趣的:(Codeforces Round #290 (Div. 2)D. Fox And Jumping)