题目链接:http://codeforces.com/contest/402/problem/D

题意:给出一个a串和素数串b 。f(1) = 0; p为s的最小素因子如果p不属于b , 否则 .

a串还可以进行这样的操作找一个r使得(1<=r<=n)g=gcd(a[1],a[2]......a[r]),然后再是a[1~r]/g。

题解:其实f的求和可以理解为num1(好的素因子)-num2(不好的素因子)。

然后就是对a操作的理解,a怎么样才需要进行这样的操作呢?只要g中不好的素因子大于好的素因子那么,

删掉这样的g就能增加f的总和。还有求除最小素因数的方法

for(int j = 2 ; j * j <= x ; j++) {//这个是快速的求法,为什么这么求可以自行理解

if(!prime[j])//prime表示是否是素数与处理一下

continue;

if(x % j)

continue;

bool flag = mmp[j];//定义map的mmp来判断j是否是坏素因数

while(x % j == 0) {

x /= j;

if(flag)

ans--;

else

ans++;

}

}

if(x > 1) {

if(mmp[x])

ans--;

else

ans++;

}

#include <iostream>
#include <cmath>
#include <cstdio>
#include <map>
#define inf 0X3f3f3f3f
using namespace std;
const int M = 1e5 + 10;
int a[5010] , b[5010];
int prime[M];
map<int , bool>mmp;
void IsPrime(){
prime[0] = prime[1] = 0;
prime[2] = 1;
for(int i = 3 ; i < M ; i++)
prime[i] = i % 2 == 0 ? 0 : 1;
int t = (int)sqrt(M * 1.0);
for(int i = 3 ; i <= t ; i++)
if(prime[i])
for(int j = i * i ; j < M ; j += 2 * i)
prime[j] = 0;
}
int gcd(int x , int y) {
return (y > 0) ? gcd(y , x % y) : x;
}
int main() {
int n , m;
scanf("%d%d" , &n , &m);
mmp.clear();
IsPrime();
for(int i = 0 ; i < n ; i++) {
scanf("%d" , &a[i]);
}
for(int i = 0 ; i < m ; i++) {
scanf("%d" , &b[i]);
mmp[b[i]] = true;
}
for(int i = n - 1 ; i >= 0 ; i--) {
int x = 0;
for(int j = 0 ; j <= i ; j++) {
x = gcd(x , a[j]);
}
int bad = 0 , good = 0;
int xx = x;
for(int l = 2 ; l * l <= x ; l++) {
if(!prime[l])
continue;
if(x % l)
continue;
bool flag = mmp[l];
while(x % l == 0) {
x /= l;
if(flag)
bad++;
else
good++;
}
}
if(x > 1) {
if(mmp[x])
bad++;
else
good++;
}
if(bad > good) {
for(int j = 0 ; j <= i ; j++) {
a[j] /= xx;
}
}
}
int ans = 0;
for(int i = 0 ; i < n ; i++) {
int x = a[i];
for(int j = 2 ; j * j <= x ; j++) {
if(!prime[j])
continue;
if(x % j)
continue;
bool flag = mmp[j];
while(x % j == 0) {
x /= j;
if(flag)
ans--;
else
ans++;
}
}
if(x > 1) {
if(mmp[x])
ans--;
else
ans++;
}
}
printf("%d\n" , ans);
return 0;
}

