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

https://codeforces.com/contest/510/problem/D
很明显的裴蜀定理

int a[maxn], c[maxn];

inline int gcd(int a, int b) {
	return b == 0 ? a : gcd(b, a % b);
}

int main(){
	int n; scanf("%d", &n);
	for (int i = 0; i < n; ++i)	scanf("%d", &a[i]);
	for (int i = 0; i < n; ++i)	scanf("%d", &c[i]);

	map<int, int> mp;
	for (int i = 0; i < n; ++i) {	
		for (auto it : mp) {
			int tmp = gcd(it.first, a[i]);
			mp[tmp] = mp[tmp] ? min(mp[tmp], it.second + c[i]) : it.second + c[i];
		}
		mp[a[i]] = mp[a[i]] ? min(mp[a[i]], c[i]) : c[i];
	}

	printf("%d\n", (mp[1] ? mp[1] : -1));

	return 0;
}

posted @ 2021-03-05 16:15  wansheking  阅读(29)  评论(0)    收藏  举报