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 ...
随机推荐
- change buffer
https://yq.aliyun.com/articles/222 change buffer: insert buffer delete buffer purge buffer 1.innodb_ ...
- 数据结构 : Hash Table
http://www.cnblogs.com/lucifer1982/archive/2008/06/18/1224319.html 作者:Angel Lucifer 引子 这篇仍然不讲并行/并发. ...
- Python开发【第十三篇】:jQuery(二)
http://www.bubuko.com/infodetail-1438296.html 处理完毕需要整理贴进来 Python之路[第十三篇]jQuery案例-Form表单&插件及扩展 ...
- float与double剖析
今天研究下float与double的编码 float: 我们来看一下这组数是如何一步步从16进制转换到float的 float编码格式: 1.将16进制转换到2进制 整理后:0 1000 0010 1 ...
- ftp不能上传解决办法
自己的服务器,然后我分好ftp,有一天,有个客户要传东西,发现怎么也传不上,但是可以下载,,,,弄了半天,目录权限也是完全访问,但还是不行,原来是serv-u分配的空间小了啊,所以只能下载不能上传.. ...
- Android开发--去掉标题栏
Android开发中为了尽可能美观,会去掉标题栏.去掉标题栏有三种方法. 一.在Activity代码里实现 在代码中实现以下方法: this.requestWindowFeature(Window.F ...
- Intellij Idea 13 vmoptions (Mac版本)
-ea -server -Xms1g -Xmx1g -Xss16m -XX:PermSize=256m -XX:MaxPermSize=256m -XX:+DoEscapeAnalysis -XX:+ ...
- if elsif;报错;new赋值
1. IF INSERTING THEN BEGIN 中间不能为空 END;ELSIF DELETING THEN BEGIN E ...
- ASP.NET 导入EXCEL文档
鉴于教务一般都是手动输入学生信息,在未了解本校数据库的客观情况之下,我们准备设计一个导入excel文档中学生信息如数据库的功能.结合网上各类大牛的综合版本出炉.. 首先具体的实现思想如下: 1.先使用 ...
- For Me ...
一直有做博客的打算,但是这也看不上,那也看不上. 总想着做一个personal blog,界面,风格全由自己设计. 可时间上总是不充裕的. 可技术上总是欠火候的. 可内容上总是待斟酌的. 于是时间被我 ...