题意:给出一个数列,可以进行一种操作将某一个前缀除去他们的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; }