bzoj 2257 (JSOI 2009) Bottles and Fuel

Description

Jyy always wanted to return to Earth as soon as possible, but unfortunately his spaceship ran out of fuel.
One day he went to the Martian again to ask for fuel, this time the Martian agreed and asked Jyy to exchange it with the bottle on the spaceship. There are a total of N bottles (1<=N<=1000) on jyy
's spacecraft. After negotiation, the Martians only need K of them. After jyy
gave the K bottles to the Martian, the Martian used them to fill jyy with some fuel. All bottles have no scale,
only the capacity is marked on the bottle mouth, the capacity of the ith bottle is Vi (Vi is an integer, and satisfies 1<=Vi<=1000000000).
Martians are stingy, and they don't fill all their bottles with fuel. After they got the bottle, they would go to the fuel
depot and fiddle around with it and get a little bit of fuel to cross. Of course jyy knew that they would do this, so he knew in advance what
the Martians were tinkering with. Martians can only do the following three operations in the fuel depot: 1. Fill a certain bottle with fuel;
2. Pour all the fuel in a certain bottle back into the fuel depot; 3. Pour the fuel from bottle a to the bottle b, until bottle b is full
or bottle a is empty. Losses during fuel dumping are negligible. The fuel that the Martian took out was, of course, the smallest positive volume that could be
obtained .
jyy knew that Martians might be forced to give different volumes of fuel for different bottle combinations. jyy wants to find
the optimal combination of bottles so that Martians give as much fuel as possible.
Input

Line 1: 2 integers N,K,
Line 2..N: 1 integer per line, the integer in line i+1 is Vi
Output

Only 1 line, an integer, representing the maximum fuel value given by Martians.

Sample Input

3 2

3

4

4
Sample Output

4

HINT

Choosing the 2nd bottle and the 1st bottle, the Martian is forced to give a capacity of 4 volumes.

This question seems difficult, but after simulation and observation, according to Pei Shu's theorem, it will be found that it is actually asking for the maximum gcd of k bottles, decomposing all the bottles by prime factor, and then sweeping from large to small after sorting. , if there are consecutive k factors, is the answer.

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>

using namespace std;
const int MAXN = 1005;

int n,k,v[MAXN],a[20000010],cnt;

inline void COUNT(int x){
    if(x==2 || x==3) {
        a[++cnt]=x;
        return;
    }
    for(register int i=2;i<=sqrt(x);i++)
        if(x%i==0){
            a[++cnt]=i;
            if(i*i!=x) a[++cnt]=x/i;
        }
    a[++cnt]=x;
}

int main(){
    scanf("%d%d",&n,&k);
    for(register int i=1;i<=n;i++){
        scanf("%d",&v[i]);
        if(v[i]>1)
            COUNT(v[i]);
    }   
    sort(a+1,a+1+cnt);
//  for(register int i=1;i<=cnt;i++) cout<<a[i]<<endl;
    int x=a[cnt];
    int sum=1;
    if(k==1) printf("%d\n",x);
    else{
        while(--cnt){
            if(a[cnt]==x)   sum++;
            else{
                sum=1;
                x=a[cnt];   
            }
            if(sum==k) {
                printf("%d\n",x);
                return 0;
            }
        }printf("1\n");     
    }

    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325192502&siteId=291194637