codeforces Upgrading Array
思路:对于每个数分解质因子然后记录每一个质因子的个数,对与在b中出现的质因子就减去1,否则加1,求出总的,然后从后面一次对它们的最大公约数,然后判断除以最大公约数之后,改变量是不是变化,求最大值,变化量为负值的话减去。
#include <cstdio>
#include <cstring>
#include <map>
#include <set>
#include <algorithm>
using namespace std; set<int>q;
int n,m;
int a[],b[];
int g[]; int gcd(int a,int b)
{
return b==?a:gcd(b,a%b);
} int Get_num(int x)
{
int ans=;
map<int,int>p;
for(int j=; j*j<=x; j++)
{
if(x%j==)
{
while(x%j==)
{
p[j]++;
x/=j;
}
}
}
if(x>) p[x]++;
map<int,int>::iterator it;
for(it=p.begin(); it!=p.end(); it++)
{
if(q.find(it->first)==q.end()) ans+=it->second;
else ans-=it->second;
}
return ans;
} int main()
{ scanf("%d%d",&n,&m);
for(int i=; i<=n; i++)
{
scanf("%d",&a[i]);
}
for(int j=; j<=m; j++)
{
scanf("%d",&b[j]);
q.insert(b[j]);
}
int ans=;
for(int i=; i<=n; i++)
{
ans+=Get_num(a[i]);
}
for(int i=; i<=n; i++)
{
if(i==) g[i]=a[i];
else g[i]=__gcd(g[i-],a[i]);
}
int c=;
for(int i=n; i>=; i--)
{
int h=g[i]/c;
int s=Get_num(h);
if(s<)
{
ans-=s*i;
c*=h;
}
}
printf("%d\n",ans);
return ;
}
codeforces Upgrading Array的更多相关文章
- Codeforces 494D Upgrading Array
http://codeforces.com/contest/494/problem/D 题意:给一个数组,和一个坏质数集合,可以无数次地让1到i这些所有数字除以他们的gcd,然后要求Σf(a[i])的 ...
- Codeforces 402D Upgrading Array:贪心 + 数学
题目链接:http://codeforces.com/problemset/problem/402/D 题意: 给你一个长度为n的数列a[i],又给出了m个“坏质数”b[i]. 定义函数f(s),其中 ...
- codeforces 402 D. Upgrading Array(数论+贪心)
题目链接:http://codeforces.com/contest/402/problem/D 题意:给出一个a串和素数串b .f(1) = 0; p为s的最小素因子如果p不属于b , 否则 . a ...
- CodeForces 402D Upgrading Array (数学+DP)
题意:给出一个数列,可以进行一种操作将某一个前缀除去他们的gcd,有一个函数f(x),f(1) = 0 , f(x) = f(x/p)+1,f(x) = f(x/p)-1(p是坏素数), 求 sum( ...
- Codeforces 797E - Array Queries
E. Array Queries 题目链接:http://codeforces.com/problemset/problem/797/E time limit per test 2 seconds m ...
- Codeforces 1108E2 Array and Segments (Hard version) 差分, 暴力
Codeforces 1108E2 E2. Array and Segments (Hard version) Description: The only difference between eas ...
- CodeForces 300A Array
http://codeforces.com/problemset/problem/300/A 题意 :给你n个数字,让你分成3组,第一组各个数之积要小于0,第二组要大于0,第三组要等于0,符合要求的答 ...
- CF_402D Upgrading Array 因式分解
题目链接:http://codeforces.com/problemset/problem/402/D /**算法分析: */ #include<bits/stdc++.h> #defin ...
- 网络流(最大流):CodeForces 499E Array and Operations
You have written on a piece of paper an array of n positive integers a[1], a[2], ..., a[n] and m goo ...
随机推荐
- android 46 service
service是安卓四大组建之一,service只能系统创建不能new,service也要在项目清单中注册说明,service分为2中,一种是系统级的服务,一种是我们自己写的服务. 启动和关闭serv ...
- DEPENDENT SUBQUERY” 和 “SUBQUERY”
http://blog.163.com/li_hx/blog/static/183991413201642410122327/ mysql> CREATE TABLE t1 (a INT, b ...
- 深入掌握JMS--转
深入掌握JMS(一):JSM基础 1. JMS基本概念 JMS(Java Message Service) 即Java消息服务.它提供标准的产生.发送.接收消息的接口简化企业应用的开发.它支持 ...
- Windows7如何在安全模式下卸载驱动(亲测)
在桌面“我的电脑”上点鼠标右键,选择“属性”,“硬件”,“设备管理器”,找到“显示卡选项”,打开前面的“+”,然后按鼠标右键,选择“卸载”就可以了. (亲测,主板驱动卸载成功启动)
- linux device driver —— 字符设备
现在对linux设备驱动还没有什么认识,跟着书上敲了一个字符驱动,这里把代码贴一下. 测试环境是 Ubuntu 16.04 64bit 驱动程序: #include <linux/fs.h> ...
- Map 迭代 两种方法
Map 迭代 两种方法 Map<String, String> map=new HashMap<String,String>(); map.put("1", ...
- AndroidStudio工程文件导入Jar包和So第三方库
AndroidStudio 导入Jar包和第三方So库 在android开发中,需要导入许多第三方的jar包和so库来支持,包括像许多第三方的支持平台--友盟,环信.融云.极光推送.微博.腾讯等第三方 ...
- C#语法糖之第一篇:自动属性&隐式类型
今天给大家分享一下C#语法糖的简单的两个知识点吧. 自动属性:在 C# 4.0 和更高版本中,当属性的访问器中不需要其他逻辑时,自动实现的属性可使属性声明更加简洁. 客户端代码还可通过这些属性创建对象 ...
- >/dev/null 2>&1 这句话的含义
1表示标准输出,2表示标准错误输出 2>&1表示将标准错误输出重定向到标准输出,这样,程序或者命令的正常输出和错误输出就可以在标准输出输出(也就是一起输出). 一般来讲标准输出和标准错误 ...
- CoreBluetooth
Core Bluetooth的基本常识 每个蓝牙4.0设备都是通过服务(Service)和特征(Characteristic)来展示自己的 一个设备必然包含一个或多个服务,每个服务下面又包含若干个特征 ...