思路:对于每个数分解质因子然后记录每一个质因子的个数,对与在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的更多相关文章

  1. Codeforces 494D Upgrading Array

    http://codeforces.com/contest/494/problem/D 题意:给一个数组,和一个坏质数集合,可以无数次地让1到i这些所有数字除以他们的gcd,然后要求Σf(a[i])的 ...

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

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

  3. codeforces 402 D. Upgrading Array(数论+贪心)

    题目链接:http://codeforces.com/contest/402/problem/D 题意:给出一个a串和素数串b .f(1) = 0; p为s的最小素因子如果p不属于b , 否则 . a ...

  4. 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( ...

  5. Codeforces 797E - Array Queries

    E. Array Queries 题目链接:http://codeforces.com/problemset/problem/797/E time limit per test 2 seconds m ...

  6. Codeforces 1108E2 Array and Segments (Hard version) 差分, 暴力

    Codeforces 1108E2 E2. Array and Segments (Hard version) Description: The only difference between eas ...

  7. CodeForces 300A Array

    http://codeforces.com/problemset/problem/300/A 题意 :给你n个数字,让你分成3组,第一组各个数之积要小于0,第二组要大于0,第三组要等于0,符合要求的答 ...

  8. CF_402D Upgrading Array 因式分解

    题目链接:http://codeforces.com/problemset/problem/402/D /**算法分析: */ #include<bits/stdc++.h> #defin ...

  9. 网络流(最大流):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 ...

随机推荐

  1. docker 下 安装rancher 笔记

    sudo yum update 更新系统环境 curl -sSL https://get.docker.com/ | sh 安装最新docker版本 systemctl start docker.se ...

  2. hdu 2457 DNA repair

    AC自动机+DP.按着自动机跑,(其实是生成新的满足题目要求的串,然后找改变最少的.)但是不能跑到是单词的地方,如果跑到单词的话那么说明改变后的串含有病毒了,不满足题意.然后就是应该怎么跑的问题了,现 ...

  3. Qt 学习之路:输入元素

    前面的章节中,我们看到了作为输入元素的MouseArea,用于接收鼠标的输入.下面,我们再来介绍关于键盘输入的两个元素:TextInput和TextEdit. TextInput是单行的文本输入框,支 ...

  4. 学习java随笔第二篇:java开发工具——Eclipse

    java开发工具有很多这里我使用的是Eclipse. 首先我在官网上下载了Eclipse的软件包,下载地址:http://www.eclipse.org/downloads/,然后有在网上找了一个汉化 ...

  5. power desinger 学习笔记三<批量执行sql语句>

    使用sql脚本导入表结构,直接 附带表的 约束.列的注释.真的可以哦 sql语句如下: create table test01 (   ID                   VARCHAR2(10 ...

  6. oracle-snapshot too old 示例

    一.快照太老例子:    1.创建一个很小的undo表空间,并且不自动扩展. create undo tablespace undo_small    datafile '/u01/app/oracl ...

  7. iOS LaunchScreen设置启动图片,启动页停留时间

    [新建的iOS 项目启动画面默认为LaunchScreen.xib] 如果想实现一张图片作为启动页,如下图

  8. Cookie技术详解

    1. Cookie的特性 属性: 1> name: Cookie的名字 2> value: Cookie的值 3> path: 可选,Cookie的存储路径,默认情况下的存储路径时访 ...

  9. c# web 删除时弹出提示框

    方法1: 在控件中增加属性 <asp:Button ID="btnSub" runat="server" Text="提交" oncl ...

  10. 使用Physics_Body_Editor获得json文件的类

    [转自]:http://www.cocoachina.com/bbs/read.php?tid=209290 工具介绍,json文件获得方法,请参考原帖 MyBodyParser.h // // My ...