BZOJ2257: [Jsoi2009] bottles and fuel

Source: Internet
Author: User
Tags greatest common divisor

Description

Jyy has been thinking of returning to Earth as soon as possible, but his ship's fuel is not enough.
One day he went to the Martians to fuel, this time the Martians promised to jyy with the ship's bottles to change. Jyy
On the ship there are N bottles (1<=n<=1000), after consultation, the Martians as long as the K. Jyy
After handing K bottles to the Martians, the Martians used them to put some fuel into the jyy. All the bottles have no scale, only
The volume is marked at the bottle, and the volume of the I Bottle is VI (vi is an integer and satisfies the 1<=vi<=1000000000).
Martians are stingy, and they are not going to fill all the bottles with fuel. When they get the bottle, they run to the fuel.
The library tinker with a little bit of fuel to get it over with. Jyy, of course, knew they would come to this, so they knew the fire beforehand.
The specific contents of the Star-man Tinker. Martians in the fuel depot will only do the following 3 operations: 1, a bottle filled with fuel;
2. Pour all the fuel in a bottle back into the fuel bank, 3. Pour the fuel from bottle A to bottle B until the bottle B is full.
Or bottle A is empty. Losses in the process of fuel dumping can be neglected. The Martians are taking out the fuel, of course, that these operations can
The smallest positive volume obtained.
Jyy knows that for different bottle combinations, Martians may be forced to give different volumes of fuel. Jyy hope to find
To the optimal bottle mix, allowing the Martians to give as much fuel as they could.

Input

Line 1th: 2 integers n,k,
2nd.. N rows: 1 integers per line, and the integer for line i+1 is VI

Output

Only 1 lines, an integer, represents the maximum value of the fuel given by the Martians.

Sample Input3 2
3
4
4Sample Output4
HINT

By choosing the 2nd bottle and the first bottle, the Martians were forced to give 4 volumes.

Topic Portal I know how I got this problem. At first, if you give 3 or more bottles, then you can do this through the operation of the array: set A&GT;B+CB full, C full, then give a, a empty, left to others then do not do, hit a table found: This is related to their greatest common divisor that can be blind to the code as follows:
#include <cmath>#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>using namespacestd;intn,k;inta[1100000],tot,cnt;intMain () {CNT=0; scanf ("%d%d",&n,&k);  for(intI=1; i<=n;i++)    {        intx; scanf ("%d",&x);  for(intj=1; J<= (int) sqrt (x); J + +)            if(x%j==0)            {                if(j*j==x) a[++cnt]=J; Elsea[++cnt]=j,a[++cnt]=x/J; }} sort (a+1, A +1+CNT); inttot=1;  for(inti=cnt;i>=1; i--)    {        if(tot==k) {printf ("%d\n", A[i]); return 0; }            if(a[i]==a[i-1]) tot++; Elsetot=1; }    return 0;}

By_lmy

BZOJ2257: [Jsoi2009] bottles and fuel

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.