题目链接:http://codeforces.com/problemset/problem/402/D

题意:

  给你一个长度为n的数列a[i],又给出了m个“坏质数”b[i]。

  定义函数f(s),其中p是s的最小质因子:

    f(1) = 0

    f(s) = f(s/p) + 1 (p不是坏质数)

    f(s) = f(s/p) - 1 (p是坏质数)

  你可以任意次数地进行操作:给a[1 to i]的每个数都除以gcd(a[1 to i])。

  问你 ∑ f(a[i)最大为多少。

题解:

  函数f(s)的实际意义是:

    s的质因子中,好质数的个数 - 坏质数的个数。

  每一次操作的实际意义是:

    对于所选前缀中的每个数,从它的质因子中丢掉若干个好质数和坏质数。

  显然,要想使 ∑ f(a[i)更大,则:

    对于每次操作,当且仅当丢掉的坏质数的个数大于丢掉好质数的个数时,才会执行操作。

  然后考虑操作顺序所带来的影响:

    假设有i < j。

    如果先执行操作opt(1 to i),再执行操作opt(1 to j):

      由于第一次操作已经使得gcd(a[1 to i])变为1,并且区间[1,j]包含着[1,i]。

      所以此时gcd(a[1 to j]) = 1,即第二次操作是无效的。

      也就是说,a[i+1 to j]中的一些数,本身可以对答案做出贡献,却因为第一次操作而失效。

    如果先执行操作opt(1 to j),再执行操作opt(1 to i):

      因为第一次操作已经将a[i+1 to j]中可以除掉的坏质数全部除掉,并将a[1 to i]中的一部分坏质数除掉。

      所以在第二次操作时,只要将a[1 to i]中剩下的坏质数除掉即可,不会有任何浪费。

  所以从后往前贪心地操作就好啦。

  复杂度分析:

    (1)预处理出前缀gcd[i]:

      O(n*logn)

    (2)处理出所有操作完成后的数列a[i](要分解质因数):

      O(n*sqrt(n))

    (3)算出所有的f(a[i])(还要分解质因数):

      O(n*sqrt(n))

AC Code:

 #include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <set>
#define MAX_N 5005 using namespace std; int n,m;
int a[MAX_N];
int g[MAX_N];
set<int> st; void read()
{
cin>>n>>m;
for(int i=;i<=n;i++)
{
cin>>a[i];
}
int x;
for(int i=;i<=m;i++)
{
cin>>x;
st.insert(x);
}
} int gcd(int a,int b)
{
return b== ? a : gcd(b,a%b);
} void cal_g()
{
g[]=a[];
for(int i=;i<=n;i++)
{
g[i]=gcd(g[i-],a[i]);
}
} pair<int,int> cal_p(int x)
{
int good=;
int bad=;
for(int i=,t=sqrt(x);i<=t;i++)
{
int cnt=;
while(x%i==)
{
x/=i;
cnt++;
}
if(cnt)
{
if(st.count(i)>) bad+=cnt;
else good+=cnt;
}
}
if(x!=)
{
if(st.count(x)>) bad++;
else good++;
}
return pair<int,int>(good,bad);
} bool check(int x)
{
pair<int,int> p=cal_p(x);
return p.first<p.second;
} void cal_a()
{
int d=;
for(int i=n;i>=;i--)
{
if(check(g[i]/d)) d=g[i];
a[i]/=d;
}
} int cal_f()
{
int ans=;
for(int i=;i<=n;i++)
{
pair<int,int> p=cal_p(a[i]);
ans+=p.first-p.second;
}
return ans;
} void work()
{
cal_g();
cal_a();
cout<<cal_f()<<endl;
} int main()
{
read();
work();
}

Codeforces 402D Upgrading Array:贪心 + 数学的更多相关文章

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

  2. CodeForces ---596B--Wilbur and Array(贪心模拟)

    Wilbur and Array Time Limit: 2000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Su ...

  3. Codeforces 494D Upgrading Array

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

  4. Codeforces 1054D Changing Array 贪心+异或和

    题意 给一个长度为\(n\)的位数为\(k\)的整数数列\(a\),一次操作可将任意\(a_i\)取反,问经过任意次操作后最多有多少个区间异或和不为\(0\) 分析 求出前缀异或和,区间异或和为\(0 ...

  5. 贪心/数学 Codeforces Round #212 (Div. 2) A. Two Semiknights Meet

    题目传送门 /* 贪心/数学:还以为是BFS,其实x1 + 4 * k = x2, y1 + 4 * l = y2 */ #include <cstdio> #include <al ...

  6. codeforces 704B - Ant Man 贪心

    codeforces 704B - Ant Man 贪心 题意:n个点,每个点有5个值,每次从一个点跳到另一个点,向左跳:abs(b.x-a.x)+a.ll+b.rr 向右跳:abs(b.x-a.x) ...

  7. Codeforces 482B Interesting Array(线段树)

    题目链接:Codeforces 482B Interesting Array 题目大意:给定一个长度为N的数组,如今有M个限制,每一个限制有l,r,q,表示从a[l]~a[r]取且后的数一定为q,问是 ...

  8. Codeforces 1077C Good Array 坑 C

    Codeforces 1077C Good Array https://vjudge.net/problem/CodeForces-1077C 题目: Let's call an array good ...

  9. codeforces 482B. Interesting Array【线段树区间更新】

    题目:codeforces 482B. Interesting Array 题意:给你一个值n和m中操作,每种操作就是三个数 l ,r,val. 就是区间l---r上的与的值为val,最后问你原来的数 ...

随机推荐

  1. php 模拟get和post提交方法[解决ajax跨域问题]

    get: $url = "http://www.111cn.net /index.php?a=b&c=d&e=f&g=" . urlencode('王璐个人 ...

  2. c++中如何查看一个类的内存布局

    打开VS command prompt,输入下述命令可以看到对象的内存布局. cl a.cpp -d1 reportSingleClassLayout[classname] //  reportSin ...

  3. 第三方苹果开发库之ASIHTTPRequest(翻译版)

    本文转载至 http://www.cnblogs.com/daguo/archive/2012/08/03/2622090.html   来自:http://www.dreamingwish.com/ ...

  4. Collecting Bugs (概率dp)

    Ivan is fond of collecting. Unlike other people who collect post stamps, coins or other material stu ...

  5. 物理cpu和逻辑cpu

    1 物理cpu 插槽里面实际插入的cpu的个数. 通过不重复的physical id可以获取实际的物理cpu的个数. 2 逻辑cpu cat /proc/info processor 1 proces ...

  6. STL中vector怎么实现邻接表

    最近,同期的一位大佬给我出了一道题目,改编自 洛谷 P2783 有机化学之神偶尔会做作弊 这道题好坑啊,普通链表过不了,只能用vector来存边.可能更快一些吧? 所以,我想记录并分享一下vector ...

  7. java堆分析神器MAT

    Memory Analyzer(MAT) 基于Eclipse的软件 http://www.eclipse.org/mat/

  8. 我的Android进阶之旅------>如何将Android源码导入Eclipse中来查看(非常实用)

    Android源码下载完成的目录结构如如所示: step1:将.classpath文件拷贝到源代码的根目录 Android源码支持多种IDE,如果是针对APP层做开发的话,建议大家使用Eclipse开 ...

  9. Apache Shiro 使用手册(四)Realm 实现(转发:http://kdboy.iteye.com/blog/1169631)

    在认证.授权内部实现机制中都有提到,最终处理都将交给Real进行处理.因为在Shiro中,最终是通过Realm来获取应用程序中的用户.角色及权限信息的.通常情况下,在Realm中会直接从我们的数据源中 ...

  10. web audio living

    总结网页音频直播的方案和遇到的问题. 代码:(github,待整理) 结果: 使用opus音频编码,web audio api 播放,可以达到100ms以内延时,高质量,低流量的音频直播. 背景: V ...