日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【CodeForces - 510D】Fox And Jumping(dp,stlmap,数论的性质)

發布時間:2023/12/10 编程问答 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【CodeForces - 510D】Fox And Jumping(dp,stlmap,数论的性质) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題干:

Fox Ciel is playing a game. In this game there is an infinite long tape with cells indexed by integers (positive, negative and zero). At the beginning she is standing at the cell 0.

There are also?n?cards, each card has 2 attributes: length?li?and cost?ci. If she pays?ci?dollars then she can apply?i-th card. After applying?i-th card she becomes able to make jumps of length?li, i. e. from cell?x?to cell?(x?-?li)?or cell?(x?+?li).

She wants to be able to jump to any cell on the tape (possibly, visiting some intermediate cells). For achieving this goal, she wants to buy some cards, paying as little money as possible.

If this is possible, calculate the minimal cost.

Input

The first line contains an integer?n?(1?≤?n?≤?300), number of cards.

The second line contains?n?numbers?li?(1?≤?li?≤?109), the jump lengths of cards.

The third line contains?n?numbers?ci?(1?≤?ci?≤?105), the costs of cards.

Output

If it is impossible to buy some cards and become able to jump to any cell, output -1. Otherwise output the minimal cost of buying such set of cards.

Examples

Input

3 100 99 9900 1 1 1

Output

2

Input

5 10 20 30 40 50 1 1 1 1 1

Output

-1

Input

7 15015 10010 6006 4290 2730 2310 1 1 1 1 1 1 1 10

Output

6

Input

8 4264 4921 6321 6984 2316 8432 6120 1026 4264 4921 6321 6984 2316 8432 6120 1026

Output

7237

Note

In first sample test, buying one card is not enough: for example, if you buy a card with length 100, you can't jump to any cell whose index is not a multiple of 100. The best way is to buy first and second card, that will make you be able to jump to any cell.

In the second sample test, even if you buy all cards, you can't jump to any cell whose index is not a multiple of 10, so you should output -1.

題目大意:

??給出n張卡片,每張帶有兩個權值l[i]和c[i]。在一條無限長的紙帶上,你可以選擇花c[i]的錢來購買卡片i,從此以后可以向左或向右跳l[i]個單位。購買其他卡片后,可以獲得更多的跳躍單位。先要求至少花多少元錢才能夠任意跳到紙帶上任意一個位置。若不行,輸出-1。

解題報告:

? ? 首先你要知道既然要可以跳到所有位置,充要條件是可以跳到1這個位置。換句話說,要想完成任務必須可以跳到1,而跳到1之后就可以跳到所有的位置,所以問題轉化為挑選k張卡片來湊出?他們的 GCD 為1。

? ? 然后我們用map進行滾動數組dp,,還有一個問題就是每次比較不用所有的都比較,只用管比較的兩個數的gcd就行了,,因為只要gcd可以出來了,其他的都可以出來。(之前cf有一個湊等差數列的也是運用這個思想)

? ?至于時間復雜度問題,可以證明1e9范圍內的每個數字,所具有的質因子個數<=9。所以在時間復雜度上是說的過去的。

AC代碼:

#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define ll long long #define pb push_back #define pm make_pair #define fi first #define se second using namespace std; const int MAX = 2e5 + 5; ll l[MAX]; ll c[MAX]; map<ll , ll > mp; map<ll , ll > :: iterator it; int main() {int n;cin>>n;for(int i = 1; i<=n; i++) scanf("%lld",l+i);for(int i = 1; i<=n; i++) scanf("%lld",c+i);mp[0]=0;for(int i = 1; i<=n; i++) {//if(mp.find(l[i]) == mp.end()) mp[l[i]]=c[i];for(it = mp.begin(); it!= mp.end(); ++it) {ll g = __gcd(it->fi,l[i]);//printf("%lld\n",mp[1]);if(mp.find(g) == mp.end()) {mp[g] = it->se + c[i];}else {mp[g] = min(mp[g],it->se + c[i]);//printf("%lld\n",mp[1]);}}} // if(mp.size() > 500000) printf("hahahaha");if(mp.find(1) == mp.end()) puts("-1");else printf("%lld\n",mp[1]); return 0 ;}

?

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的【CodeForces - 510D】Fox And Jumping(dp,stlmap,数论的性质)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。