【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 1Output
2Input
5 10 20 30 40 50 1 1 1 1 1Output
-1Input
7 15015 10010 6006 4290 2730 2310 1 1 1 1 1 1 1 10Output
6Input
8 4264 4921 6321 6984 2316 8432 6120 1026 4264 4921 6321 6984 2316 8432 6120 1026Output
7237Note
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,数论的性质)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 首发AMD锐龙7 6800U!AOKZO
- 下一篇: 北方入汛来最强降雨开启!波及十余省 历史