http://codeforces.com/contest/494/problem/D

题意:给一个数组,和一个坏质数集合,可以无数次地让1到i这些所有数字除以他们的gcd,然后要求Σf(a[i])的最大值,其中

f(x)=f(x/p)+1,p为x的最小质数,且p不为坏质数

f(x/p)-1 ,p为x的最小质数,且p为坏质数

思路:我们考虑,如果f[j]取了1到j的gcd,那么对后面的决策并没有任何影响,因为我们统计的贡献,只统计1到i这个部分,不统计i以后的部分。

略坑,不知道第一次被什么卡了超时。。就写哈希了。。

 #include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<map>
#include<set>
std::map<int,int>mp;
std::set<int>b;
int n,m,f[],g[],a[];
int read(){
char ch=getchar();int t=,f=;
while (ch<''||ch>''){if (ch=='-') f=-;ch=getchar();}
while (''<=ch&&ch<=''){t=t*+ch-'';ch=getchar();}
return t*f;
}
int gcd(int a,int b){
if (b==) return a;
else return gcd(b,a%b);
}
int F(int x){
if (x<=) return ;
if (b.count(x)) return -;
int rec=x,res=;
if (mp.find(rec)!=mp.end()) return mp[rec];
int d=,cnt=;
for (int i=;i*i<=rec;i++){
int p=i;
if (x%p==){
if (b.count(p)) d=-;else d=;
while (x%p==) x/=p,cnt+=d;
}
}
if (x!=){
if (b.count(x)) d=-;else d=;
cnt+=d;
}
return (mp[rec]=cnt);
}
int main(){
n=read();m=read();
for (int i=;i<=n;i++) a[i]=read();
for (int i=;i<=m;i++) {int x=read();b.insert(x);}
int ans=;
g[]=a[];
for (int i=;i<=n;i++) g[i]=gcd(g[i-],a[i]);
for (int i=;i<=n;i++)
ans+=F(a[i]);
f[]=ans;
for (int i=;i<=n;i++){
int s=F(g[i]);
f[i]=-0x3f3f3f3f;
for (int j=i-;j>=;j--)
f[i]=std::max(f[i],f[j]-s*(i-j));
}
ans=-0x3f3f3f3f;
for (int i=;i<=n;i++) ans=std::max(ans,f[i]);
printf("%d\n",ans);
}

还有一种贪心做法,就是从后往前取,能取就取,直到变小为止。

 #include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<map>
#include<set>
std::map<int,int>mp;
std::set<int>b;
int n,m,f[],g[],a[];
int read(){
char ch=getchar();int t=,f=;
while (ch<''||ch>''){if (ch=='-') f=-;ch=getchar();}
while (''<=ch&&ch<=''){t=t*+ch-'';ch=getchar();}
return t*f;
}
int gcd(int a,int b){
if (b==) return a;
else return gcd(b,a%b);
}
int F(int x){
if (x<=) return ;
if (b.count(x)) return -;
int rec=x,res=;
if (mp.find(rec)!=mp.end()) return mp[rec];
int d=,cnt=;
for (int i=;i*i<=rec;i++){
int p=i;
if (x%p==){
if (b.count(p)) d=-;else d=;
while (x%p==) x/=p,cnt+=d;
}
}
if (x!=){
if (b.count(x)) d=-;else d=;
cnt+=d;
}
return (mp[rec]=cnt);
}
int main(){
n=read();m=read();
for (int i=;i<=n;i++) a[i]=read();
for (int i=;i<=m;i++) {int x=read();b.insert(x);}
int ans=;
g[]=a[];
for (int i=;i<=n;i++) g[i]=gcd(g[i-],a[i]);
for (int i=;i<=n;i++)
ans+=F(a[i]);
for (int i=n;i>=;i--){
int s=F(g[i]);
if (s<){
ans-=s*i;
int t=g[i];
for (int j=;j<=i;j++)
g[j]/=t;
}
}
printf("%d\n",ans);
}

Codeforces 494D Upgrading Array的更多相关文章

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

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

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

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

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

  4. Codeforces 1077C Good Array 坑 C

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

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

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

  6. codeforces 407C Curious Array

    codeforces 407C Curious Array UPD: 我觉得这个做法比较好理解啊 参考题解:https://www.cnblogs.com/ChopsticksAN/p/4908377 ...

  7. codeforces 797 E. Array Queries【dp,暴力】

    题目链接:codeforces 797 E. Array Queries   题意:给你一个长度为n的数组a,和q个询问,每次询问为(p,k),相应的把p转换为p+a[p]+k,直到p > n为 ...

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

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

  9. codeforces Upgrading Array

    思路:对于每个数分解质因子然后记录每一个质因子的个数,对与在b中出现的质因子就减去1,否则加1,求出总的,然后从后面一次对它们的最大公约数,然后判断除以最大公约数之后,改变量是不是变化,求最大值,变化 ...

随机推荐

  1. IntelliJ IDEA: maven & jetty 开发 java web

    之前使用eclipse + maven + jetty开发java web应用,本着no zuo no gain的想法, 折腾了一下Intellj idea下开发环境的搭建,顺带学习了maven re ...

  2. coalesce和nvl函数

    coalesce 函数 : Oracle COALESCE函数语法为COALESCE(表达式1,表达式2,...,表达式n),n>=2,此表达式的功能为返回第一个不为空的表达式,如果 都为空则返 ...

  3. netstat命令, netstat指令在windows和linux有什么不同

    查看当前tcp监听端口[op@TIM html]$ netstat -nltp(Not all processes could be identified, non-owned process inf ...

  4. 3D objects key rendering steps

    Key steps of Rendering objects: 1 Create objects’ meshes, which we can use C++’s vector container to ...

  5. URAL 1658

    题目大意:求出T个最小的满足各个位的和为S1,平方和为S2的数.按顺序输出.数的位数大于100或者不存在这样一个数时,输出:No solution. KB     64bit IO Format:%I ...

  6. android推送-PHP(第三方推送:个推)

    在项目初期,就安卓推送功能怎么做,曾经参考过例如XMPP之类的推送方法.但苦于那些是些英文档案,又没太多时间研究,所以打算采用第三方推送(个推,极光推送等),后来在美图技术老大推荐下用采用个推. PS ...

  7. windows下批量杀死进程

    有时候因为病毒或其它原因,启动了一系列的进程,而且有时杀了这个,又多了那个.使用命令taskkill可将这些进程一下子所有杀光: C:\Users\NR>taskkill /F /im fron ...

  8. 初学者学Java设计模式(一)------单例设计模式

    单例设计模式 单例设计模式是指一个类只会生成一个对象,优点是他可以确保所有对象都访问唯一实例. 具体实现代码如下: public class A { public static void main(S ...

  9. LoadRunner测试下载功能点脚本(方法一)

    性能需求:对系统某页面中,点击下载功能做并发测试,以获取在并发下载文件的情况下系统的性能指标. 备注:页面上点击下载时的文件可以是word.excel.pdf等. 问题1:录制完下载的场景后,发现脚本 ...

  10. jQuery.extend 和 jQuery.fn.extend

    1.jQuery.extend 我们先把jQuery看成了一个类,这样好理解一些.jQuery.extend(),是扩展的jQuery这个类. 假设我们把jQuery这个类看成是人类,能吃饭能喝水能跑 ...