codeforces 402 D. Upgrading Array(数论+贪心)
题目链接: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(数论+贪心)的更多相关文章
- Codeforces 402D Upgrading Array:贪心 + 数学
题目链接:http://codeforces.com/problemset/problem/402/D 题意: 给你一个长度为n的数列a[i],又给出了m个“坏质数”b[i]. 定义函数f(s),其中 ...
- 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 ...
- Codeforces #402
目录 Codeforces #402 Codeforces #402 Codeforces 779A Pupils Redistribution 链接:http://codeforces.com/co ...
- Tsinsen A1504. Book(王迪) 数论,贪心
题目:http://www.tsinsen.com/A1504 A1504. Book(王迪) 时间限制:1.0s 内存限制:256.0MB Special Judge 总提交次数:359 ...
- Codeforces 437C The Child and Toy(贪心)
题目连接:Codeforces 437C The Child and Toy 贪心,每条绳子都是须要割断的,那就先割断最大值相应的那部分周围的绳子. #include <iostream> ...
- Codeforces 442C Artem and Array(stack+贪婪)
题目连接:Codeforces 442C Artem and Array 题目大意:给出一个数组,每次删除一个数.删除一个数的得分为两边数的最小值,假设左右有一边不存在则算作0分. 问最大得分是多少. ...
- Codeforces Round #546 (Div. 2) D 贪心 + 思维
https://codeforces.com/contest/1136/problem/D 贪心 + 思维 题意 你面前有一个队列,加上你有n个人(n<=3e5),有m(m<=个交换法则, ...
- Codeforces Round #504 D. Array Restoration
Codeforces Round #504 D. Array Restoration 题目描述:有一个长度为\(n\)的序列\(a\),有\(q\)次操作,第\(i\)次选择一个区间,将区间里的数全部 ...
- [CodeForces - 1225D]Power Products 【数论】 【分解质因数】
[CodeForces - 1225D]Power Products [数论] [分解质因数] 标签:题解 codeforces题解 数论 题目描述 Time limit 2000 ms Memory ...
随机推荐
- 解决:django.db.utils.OperationalError: unable to open database file
这是一个从GitHub上下载的,一个网站项目的源码.想要在自己的电脑上运行,期间过程相当曲折,不过至此终于是完成了. 1.安装过程: python2->virtualenv->django ...
- 配置多个JDK存在的问题与解决方案 (亲测可用)
安装多个JDK时的技巧 (亲测可用) 我的电脑本来是JDK8的,后来的想在不同的JDK版本下测试JDK的垃圾回收器. 一开始的的思路是,先安装JDK,为每个JDK配置自己的家目录,然后在想用哪个版本的 ...
- DesignPattern系列__02接口隔离原则
介绍 客户端不应该依赖它不需要的接口,即一个类对另一个类的依赖应该建立在最小接口上. Demo引入 先来看一张图: interface MyInterface { void operation1(); ...
- 【Java例题】2.6 三角形的面积
6. 用海伦公式计算三角形的面积. 设边长分别时a,b和c,s=(a+b+c)/2, 则三角形面积area=sqrt(s*(s-a)*(s-b)*(s-c)). package study; impo ...
- 控制台基于Quartz.Net组件实现定时任务调度(一)
前言: 你曾经需要应用执行一个任务吗?比如现在有一个需求,需要每天在零点定时执行一些操作,那应该怎样操作呢? 这个时候,如果你和你的团队是用.NET编程的话,可以考虑使用Quartz.NET调度器.允 ...
- Axure 使用 简单入门
1.Axure 简介 Axure是快速原型工具,简单来说就是把自己的web或app想法快速的展示出来的工具.具体信息百科:https://baike.baidu.com/item/axure%20rp ...
- io流处理文件夹复制功能(java代码)
拷贝某个目录下得所有文件拷指定位置 思想归纳 首先我们需要做的先获取到资源文件夹路径,这里我们先在程序中写死,然后我们还需要一个目标文件夹就是你需要拷贝到哪里.有了这两个文件夹我就可以进行复制了 然后 ...
- python数据类型图解
- (十八)c#Winform自定义控件-提示框
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- AUTOCAD二次开发-----删除一个图层里面的所有对象
https://blog.csdn.net/aasswwe/article/details/40899759 private void Test() { // 获取当前文档和数据库 Document ...