题目链接: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. Linux下zookeeper下载与安装教程

    原文连接:(http://www.studyshare.cn/blog-front//blog/details/1169/0)一.下载 官网下载:点这里  百度网盘下载:点这里 官网下载图示: jav ...

  2. Spring 源码注解

    一.@Retention可以用来修饰注解,是注解的注解,称为元注解.     Retention注解有一个属性value,是RetentionPolicy类型的,Enum RetentionPolic ...

  3. sql server 2008 外键的级联操作

    问题提出:现在我有三张表,学生Student,课程Course,成绩SC 1.  学生表Student,主键是学号Sno 2.  课程Course,主码是课程号Cno 3.  成绩SC,主码是Sno和 ...

  4. Asp.Net Core 发布到 Docker(Linux Centos 虚拟机,使用Dockerfile)

    实践一下 Asp.Net Core (基于.net core 2.2)部署到Docker 一.准备工作: 1. 使用Virtualbox创建一个Centos系统的虚拟机,并安装docker和vim 2 ...

  5. Install eclipse ns3 in ubuntu 14.04

    1. NS3 install 参考NS3 tutorial即可. 2.eclipse 2.1下载 下载地址:http://www.eclipse.org/downloads/              ...

  6. [转载]ActiveMQ实现负载均衡+高可用部署方案

    转载于 http://www.open-open.com/lib/view/open1400126457817.html 一.架构和技术介绍 1.简介 ActiveMQ 是Apache出品,最流行的, ...

  7. Redis——基础使用

    Redis总体介绍: Redis特性 Redis(REmote DIctionary Server),是一个开源的内存数据库 持久化:RDB.AOF 主备复制 丰富的数据结构 Lua脚本.事务 Red ...

  8. 洛谷 P2657 [SCOI2009]windy数

    题意简述 求l~r之间不含前导零且相邻两个数字之差至少为2的正整数的个数 题解思路 数位DP 代码 #include <cstdio> #include <cstring> # ...

  9. go 学习笔记之值得特别关注的基础语法有哪些

    在上篇文章中,我们动手亲自编写了第一个 Go 语言版本的 Hello World,并且认识了 Go 语言中有意思的变量和不安分的常量. 相信通过上篇文章的斐波那契数列,你已经初步掌握了 Go 语言的变 ...

  10. 配置Oracle透明网关用以连接 SQLServer经验总结

    一.情景介绍   业务中设计两个不同的系统,系统1和系统2,两个系统分别使用的是Oracle和SQLServer数据库.现需要在系统1的数据库中直接查询系统2数据库的数据.即在Oracle中执行SQL ...