cf div2 236 D
1 second
256 megabytes
standard input
standard output
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 2
4 20 34 10 10
2 5
-2
4 5
2 4 8 16
3 5 7 11 17
10 贪心,从右往左扫gcd,若遇到好的质因子少于坏的质因子则可以更新使答案增加
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <set> using namespace std; #define maxn 40005
#define INF (1 << 30) bool prime[maxn];
int ele[maxn],a[],g[],b[];
int len = ,n,m,ans = ; int gcd(int x,int y) {
return y ? gcd(y,x % y) : x;
} int cal(int x) {
int sum = ;
for(int i = ; i * i <= x; ++i) {
if(x % i == ) {
int v,pos;
pos = lower_bound(b + ,b + m + ,i) - b;
v = b[pos] == i ? - : ;
while(x % i == ) {
sum += v;
x /= i;
}
}
} if(x != ) {
int pos = lower_bound(b + ,b + m + ,x) - b;
sum += b[pos] == x ? - : ;
}
return sum;
} void solve() {
//printf("cal = %d\n",cal(4));
for(int i = ; i <= n; ++i) {
ans += cal(a[i]);
} g[] = a[];
for(int i = ; i <= n; ++i) {
g[i] = gcd(g[i - ],a[i]);
} int t = ;
for(int i = n; i >= ; --i) {
int v;
if((v = cal(g[i] / t)) < ) {
//printf("g = %d v = %d\n",g[i],v);
ans += (-v) * i;
t *= g[i] / t;
}
} printf("%d\n",ans);
}
int main() { //freopen("sw.in","r",stdin); scanf("%d%d",&n,&m);
for(int i = ; i <= n; ++i) scanf("%d",&a[i]);
for(int i = ; i <= m; ++i) {
scanf("%d",&b[i]);
}
b[m + ] = INF; solve();
return ;
}
cf div2 236 D的更多相关文章
- cf div2 234 D
D. Dima and Bacteria time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- 离线dfs CF div2 707 D
http://codeforces.com/contest/707/problem/D 先说一下离线和在线:在线的意思就是每一个询问单独处理复杂度O(多少多少),离线是指将所有的可能的询问先一次都处理 ...
- cf div2 239 D
D. Long Path time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- cf div2 237 D
D. Minesweeper 1D time limit per test 2 seconds memory limit per test 512 megabytes input standard i ...
- cf div2 238 D
D. Toy Sum time limit per test 1 second memory limit per test 256 megabytes input standard input out ...
- cf div2 238 c
C. Unusual Product time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- cf div2 235 D
D. Roman and Numbers time limit per test 4 seconds memory limit per test 512 megabytes input standar ...
- cf div2 234 E
E. Inna and Binary Logic time limit per test 3 seconds memory limit per test 256 megabytes input sta ...
- CF div2 D BFS
http://codeforces.com/contest/676/problem/D 题目大意: 勇者去迷宫杀恶龙.迷宫是有n*m的方格子组成的.迷宫上有各种记号,这些记号表达着能走的方向.当且仅当 ...
随机推荐
- hdu1007
Have you ever played quoit in a playground? Quoit is a game in which flat rings are pitched at some ...
- Oracle private dblink和pubic dblink
DB : 11.2.0.3.0 Oracle DBLINK 创建分为private 和 public dblink,默认创建的为private ; private dblink 只有创建的schema ...
- RMAN - 备份异机恢复
OS: Oracle Linux Server release 5.7 DB: Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - ...
- 鲁棒性是 Robustness
鲁棒性是 Robustness 的音译,是指当系统受到不正常干扰时,是否还能保证主体功能正常运作.可参考 维基百科:http://zh.wikipedia.org/zh/ 鲁棒性 _( 计算机科学 ) ...
- django-pagination的使用
1.安装django-pagination 2.将文件夹pagination复制到项目的根目录下 3.修改settings: 1.将 'pagination.middleware.Pagination ...
- SQL中一种类似GUID值的函数实现
开发中会需要用到多列值组合成一个ID值的情况.比如做数据清洗的时候,一张表A有五列,分别是医院.科室.医生.职称.电话.面有许多重复的数据需要和另一个表B(和A列相同)做对比.清洗需要做两件事 ...
- Call C# in powershell
How to call C# code in powershell Powershell Command Add-Type usage of Add-Type we use Add-Type -Typ ...
- SharePoint 2010 用xsl文件定制列表样式
有时候我们不希望列表用默认的方式显示,要我们自定义的方式定制.其中有一种方式是使用xsl文件. 在AllItems.aspx页面中,列表是以webpart的形式显示在页面上的,webpart类型是Xs ...
- 阅读verilog程序总结
1.写程序先直接写出时钟信号 //-----------------产生串行时钟scl,为输入时钟的二分频--------- always@(negedge clk)//二分频表示频率小了,周期大了 ...
- 关于VS2010error RC2170 : bitmap file res\tmp1.bmp is not in 3.00 format
我们有时候向VS中的程序插入图片,会出现如下错误: 这是VS的一个bug,对于不能识别的资源,添加的时候,VS会弹出一个对话框让你填类型,这个类型其实是字符串表示,而不是像内置类型,例如整数. 解 ...