D. Upgrading Array
time limit per test1 second
memory limit per test256 megabytes
You have an array of positive integersa1,a2,...,ana1, a2, ..., an and a set of bad prime numbers b1,b2,...,bmb1, b2, ..., bm. The prime numbers that do not occur in the set b are considered good. The beauty of array a is the sum , where function f(s) is determined as follows:
f(1)=0;f(1) = 0;
Let’s assume that pp is the minimum prime divisor of . If pp is a good prime, then , otherwise f(s)=f(sp−1)f(s)=f(sp−1).
You are allowed to perform an arbitrary (probably zero) number of operations to improve array a. The operation of improvement is the following sequence of actions:
Choose some number r(1≤r≤n)r(1 ≤ r ≤ n) and calculate the value g=GCD(a1,a2,...,ar)g=GCD(a1, a2, ..., ar).
Apply the assignments: a1=a1g,a2=a2g,...,ar=arga1=a1g,a2=a2g,...,ar=arg.
What is the maximum beauty of the array you can get?
Input
The first line contains two integers nn and (1≤n,m≤5000)(1 ≤ n, m ≤ 5000) showing how many numbers are in the array and how many bad prime numbers there are.
The second line contains nn space-separated integers — array a. The third line contains mm space-separated integers (2≤b1<b2<...<bm≤109)(2 ≤ b1 < b2 < ... < bm ≤ 109) — the set of bad prime numbers.
Output
Print a single integer — the answer to the problem.
Examples
input1
5 2
4 20 34 10 10
2 5
output1
-2
input2
4 5
2 4 8 16
3 5 7 11 17
output2
10
Note
Note that the answer to the problem can be negative.
The GCD(x1,x2,...,xk)GCD(x1,x2,...,xk) is the maximum positive integer that divides each xi.
Soluion
use g[i]g[i] denoting gcd(a1,a2,...,an)gcd(a1,a2,...,an),so we can find that array gg is a unincreasing array.
Also,must be a divisor of g[i−1]g[i−1].That’s obviously we do from the tail to the head is better(For the latter prefix gcdgcd do not impact the gcdgcd in the front).
if delete the prefix gcdgcd can add the beauty of the array,we divide it.
But how can we get the f[a[i]]f[a[i]]???
Clearly,Pretreatment comes.We can pretreat the primes less than max(a[i])‾‾‾‾‾‾‾‾‾√max(a[i]) and put the bad primes into hash,so we can get whether a prime is good or not in time O(1)O(1).
Code
#include <cstdio>
#include <algorithm>
#include <math.h>
#define N 5010
#define PSC 6974895
#define P 32000
#define DB printf("......\n")
using namespace std;
int hash[PSC], a[N], g[N], p[P], cnt;
bool pr[P];
int gcd(int x, int y) {
if (y == 0) return x;
return gcd(y, x % y);
}
inline void hash_ins(int x) {
int o = x % PSC;
while(hash[o] > 0) {
o++;
if (o == PSC) o -= PSC;
}
hash[o] = x;
}
inline bool hash_que(int x) {
int o = x % PSC;
while(hash[o] > 0) {
if (hash[o] == x) return 1;
o++;
if (o == PSC) o -= PSC;
}
return 0;
}
int main() {
int n, m, maxa = 0;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i) {
scanf("%d", &a[i]);
maxa = max(maxa, a[i]);
}
for (int i = 1; i <= m; ++i) {
int x;
scanf("%d", &x);
hash_ins(x);
}
maxa = (int)sqrt(maxa) + 1;
for (int i = 2; i * i <= maxa; ++i)
if (!pr[i])
for (int j = 2; j * i <= maxa; ++j)
pr[i * j] = 1;
for (int i = 2; i <= maxa; ++i)
if (!pr[i]) p[++cnt] = i;
int ans = 0;
for (int i = 1; i <= n; ++i) {
int o = a[i];
for (int j = 1; p[j] * p[j] <= a[i] && j <= cnt; ++j) {
int num = 0;
while(o % p[j] == 0) {
o /= p[j];
num++;
}
if (hash_que(p[j])) ans -= num;
else ans += num;
}
if (o > 1) {
if (hash_que(o)) ans--;
else ans++;
}
}
g[1] = a[1];
int i;
for (i = 2; i <= n; ++i) {
g[i] = gcd(g[i - 1], a[i]);
if (g[i] == 1) break;
}
int di = 1;
for (i = i - 1; i >= 1; --i) {
int l = g[i] / di, now = 0, o = l;
for (int j = 1; p[j] * p[j] <= l && j <= cnt; ++j) {
int num = 0;
while (o % p[j] == 0) {
o /= p[j];
num++;
}
if (hash_que(p[j])) now -= num;
else now += num;
}
if (o > 1) {
if (hash_que(o)) now--;
else now++;
}
if (now < 0) {
ans -= now * i;
di *= l;
}
}
printf("%d\n", ans);
return 0;
}
本文探讨了一种通过操作数组元素来最大化数组之美的算法。关键在于理解如何通过计算前缀最大公约数并利用质数的性质来优化数组。文章详细介绍了算法步骤,包括预处理小于最大数组元素平方根的所有质数,以及判断质数好坏的方法。
2002

被折叠的 条评论
为什么被折叠?



