题意:给出一个数列,可以进行一种操作将某一个前缀除去他们的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. [转载] 最简单的基于FFmpeg的AVDevice例子(读取摄像头)

    =====================================================最简单的基于FFmpeg的AVDevice例子文章列表: 最简单的基于FFmpeg的AVDev ...

  2. Python 2.7_爬取CSDN单页面博客文章及url(二)_xpath提取_20170118

    上次用的是正则匹配文章title 和文章url,因为最近在看Scrapy框架爬虫 需要了解xpath语法 学习了下拿这个例子练手 1.爬取的单页面还是这个rooturl:http://blog.csd ...

  3. SQL夯实基础(二):连接操作中使用on与where筛选的差异

    一.on筛选和where筛选 在连接查询语法中,另人迷惑首当其冲的就要属on筛选和where筛选的区别了,如果在我们编写查询的时候, 筛选条件的放置不管是在on后面还是where后面, 查出来的结果总 ...

  4. LeetCode Binary Tree Tilt

    原题链接在这里:https://leetcode.com/problems/binary-tree-tilt/description/ 题目: Given a binary tree, return ...

  5. C++对C语言的拓展(1)—— 引用

    1.变量名 变量名实质上是一段连续存储空间的别名,是一个标号(门牌号): 通过变量来申请并命名内存空间: 通过变量的名字可以使用内存空间. 2.引用的概念 变量名,本身是一段内存的引用,即别名(ali ...

  6. RabbitMQ入门Demo

    之前环境安装已经介绍过了,下面直接跑个Demo. 1.添加Maven依赖 <dependency> <groupId>org.springframework.amqp</ ...

  7. python数据类型,int,str,bool

    一,python中的int() int在python中主要用来运算,对字符串的转化,用int(str)表示,并且需要str.isdigit为真. 在int()中二进制的转换如下: #bit_lengt ...

  8. 应用层-day02

    web与HTTP web的应用层协议时超文本传输协议(HyperText Transfer Protocol HTTP) HTTP是由两个程序实现的:一个客户端程序和一个服务器程序. HTTP定义了w ...

  9. djangocms安装使用指南

    ubuntu 14.04 virtualenv venv --python=python3 . venv/bin/activate sudo apt-get upgradesudo apt-get i ...

  10. c++11之二: 类成员变量初始化

    在C++11中, 1.允许非静态成员变量的初始化有多种形式:初始化列表; 使用等号=或花括号{}进行就地的初始化. 可以为同一成员变量既声明就地的列表初始化,又在初始化列表中进行初始化,只不过初始化列 ...