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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

【ZOJ - 2955】Interesting Dart Game(背包,结论,裴蜀定理,数论)

發布時間:2023/12/10 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【ZOJ - 2955】Interesting Dart Game(背包,结论,裴蜀定理,数论) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題干:

Recently, Dearboy buys a dart for his dormitory, but neither Dearboy nor his roommate knows how to play it. So they decide to make a new rule in the dormitory, which goes as follows:

Given a number?N, the person whose scores accumulate exactly to?N?by the fewest times wins the game.

Notice once the scores accumulate to more than?N, one loses the game.

Now they want to know the fewest times to get the score?N.

So the task is :?
Given all possible dart scores that a player can get one time and?N, you are required to calculate the fewest times to get the exact score?N.

Input

Standard input will contain multiple test cases. The first line of the input is a single integer?T?(1 <=?T?<= 50) which is the number of test cases. And it will be followed by?T?consecutive test cases.

Each test case begins with two positive integers?M(the number of all possible dart scores that a player can get one time) and?N. ?Then the following?M?integers are the exact possible scores in the next line.

Notice:?M?(0 <?M?< 100),?N?(1 <?N?<= 1000000000), every possible score is (0, 100).

Output

For each test case, print out an integer representing the fewest times to get the exact score?N.
If the score can't be reached, just print -1 in a line.

Sample Input

3 3 6 1 2 3 3 12 5 1 4 1 3 2

Sample Output

2 3 -1

題目大意:

? 給m(<=100)個數,每個數的范圍(1,100),一個n(<=1e9),問你能否通過組合這m個數來湊出n來。如果可以就輸出最小步數,反之輸出-1。

解題報告:

? n<=20000的時候可以直接背包,如果大于20000就先貪心最大的數,縮小下范圍到20000以內,然后直接讀表。

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 using namespace std; const int MAX = 20000 + 5; const ll INF = 9999999999; ll dp[MAX]; int a[MAX]; int main() {int t;cin>>t;while(t--) {int m;ll n;scanf("%d%lld",&m,&n);for(int i = 0; i<MAX; i++) dp[i] = INF;for(int i = 1; i<=m; i++) scanf("%d",a+i);sort(a+1,a+m+1);dp[0] = 0;for(int i = 1; i<=m; i++) {for(int j = a[i]; j<=20000; j++) {dp[j] = min(dp[j],dp[j-a[i]] +1);}}ll qq = a[m];if(n<=20000) {if(dp[n] == INF) printf("-1\n");else printf("%lld\n",dp[n]);continue;}ll sub = (n-19000) / a[m];n -= sub * a[m];if(dp[n] == INF) printf("-1\n");else printf("%lld\n",sub + dp[n]);}return 0 ; }

?

總結

以上是生活随笔為你收集整理的【ZOJ - 2955】Interesting Dart Game(背包,结论,裴蜀定理,数论)的全部內容,希望文章能夠幫你解決所遇到的問題。

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