CodeForces 402D-Upgrading Array

D. Upgrading Array

time limit per test1 second
memory limit per test256 megabytes

You have an array of positive integers a1,a2,...,an a 1 ,   a 2 ,   . . . ,   a n and a set of bad prime numbers b1,b2,...,bm b 1 ,   b 2 ,   . . . ,   b m . 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 p p is the minimum prime divisor of s. If p p is a good prime, then f(s)=f(sp+1), otherwise f(s)=f(sp1) f ( s ) = f ( s p − 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 = G C D ( a 1 ,   a 2 ,   . . . ,   a r ) .

  • Apply the assignments: a1=a1g,a2=a2g,...,ar=arg a 1 = a 1 g , a 2 = a 2 g , . . . , a r = a r g .
    What is the maximum beauty of the array you can get?

Input
The first line contains two integers n n 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 n n space-separated integers a[1],a[2],...,a[n](1a[i]109) — array a. The third line contains m m space-separated integers b1,b2,...,bm (2b1<b2<...<bm109) ( 2   ≤   b 1   <   b 2   <   . . .   <   b m   ≤   10 9 ) — 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) G C D ( x 1 , x 2 , . . . , x k ) is the maximum positive integer that divides each xi.

Soluion
use g[i] g [ i ] denoting gcd(a1,a2,...,an) g c d ( a 1 , a 2 , . . . , a n ) ,so we can find that array g g 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 gcd g c d do not impact the gcd g c d in the front).
if delete the prefix gcd g c d 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]) m a x ( 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;
}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CodeForces - 616D是一个关于找到一个序列中最长的第k好子段的起始位置和结束位置的问题。给定一个长度为n的序列和一个整数k,需要找到一个子段,该子段中不超过k个不同的数字。题目要求输出这个序列最长的第k好子段的起始位置和终止位置。 解决这个问题的方法有两种。第一种方法是使用尺取算法,通过维护一个滑动窗口来记录\[l,r\]中不同数的个数。每次如果这个数小于k,就将r向右移动一位;如果已经大于k,则将l向右移动一位,直到个数不大于k。每次更新完r之后,判断r-l+1是否比已有答案更优来更新答案。这种方法的时间复杂度为O(n)。 第二种方法是使用枚举r和双指针的方法。通过维护一个最小的l,满足\[l,r\]最多只有k种数。使用一个map来判断数的种类。遍历序列,如果当前数字在map中不存在,则将种类数sum加一;如果sum大于k,则将l向右移动一位,直到sum不大于k。每次更新完r之后,判断i-l+1是否大于等于y-x+1来更新答案。这种方法的时间复杂度为O(n)。 以上是两种解决CodeForces - 616D问题的方法。具体的代码实现可以参考引用\[1\]和引用\[2\]中的代码。 #### 引用[.reference_title] - *1* [CodeForces 616 D. Longest k-Good Segment(尺取)](https://blog.csdn.net/V5ZSQ/article/details/50750827)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Codeforces616 D. Longest k-Good Segment(双指针+map)](https://blog.csdn.net/weixin_44178736/article/details/114328999)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值