The main topic: given n bottles, choose K, you can guide the oil casually, asked to choose K bottles can be exported to the minimum value of the maximum number of oil
First of all, K bottles can be exported to the minimum oil must be K bottle capacity of the largest common factor so the problem is converted to the n number of the choice K to maximize the maximum common factor
Find out all the factor of n number to sort out the largest and the output with the number of occurrences greater than or equal to K
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define M 1600100using namespace Std;int n,k;int factors[m],tot;void get_factors (int x) {int i;for (i=1;i*i<x;i++) if (x%i==0) Factors[++tot]=i,factors[++tot]=x/i;if (i*i==x) factors[++tot]=i;} int main () {int i,x;cin>>n>>k;for (i=1;i<=n;i++) {scanf ("%d", &x); Get_factors (x);} Sort (factors+1,factors+tot+1); int cnt=0;for (i=tot;i;i--) {if (factors[i]!=factors[i+1]) cnt=0;++cnt;if (cnt==k) { printf ("%d\n", Factors[i]); return 0;}}}
Bzoj 2257 JSOI2009 Bottle and fuel number theory