【Codeforces403B】【贪心】Upgrading Array

Upgrading Array

Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u

Description

You have an array of positive integers a[1], a[2], …, a[n] and a set of bad prime numbers b1, 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;
Let’s assume that p is the minimum prime divisor of s. If p is a good prime, then , otherwise .
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) and calculate the value g = GCD(a[1], a[2], …, a[r]).
Apply the assignments: , , …, .
What is the maximum beauty of the array you can get?

Input

The first line contains two integers n and m (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 space-separated integers a[1], a[2], …, a[n] (1 ≤ a[i] ≤ 109) — array a. The third line contains m space-separated integers b1, b2, …, bm (2 ≤ b1 < b2 < … < bm ≤ 109) — the set of bad prime numbers.

Output

Print a single integer — the answer to the problem.

Samples

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

Hint

Note that the answer to the problem can be negative.
The GCD(x1, x2, …, xk) is the maximum positive integer that divides each xi.

Source

Codeforces Round #236 (Div. 1)

贪心的题,先算出所有的gcd,然后从后向前贪心,如果当前除gcd有贡献就除,不然向前
然后不知道那个题解说的可以暴力分解,我就T了,最后还是加了个筛子

下面直接放代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<set>
#include<map>
#include<queue>
#include<algorithm>
#include<vector>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<stack>
#define INF 2100000000
#define ll long long
#define clr(x)  memset(x,0,sizeof(x))
#define clrmax(x)  memset(x,127,sizeof(x))

using namespace std;

inline int read()
{
    char c;
    int ret=0;
    while(!(c>='0'&&c<='9'))
        c=getchar();
    while(c>='0'&&c<='9')
    {
        ret=(c-'0')+(ret<<1)+(ret<<3);
        c=getchar();
    }
    return ret;
}

#define M 5005

int a[M],b[M],n,m,ans,g[M],di=1;
int p[M*2],pn,s[M*10];
map<int,int>c;

void get_prime()
{
    for(int i=2;i<=240;i++)
    {
        if(s[i])continue;
        for(int j=i+i;j<=50000;j+=i)
            s[j]=1;
    }
    for(int i=2;i<=50000;i++)
        if(!s[i])p[++pn]=i;
}

int gcd(int m,int n)
{
    return n==0?m:gcd(n,m%n);
}

bool in(int x)
{
    if(x<50000)
        if(s[x]==-1)return 1;
        else return 0;
    else if(c[x])return 1;
         else return 0;
}

int get_gong(int x)
{
    int ret=0;
    for(int i=1;i<=pn&&p[i]<=sqrt(x+0.5);i++)
    {
        while(x%p[i]==0)
        {
            if(in(p[i]))ret--;
            else ret++;
            x/=p[i];
        }
    }
    if(x!=1)
    {
        if(c[x])ret--;
        else ret++;
    }
    return ret;
}


int main()
{
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
    get_prime();
    n=read();m=read();
    for(int i=1;i<=n;i++)
        a[i]=read();
    for(int i=1;i<=m;i++)
    {
        b[i]=read();
        if(b[i]<50000)s[b[i]]=-1;
        c[b[i]]=1;
    }
    g[1]=a[1];ans+=get_gong(a[1]);
    for(int i=2;i<=n;i++)
    {
        g[i]=gcd(g[i-1],a[i]);
        ans+=get_gong(a[i]);
    }
    for(int i=n;i>=1;i--)
    {
        g[i]/=di;
        int t=get_gong(g[i]);
        if(t<0)
        {
            ans-=t*i;
            di*=g[i];
        }
    }
    cout<<ans;
    return 0;
}

大概就是这个样子,如果有什么问题,或错误,请在评论区提出,谢谢。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值