CodeForces 402D-Upgrading Array

本文探讨了一种通过操作数组元素来最大化数组之美的算法。关键在于理解如何通过计算前缀最大公约数并利用质数的性质来优化数组。文章详细介绍了算法步骤,包括预处理小于最大数组元素平方根的所有质数,以及判断质数好坏的方法。

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 s. If pp is a good prime, then f(s)=f(sp+1), otherwise f(s)=f(sp1)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(1rn)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 m (1n,m5000)(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 a[1],a[2],...,a[n](1a[i]109) — array a. The third line contains mm space-separated integers b1,b2,...,bm (2b1<b2<...<bm109)(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,g[i]must be a divisor of g[i1]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;
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值