D. Upgrading Array
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

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?

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.

Sample test(s)
input
5 2
4 20 34 10 10
2 5
output
-2
input
4 5
2 4 8 16
3 5 7 11 17
output
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的更多相关文章

  1. cf div2 234 D

    D. Dima and Bacteria time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  2. 离线dfs CF div2 707 D

    http://codeforces.com/contest/707/problem/D 先说一下离线和在线:在线的意思就是每一个询问单独处理复杂度O(多少多少),离线是指将所有的可能的询问先一次都处理 ...

  3. cf div2 239 D

    D. Long Path time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  4. cf div2 237 D

    D. Minesweeper 1D time limit per test 2 seconds memory limit per test 512 megabytes input standard i ...

  5. cf div2 238 D

    D. Toy Sum time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  6. cf div2 238 c

    C. Unusual Product time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  7. cf div2 235 D

    D. Roman and Numbers time limit per test 4 seconds memory limit per test 512 megabytes input standar ...

  8. cf div2 234 E

    E. Inna and Binary Logic time limit per test 3 seconds memory limit per test 256 megabytes input sta ...

  9. CF div2 D BFS

    http://codeforces.com/contest/676/problem/D 题目大意: 勇者去迷宫杀恶龙.迷宫是有n*m的方格子组成的.迷宫上有各种记号,这些记号表达着能走的方向.当且仅当 ...

随机推荐

  1. EmguCV学习——简单使用

    关于EmguCV我就不多说了,是对应于OpenCV的一套net库. 公司是视觉方面的业务,我又不会c++(好想会啊,正在学习中).由于各种需求,自己觉得对c++不是特别感冒,所以选用了net下的ope ...

  2. MIFARE系列8《D8M1.exe》

    软件名:D8M1.exe 更新时间:2014.06.28 操作系统:windowAll 外部设备:D8读卡器 D8M1可以对MIFARE块读写操作,支持1K,4K.检验KEY后返回SAK,QTAQ,U ...

  3. 说说oracle中的面向对象与面向集合

    这一篇算是对近期自己学习的一个心得总结 一.oracle的面向对象 SQL是面向集合的这个大家都知道,但是不可否认现在的oracle中有很多地方都体现着面向对象的思维.(这也算是各大语言殊途同归的一个 ...

  4. Redis 四:存储类型之有序集合

    有序集合似乎更大的操作是由于加了一个叫做“分子”的东西 事实上就好像普通的数据,只是为这个数据加了一个纯数字的标识, 通过操作这些标识来得到我们想要的数据! 分子可以是整形,也可以是双精度浮点型: = ...

  5. DeviceOne开发HelloWord

    http://www.cnblogs.com/wjiaonianhua/p/5278061.html http://www.jb51.net/article/75693.htm 2015 年 9 月 ...

  6. hdu 4585 Shaolin

    原题链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=46093 #include<algorithm> #in ...

  7. 在JAVA中使用JSONObject生成json

    JSON是一种轻量级的数据交换格式,在现在的web开发中,是非常常见的.在没有方便的工具之前,我们或许会使用拼字符串的形式来生成json数组,今天我们使用一个json-lib.jar包来为我们实现生成 ...

  8. 使用IC框架开发跨平台App的备忘录123

    1,关于图标与启动屏幕 icon.png 192x192splash.png 2208x2208 将这两个图片放在resources目录下,在终端执行:ionic resources --iocn - ...

  9. Android facebook-android-sdk-3.5.2 FQL

    android平台下使用Facebook 3.5.2的SDK实践 步骤总结: 1.引用SDK 在Facebook官网下载最新版的SDK,解压. 打开Eclipse, 'File' -> 'Imp ...

  10. [无人值守安装操作系统]_FTP+TFTP+DHCP+Kickstart+PXE_中遇到的错误

    本篇记录的是实验  http://www.cnblogs.com/snsdzjlz320/p/5629127.html  过程出现的问题 问题一: PXE-E11:ARP timeout TFTP c ...