codeforces 402 D. Upgrading Array(数论+贪心)的更多相关文章

  1. Codeforces 402D Upgrading Array:贪心 + 数学

    题目链接:http://codeforces.com/problemset/problem/402/D 题意: 给你一个长度为n的数列a[i],又给出了m个“坏质数”b[i]. 定义函数f(s),其中 ...

  2. Codeforces G. Nick and Array(贪心)

    题目描述: Nick had received an awesome array of integers a=[a1,a2,…,an] as a gift for his 5 birthday fro ...

  3. Codeforces #402

    目录 Codeforces #402 Codeforces #402 Codeforces 779A Pupils Redistribution 链接:http://codeforces.com/co ...

  4. Tsinsen A1504. Book(王迪) 数论,贪心

    题目:http://www.tsinsen.com/A1504 A1504. Book(王迪) 时间限制:1.0s   内存限制:256.0MB   Special Judge 总提交次数:359   ...

  5. Codeforces 437C The Child and Toy(贪心)

    题目连接:Codeforces 437C  The Child and Toy 贪心,每条绳子都是须要割断的,那就先割断最大值相应的那部分周围的绳子. #include <iostream> ...

  6. Codeforces 442C Artem and Array(stack+贪婪)

    题目连接:Codeforces 442C Artem and Array 题目大意:给出一个数组,每次删除一个数.删除一个数的得分为两边数的最小值,假设左右有一边不存在则算作0分. 问最大得分是多少. ...

  7. Codeforces Round #546 (Div. 2) D 贪心 + 思维

    https://codeforces.com/contest/1136/problem/D 贪心 + 思维 题意 你面前有一个队列,加上你有n个人(n<=3e5),有m(m<=个交换法则, ...

  8. Codeforces Round #504 D. Array Restoration

    Codeforces Round #504 D. Array Restoration 题目描述:有一个长度为\(n\)的序列\(a\),有\(q\)次操作,第\(i\)次选择一个区间,将区间里的数全部 ...

  9. [CodeForces - 1225D]Power Products 【数论】 【分解质因数】

    [CodeForces - 1225D]Power Products [数论] [分解质因数] 标签:题解 codeforces题解 数论 题目描述 Time limit 2000 ms Memory ...

随机推荐

  1. wamp不显示文件图标

    wamp不显示文件图标 效果如下图 右键图片"在新的标签页打开图片"后会跳转到404页面,并显示The requested URL /icons/unknown.gif was n ...

  2. 如何选择合适的SSL证书类型

    网站安装SSL证书就可以将http升级为https加密模式,网站安装SSL证书因此成为一种趋势.如何为网站选择适合的SSL证书类型呢? SSL证书类型可分为2大类:1)按照验证方式分类2)按照支持域名 ...

  3. kubernetes API服务器的安全防护

    12.1.了解认证机制 启动API服务器时,通过命令行选项可以开启认证插件. 12.1.1.用户和组 了解用户: 分为两种连接到api服务器的客户端: 1.真实的人 2.pod,使用一种称为Servi ...

  4. 8.6 day27 网络编程 osi七层协议 Time模块补充知识 TCP协议

    Time模块补充知识 date和datetime区别是什么? date 就是年月日 datetime就是年月时时分秒 以下代码为什么会报错? import json from datetime imp ...

  5. java中String,StringBuffer,StringBuilder的区别

    String: 1,是字符串常量,一旦创建就不能修改.对于已经存在了的String对象的修改都是重新创建一个新的对象,然后把新的值保存进去. 2,String也是final类,不能被继承. 3,而且S ...

  6. Daily,一个入门级的 React Native 应用

    Daily,一个React-Native写的android app. 下拉刷新获取:图片.诗句.言语.音乐.乐评.雨声.知乎日报.历史上的今天. 可以说是一个入门级的React-Native应用. 项 ...

  7. 某团面试题:JVM 堆内存溢出后,其他线程是否可继续工作?

    转载注明:http://dwz.win/gHc 最近网上出现一个美团面试题:"一个线程OOM后,其他线程还能运行吗?".我看网上出现了很多不靠谱的答案.这道题其实很有难度,涉及的知 ...

  8. spring-boot-plus项目目录结构(六)

    spring-boot-plus项目目录结构 目录结构 bin:启动/重启命令脚本目录 logs:部署后记录日志目录 assembly:maven打包配置文件目录 java:源代码目录 resourc ...

  9. case when多条件

    SELECT label ,label3 ,lon_cen ,lat_cen ,lon3 ,lat3 ,antenna_height ,horizontal_angle ,CASE WHEN roun ...

  10. spring-boot-plus V1.2.2 发布,5 Minutes Finish CRUD

    更新日志 CHANGELOG [V1.2.2-RELEASE] 2019.08.26