Bzoj 2257[jsoi2009] Number theory/bezout theorem for bottles and fuels

Source: Internet
Author: User
Tags gcd greatest common divisor

Topic

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 A word test instructionsoriginally just want to learn a simple bezout theorem, did not expect to encounter this problem. At first glance look familiar ah, this is not learning sister out of an almost identical problem (escape. Say that question when said Bezout theorem, at that time also understand, just after class forget Orz, but finally said solve this problem with sub-block (??? so let's take a look at the bezout theorem first.from the point of view of Oi, bezout theorem:for any integer A, B, there is a pair of integers x, y to make ax+by=gcd (A, b)proof part can go left to Baidu ...So what does that have to do with the subject? Read a lot of log blog, all the reasons for seeking gcd. But I was too konjac to see that it was obvious. first look at the case of two bottles, if the capacity of two bottles is a, B, then two bottles of the volume of Ax+by, and because GCD (A, B) |ax+by,so the minimum volume is gcd (A, B). Spread to n bottles, which is the gcd of the capacity of the n bottles. that's not what I'm talking about, another god Ben said.code to make a mess (vector is not good, only in plain way
1#include <cmath>2#include <algorithm>3#include <cstdio>4#include <vector>5 using namespacestd;6 intn,k,ans=1, step;7 intcnt[1500000];8 9 BOOLcmpintAintb)Ten { One     returnA>b; A } -  - voidApartintx) the { -      for(intI=1; i*i<=x;i++) -     { -         if(x%i==0) +         { -cnt[++step]=i; +             if(x/i!=i) cnt[++step]=x/i; A         } at     }  - } - intMain () - { -scanf"%d%d",&n,&k); -      for(intI=1; i<=n;i++) in         { -             intx=0;  toscanf"%d",&x); + apart (x); -         } theSort (cnt+1, cnt+step+1, CMP); *          for(intI=1; i<=step;i++) $         {Panax Notoginseng             if(cnt[i]==cnt[i-1]) -             { theans++; +                 if(ans==k) A                 { theprintf"%d", Cnt[i]); +                     return 0; -                   } $             } $             Elseans=1; -         } -      the     return 0; -}
View Code

more about the bezout theorem

In number theory, the Bezout theorem is a theorem about greatest common divisor (or the largest convention). The bezout theorem derives its name from the French mathematician , the Bezout, which illustrates the linear graph-loss equation (called the bezout equation) for any integers a, B, and their greatest common divisor d, on unknowns x and y:

  Ax + by = m

There is a solution if and only if m is a multiple of D. When the Bezout equation has a solution, there must be infinitely many integer solutions, each set of solutions x,y is called bezout number, can be obtained by the method of division.

For example, themaximum common factor for 12 and 42 is 6, then the equation 12x + 42y = 6 has a solution. In fact there are ( -3) x12 + 1x42 = 6 and 4x12 + ( -1) x42 = 6.

In particular, the equation ax + by = 1 has a solution when and only if the integers a and B are interdependent.

The Bezout equation can also be used to define greatest common divisor:D is actually the smallest positive integer that can be written in Ax + by form. The essence of this definition is the concept of "ideal" in the whole ring . Therefore, there are corresponding bezout theorems for polynomial integral loops.

Bzoj 2257[jsoi2009] Number theory/bezout theorem for bottles and fuels

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.