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的方格子组成的.迷宫上有各种记号,这些记号表达着能走的方向.当且仅当 ...
随机推荐
- C基础 那些年用过的奇巧淫技
引言 - 为寻一颗明星 为要寻一颗明星 徐志摩 1924年12月1日<晨报六周年纪念增刊> 我骑著一匹拐腿的瞎马, 向著黑夜里加鞭:—— 向著黑夜里加鞭, 我跨著一匹拐腿的瞎马.// 我冲 ...
- unp.h
unp.h #ifndef _UNP_H_ #define _UNP_H_ #include <unistd.h> #include <stdio.h> #include &l ...
- 我爱我家:我为什么选择AppCan?
10年前,说起手机,大家联想到的词大概是:电话.短信.QQ.拍照,以及贪吃蛇等有限的几个小游戏.而如今,手机毫无疑问已经成为人们生活中不可或缺的部分.这是一个神奇的东西:通讯工具,外卖神器,游戏机,移 ...
- hdu 2034 人见人爱A-B
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2034 人见人爱A-B Description 参加过上个月月赛的同学一定还记得其中的一个最简单的题目, ...
- c++基础(一):数据类型和结构
1.map map<int, int> rankDict;//定义map rankDict[1] = 5; rankDict[2] = 6;//map赋值 int dictSize = r ...
- 未能加载文件或程序集“System.Web.Razor”或它的某一个依赖项。文件或目录损坏且无法读取。
“/”应用程序中的服务器错误. 未能加载文件或程序集“System.Web.Razor”或它的某一个依赖项.文件或目录损坏且无法读取. (异常来自 HRESULT:0x80070570) 说明: 执行 ...
- Linux虚拟机配置本地yum源
刚开始使用Linux,自己构建了一个Linux虚拟机之后,在使用yum install的时候,经常是出错,提示连接不上. 一直以为是自己构建的虚拟机的问题,后来在网上查找了一些资料,才发现:需要配置本 ...
- Data Developer Center > Learn > Entity Framework > Get Started > Loading Related Entities
Data Developer Center > Learn > Entity Framework > Get Started > Loading Related Entitie ...
- [原创]flexslider 中文文档 使用教程 参数手册
要改前人用的flexslider功能,但苦于找不到详细的文档教程,折磨了好久……(所以我才说不爱乱用插件) 为了福利下之后也苦于这个问题的人,我整理总结了下我找到的一些东西.可能没那么完善正确,欢迎在 ...
- Windows Live Writer教程及代码高亮工具
十分感谢六仙庵对于Windows Live Writer的教程,方便了编辑与发布,教程地址如下: http://www.cnblogs.com/liuxianan/archive/2013/04/13 ...