枚举质数,判断是否超过临界值。临界值就是将不是因子中不含这个素数的数的个数乘以x和y的较小值,是否小于当前最小值。
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> #define Max 1000001 #define MAXN 500005 #define MAXNUM 1000005 #define MOD 100000000 #define rin freopen("in.txt","r",stdin) #define rout freopen("1.out","w",stdout) #define Del(a,b) memset(a,b,sizeof(a)) using namespace std; typedef long long LL; const int INF = 0x3f3f3f3f; const LL LINF = 0x3f3f3f3f3f3f3f3fLL; int a[MAXN]; int sum[MAXNUM]; int vis[MAXNUM]; int n, x, y; int main() { //rin; scanf("%d%d%d", &n, &x, &y); Del(sum, 0); Del(vis, 0); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); sum[a[i]]++; } LL cnt = LINF; for (int i = 2; i <= 1000000; i++) { if (!vis[i]) { LL ans = sum[i]; for (int j = 2 * i; j <= 1000000; j += i) vis[j] = 1, ans += sum[j]; if ((n - ans) * min(x, y) < cnt) { LL tans = 0; for (int j = 0; j < n; j++) { if (a[j] % i) { LL t = a[j] % i; tans += min(1ll * x, 1ll * y * (i - t)); } } cnt = min(cnt, tans); } } } printf("%lld",cnt); return 0; }
时间: 2024-10-04 23:20:20