题意:给出一个数列,可以进行一种操作将某一个前缀除去他们的gcd,有一个函数f(x),f(1) = 0 , f(x) = f(x/p)+1,f(x) = f(x/p)-1(p是坏素数),

求 sum(f[a[i]]) 的最大值。

析:因为f(1) = 0,否则如果是好素数,那么就加一,如果是坏素数就减一,很明显每个数 f(a[i]) 的值就是好素数的数目,送去坏素数的数目,

然后求总的和,这样可以预处理出所有的 gcd,好素数的个数,坏素数的个数,然后dp[i] 表示 sum(f(a[i])) 前 i 个的和最大值。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e16;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 5000 + 10;
const int mod = 1000000007;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
vector<int> prime;
bool vis[(int)1e5+5];
int a[maxn];
void init(){
int t = (int)sqrt(1e9 + 0.5);
for(int i = 2; i <= t; ++i) if(!vis[i]){
prime.push_back(i);
for(int j = i*i; j <= t; j += i) vis[j] = true;
}
} int dp[maxn];
int good[maxn];
int bad[maxn];
int gg[maxn];
int gb[maxn];
int g[maxn];
map<int, bool> mp; void solve(int i, int t, int *good, int *bad){
for(int j = 0; j < prime.size() && t > 1; ++j) if(t % prime[j] == 0){
if(mp[prime[j]]){
while(t % prime[j] == 0){
t /= prime[j];
++bad[i];
}
}
else while(t % prime[j] == 0){
t /= prime[j];
++good[i];
}
}
if(t > 1 && mp[t]) ++bad[i];
else if(t > 1) ++good[i];
} int main(){
init();
scanf("%d %d", &n, &m);
for(int i = 1; i <= n; ++i){
scanf("%d", a+i);
g[i] = gcd(g[i-1], a[i]);
}
for(int i = 0; i < m; ++i){
int x;
scanf("%d", &x);
mp[x] = true;
} for(int i = 1; i <= n; ++i){
solve(i, a[i], good, bad);
good[i] += good[i-1];
bad[i] += bad[i-1];
solve(i, g[i], gg, gb);
} for(int i = 0; i <= n; ++i) dp[i] = -INF;
dp[0] = 0;
for(int i = 1; i <= n; ++i)
for(int j = 0; j < i; ++j)
dp[i] = max(dp[i], dp[j]+good[i]-good[j]+bad[j]-bad[i]+max(0, (i-j)*(gb[i]-gg[i]))); printf("%d\n", dp[n]);
return 0;
}

  

CodeForces 402D Upgrading Array (数学+DP)的更多相关文章

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

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

  2. Codeforces 494D Upgrading Array

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

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

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

  4. 数学+DP Codeforces Round #304 (Div. 2) D. Soldier and Number Game

    题目传送门 /* 题意:这题就是求b+1到a的因子个数和. 数学+DP:a[i]保存i的最小因子,dp[i] = dp[i/a[i]] +1;再来一个前缀和 */ /***************** ...

  5. 数学+dp HDOJ 5317 RGCDQ

    题目传送门 /* 题意:给一个区间,问任意两个数的素数因子的GCD最大 数学+dp:预处理出f[i],发现f[i] <= 7,那么用dp[i][j] 记录前i个f[]个数为j的数有几个, dp[ ...

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

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

  7. Codeforces 1077C Good Array 坑 C

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

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

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

  9. codeforces 407C Curious Array

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

随机推荐

  1. Agc019_C Fountain Walk

    传送门 题目大意 给定网格图上起点和终点每个格子是长为$100$米的正方形,你可以沿着线走. 平面上还有若干个关键点,以每个关键点为圆心,$10$为半径画圆,表示不能进入圆内的线,但是可以从圆周上走, ...

  2. HDU - 1506 Largest Rectangle in a Histogram (单调栈/笛卡尔树)

    题意:求一个直方图中最大矩形的面积. 很经典的一道问题了吧,可以用单调栈分别求出每个柱子左右两边第一个比它低的柱子(也就相当于求出了和它相连的最后一个比它高的柱子),确定每个柱子的左右边界,每个柱子的 ...

  3. Mybatis相关SQL操作总结

    1.resultMap和resultType等参数以及结果集 <select id="getApplicationByRoleCode" resultType="p ...

  4. phpstorm下载和破解

    http://idea.qinxi1992.cn/ 官网下载对应版本,在下面进行破解! storm官网:https://www.jetbrains.com/phpstorm/ 破解网址  :http: ...

  5. MySQL的varchar类型注意事项

    前几天就在工作中发现这样一个问题:当某个字段的类型为varchar时,字段保存的值类似'100,200,300'  和 '100' 或 '100,400'.写SQL语句的时候就会犯这样的错误,例如: ...

  6. POJ2831(次小生成树问题)

    Can We Build This One? Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 1475   Accepted: ...

  7. spark 算子分析

    别的不说先上官网: action 这些算子中需要注意: 1.reduce 和 reduceByKey 虽说都有reduce,但是一个是action级别,一个是transformation级别,速度上会 ...

  8. linux指令 apt-grt指令使用

    apt-get 是linux的一条指令,主流的linux版本Debian和ubuntu都使用apt-get来安装软件.那么,需安装的软件都放在哪里呢??? apt-get 利用软件安装源来安装软件,其 ...

  9. java代码输出1到100的质数

    总结:循环,循环.. package com.dfd; import java.util.Scanner; //输出0到100的质数 //要判断当到100时候,等于2的直接输出,能被2整除的不输出, ...

  10. &(((struct A*)NULL)->m_float)---offsetof

    问题描述: struct A { int m_int; float m_float; }; int main(void) { printf("%p",&(((struct ...