[Bzoj 2257] [JSOI2009] bottle and fuel (GCD)

Source: Internet
Author: User
Tags gcd

[Bzoj 2257] [JSOI2009] bottle and fuel 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.

Solution

1. Consider that the Martians will only pour between the bottles, will not fall off half or other magical means, so they can get out of the fuel volume should be a linear combination of the volume of all bottles;

2. Through the bezout theorem we can easily prove that for any number of integers, they can be combined with the smallest positive integer is their gcd, so the answer is to ask for all the number of the approximate maximum value of k;

3. Considering that the counter array is not open, use the array to save all the approximate, and finally sort over, O (n) to find the optimal solution.

Code
  #include <iostream> #include <cstdio> #include <cmath> #include <cstring> #include    <algorithm>using namespace Std;int n,m,divs[10000001],tot,cnt=1;inline Int rd () {int x=0;    Char C=getchar ();    while (!isdigit (c)) C=getchar ();        while (IsDigit (c)) {x= (x<<1) + (x<<3) + (c^48);    C=getchar (); } return x;} inline int mx (int x,int y) {return x>y?x:y;} inline BOOL cmp (int x,int y) {return x>y;}    void Calc (int v) {int temp=sqrt (v); for (int i=1;i<temp;++i) if (! (            v%i)) {divs[++tot]=i;        divs[++tot]=v/i; } if (! (        v%temp)) {divs[++tot]=temp;    if (v/temp!=temp) divs[++tot]=v/temp;    }}int Main () {n=rd (); M=rd ();    for (int i=1;i<=n;++i) Calc (rd ());    Sort (divs+1,divs+1+tot,cmp);            for (int i=2;i<=tot;++i) if (Divs[i]!=divs[i-1]) {if (cnt>=m) {printf ("%d", divs[i-1]); return 0;}        Cnt=1;    } else ++cnt; return 0;} 

GCD basic Knowledge section can refer to my essay: http://www.cnblogs.com/COLIN-LIGHTNING/p/8371664.html

[Bzoj 2257] [JSOI2009] bottle and fuel (GCD)

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.