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的方格子组成的.迷宫上有各种记号,这些记号表达着能走的方向.当且仅当 ...
随机推荐
- 仿SDWebImage
仿SDWebImage 目标:模拟 SDWebImage 的实现 说明:整体代码与之前博客上的演练代码的基本一致,只是编写顺序会有变化! 在模仿 SDWebImage 之前,首先需要补充一个知识点:N ...
- 刀哥多线程之gcd-01-sync&async
同步 & 异步 概念 同步 必须等待当前语句执行完毕,才会执行下一条语句 异步 不用等待当前语句执行完毕,就可以执行下一条语句 NSThread 中的 同步 & 异步 - (void) ...
- hdu 5199 Gunner
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=5199 简单题,stl水之... #include<algorithm> #include& ...
- Java动态替换InetAddress中DNS的做法简单分析1
在java.net包描述中, 简要说明了一些关键的接口. 其中负责networking identifiers的是Addresses. 这个类的具体实现类是InetAddress, 底层封装了Inet ...
- ios开发--常用宏定义(部分转)
1.release时,屏蔽log #if defined (DEBUG) && DEBUG == 1 #else #define NSLog(...) {}; #endif #if d ...
- 兼容sdk7&iOS7的issue解决小片段总结
ios7新增加的icon尺寸: 76 x 76:Size for iPad 2 and iPad mini (standard resolution) 120 x 120 :Size for iPho ...
- ASP.NET MVC中使用事务写法
一些项目中,会涉及到事务的写法,比如订单相关,订单成功,会涉及到产品的库存和账户金额的一些信息变动,当然,如果整个流程成功,那是没什么问题,关键是如果中间某一步骤出现bug了,那之前已执行的一些变动就 ...
- netstat用法
netstat - 显示网络连接,路由表,接口状态,伪装连接,网络链路信息和组播成员组. 总 netstat [address_family_options] [--tcp|-t] [--udp|-u ...
- npm ERR!无法安装任何包的解决办法
npm ERR! Windows_NT 6.1.7601npm ERR! argv "E:\\node\\\\node.exe" "E:\\node\\node_modu ...
- Netsharp快速入门(之10) 销售管理(插件、资源、业务建模)
作者:秋时 杨昶 时间:2014-02-15 转载须说明出处 第4章 销售模块开发 4.1 创建插件和资源 参考基础档案的开发 4.2 创建业务模型 Netsharp工具 ...