1. 程式人生 > >BZOJ2257 [Jsoi2009]瓶子和燃料 【裴蜀定理】

BZOJ2257 [Jsoi2009]瓶子和燃料 【裴蜀定理】

clas ++ pair code 現在 lse HA print make

題目鏈接

BZOJ2257

題解

由裴蜀定理我們知道,若幹的瓶子如此傾倒最小能湊出的是其\(gcd\)
現在我們需要求出\(n\)個瓶子中選出\(K\)個使\(gcd\)最大
每個數求出因數排序即可

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<map>
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define mp(a,b) make_pair<int,int>(a,b) #define cls(s) memset(s,0,sizeof(s)) #define cp pair<int,int> #define LL long long int using namespace std; const int maxn = 1005,maxm = 1000005,INF = 1000000000; inline int read(){ int out = 0,flag = 1; char c = getchar(); while (c < 48 || c > 57){if
(c == ‘-‘) flag = -1; c = getchar();} while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();} return out * flag; } int n,K; int p[maxm],pi; void sp(int x){ p[++pi] = x; for (int i = 2; i * i <= x; i++) if (x % i == 0){ p[++pi] = i; if
(i * i != x) p[++pi] = x / i; } } int main(){ n = read(); K = read(); REP(i,n) sp(read()); sort(p + 1,p + 1 + pi); int ans = 1,cnt = 0; for (int i = 1; i <= pi; i++){ if (p[i] != p[i - 1]){ if (cnt >= K) ans = max(ans,p[i - 1]); cnt = 1; } else cnt++; } if (cnt >= K) ans = max(ans,p[pi]); printf("%d\n",ans); return 0; }

BZOJ2257 [Jsoi2009]瓶子和燃料 【裴蜀定理】