【Codeforces403B】【贪心】Upgrading Array
来源:互联网 发布:传感器网络的目标跟踪 编辑:程序博客网 时间:2024/05/16 01:52
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 5005int 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;}
大概就是这个样子,如果有什么问题,或错误,请在评论区提出,谢谢。
- 【Codeforces403B】【贪心】Upgrading Array
- Codeforces 403B Upgrading Array [贪心] [数论]
- CF402D Upgrading Array(贪心+数学+线性素数筛)
- CodeForces 402D Upgrading Array
- CF 402D Upgrading Array
- CodeForces 402D Upgrading Array
- CodeForces 402D Upgrading Array
- codeforces 402D D. Upgrading Array
- Codeforces Round #236 (Div. 2) D. Upgrading Array
- Codeforces Round #236 (Div. 2)D题 Upgrading Array ;dp
- codeforces 402D D. Upgrading Array(dp+数论)
- LeetCode330. Patching Array【贪心】
- leetcode330:Patching Array(贪心)
- CF:402D - Upgrading Array 数分解为素数之积滴判定
- 2015年ALPC暑期代码能力练习I D Upgrading Array
- HZAUoj 1016: Array C 【贪心】
- codeforces721D Maxim and Array(贪心)
- Leetcode330 - Patching Array(贪心)
- codevs 1006
- 三路循迹的编程方法
- mysql error 1366
- 一个软件专业的学习者
- HDU ACM 11 2026 首字母变大写
- 【Codeforces403B】【贪心】Upgrading Array
- Caffe在Windows的VS2013运行方法
- 文件的操作2-----读写文件/移动文件读写指针位置/重回文件头部、fprintf()函数、freopen函数
- NodeJs安裝教程
- 项目演示总结
- c!!#获!!!!取!!计算!!!!机!!中!!已!安装!的!!字!!!体
- logstash multiline
- 项目中各种后缀名的文件们
- 根据前序和中序建立二叉树