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 5Output1
-2
Input2
4 5
2 4 8 16
3 5 7 11 17Output2
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;
}
大概就是這個樣子,如果有什麼問題,或錯誤,請在評論區提出,謝謝。