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 ...
随机推荐
- maven项目引用错误 和项目结构问题
解决办法: 鼠标右键 maven ---->update prroject Configuration 然后 maven clean maven install
- javascript+jQuery补充
一.jQuery事件绑定 <div class='c1'> <div> <div class='title'>菜单一</div> <div cla ...
- 爬虫环境搭建及 scrapy 启动
创建虚拟环境 C:\Users\Toling>mkvirtualenv article 这个是普通的创建虚拟环境,但是实际开发中可能会使用python2或python3所以我们需要指定开发的环境 ...
- C# 10分钟完成百度语音技术(语音识别与合成)——入门篇
我们已经讲了人脸识别(入门+进阶).图片识别(入门).下面是链接: C# 10分钟完成百度人脸识别——入门篇 C# 30分钟完成百度人脸识别——进阶篇(文末附源码) C# 10分钟完成百度图片提取文字 ...
- WPF:window设置单一开启
方法一: Window window = new Window();window.ShowDialog; 方法二: 设置一个判断窗口打开状态的全局控制变量 private bool i ...
- java课堂 动手动脑3
(1) 该函数没有赋初值再就是如果类提供一个自定义的构造方法,将导致系统不在提供默认的构造方法. (2) public class test { public static void main(Str ...
- JAVA jobs
Java岗位1, SpringMVC, spring, mybaits2, 高并发编程3, mysql或者oracle SQL调优及函数,存储过程,JOB调度
- ES 26 - 通过partial update局部更新索引文档 (partial update增量修改原理)
目录 1 什么是partial update 1.1 全量修改文档的原理 1.2 修改指定field的思路 1.3 partial update的优势 1.4 partial update的使用 2 ...
- Knative 基本功能深入剖析:Knative Serving 之服务路由管理
导读:本文主要围绕 Knative Service 域名展开,介绍了 Knative Service 的路由管理.文章首先介绍了如何修改默认主域名,紧接着深入一层介绍了如何添加自定义域名以及如何根据 ...
- 用 Python 分析上网记录,发现了很多不可思议的事
摘要:分享个 Python 神工具. 长时间使用浏览器会积累大量浏览器历史记录,这些是很隐私的数据,里面甚至可能有一些不可描述的网站或者搜索记录不想让别人知道. 不过,我们自己可能会感兴趣,天天上 ...