CF:402D - Upgrading Array 数分解为素数之积滴判定
来源:互联网 发布:金融大数据行业报告 编辑:程序博客网 时间:2024/06/04 19:19
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?
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.
Print a single integer — the answer to the problem.
5 24 20 34 10 102 5
-2
4 52 4 8 163 5 7 11 17
10
Note that the answer to the problem can be negative.
The GCD(x1, x2, ..., xk) is the maximum positive integer that divides each xi.
这题有点神啊……题意看了好久都没太理解,又研究队友那时过的代码,也好久了才懂什么意思,太神了,以前知道怎么分解一个数为素数之积,不过没写过代码,这题就作为模板了吧……
#include <iostream>#include <cstdio>#include <fstream>#include <algorithm>#include <cmath>#include <deque>#include <vector>#include <list>#include <queue>#include <string>#include <cstring>#include <map>#include <stack>#include <set>#define PI acos(-1.0)#define mem(a,b) memset(a,b,sizeof(a))#define sca(a) scanf("%d",&a)#define pri(a) printf("%d\n",a)#define MM 1000000000#define MN 5005#define INF 10000007typedef long long ll;using namespace std;int n,m,bad[MN],a[MN],is[MN*200],ans,prime[MN*2],gcd[MN];void get_prime(){ int i,j,k=sqrt(MM+0.5); for(i=2;i<=k;i++) if(!is[i]) { prime[ans++]=i; for(j=i*i;j<=k;j+=i) is[j]=1; }}int fenjie(int x){ int cnt=0,i,t,pos; for(i=0;i<ans&&x>1;i++) if(!(x%prime[i])) { t=0; while(!(x%prime[i])) x/=prime[i],t++; pos=lower_bound(bad,bad+m,prime[i])-bad; if(pos<m&&bad[pos]==prime[i]) cnt-=t; else cnt+=t; } if(x>1) //说明x比prime中最大的数还大,那么就在bad里面找了 { pos=lower_bound(bad,bad+m,x)-bad; if(pos<m&&bad[pos]==x) cnt--; else cnt++; } return cnt;}int main(){ ll sum=0; int i,j; get_prime(); scanf("%d%d%d",&n,&m,&a[0]); gcd[0]=a[0]; for(i=1;i<n;i++) sca(a[i]),gcd[i]=__gcd(gcd[i-1],a[i]); for(i=0;i<m;i++) sca(bad[i]); for(i=0;i<n;i++) sum+=fenjie(a[i]); for(i=n-1;i>=0;i--) if(gcd[i]>0) { int d=fenjie(gcd[i]); if(d<0) { for(j=0;j<=i;j++) a[j]/=gcd[i],gcd[j]/=gcd[i]; sum-=d*(i+1); } } cout<<sum<<endl; return 0;}
- CF:402D - Upgrading Array 数分解为素数之积滴判定
- CF 402D Upgrading Array
- CodeForces 402D Upgrading Array
- CodeForces 402D Upgrading Array
- CodeForces 402D Upgrading Array
- codeforces 402D D. Upgrading Array
- 数论之大数分解与素数判定
- codeforces 402D D. Upgrading Array(dp+数论)
- 将一个数分解为素数乘积
- cf D. Dima and Lisa (三素数定理_素数打表+判定)
- 大数分解与素数判定
- 将任意正整数分解为素数之积
- 大素数分解与素数判定
- 随机产生一个数并判定是否为素数
- 把一个数分解为素数的积(先不考虑位数溢出的情况)
- CF402D Upgrading Array(贪心+数学+线性素数筛)
- 【数论学习】奇素数分解为两个数平方和
- 素数判定(Miller_Rabin) & 大数分解(Pollard_Pho)
- Scrapy Callback传递多个参数方式
- CGPoint、CGSize和CGRect,CGPointMake,CGRectMake内联函数。
- ios 越狱包生成方法
- 插入排序--折半插入
- Wince下注册表的学习
- CF:402D - Upgrading Array 数分解为素数之积滴判定
- KALDI学习笔记(一)——About the Kaldi project
- 如何基于SQL Server表创建外部内容类型
- 第二周作业----求两个数的公约数
- 使用 HttpClient 和 HtmlParser 实现简易爬虫
- 【Matlab编程】Matlab及Java小时钟
- 电视数据初创企业Boxfish获700万美元融资
- Javascript encrypt and decrypt library CrytoJS
- 安装SqlServer2008时,出现:此计算机上安装了Microsoftstudio2008早期版本解决办法