codeforces Upgrading Array

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

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <map>
 4 #include <set>
 5 #include <algorithm>
 6 using namespace std;
 7
 8 set<int>q;
 9 int n,m;
10 int a[50010],b[50010];
11 int g[50010];
12
13 int gcd(int a,int b)
14 {
15     return b==0?a:gcd(b,a%b);
16 }
17
18 int Get_num(int x)
19 {
20     int ans=0;
21     map<int,int>p;
22     for(int j=2; j*j<=x; j++)
23     {
24         if(x%j==0)
25         {
26             while(x%j==0)
27             {
28                 p[j]++;
29                 x/=j;
30             }
31         }
32     }
33     if(x>1) p[x]++;
34     map<int,int>::iterator it;
35     for(it=p.begin(); it!=p.end(); it++)
36     {
37         if(q.find(it->first)==q.end()) ans+=it->second;
38         else ans-=it->second;
39     }
40     return ans;
41 }
42
43 int main()
44 {
45
46     scanf("%d%d",&n,&m);
47     for(int i=1; i<=n; i++)
48     {
49         scanf("%d",&a[i]);
50     }
51     for(int j=1; j<=m; j++)
52     {
53         scanf("%d",&b[j]);
54         q.insert(b[j]);
55     }
56     int ans=0;
57     for(int i=1; i<=n; i++)
58     {
59        ans+=Get_num(a[i]);
60     }
61     for(int i=1; i<=n; i++)
62     {
63         if(i==1) g[i]=a[i];
64         else g[i]=__gcd(g[i-1],a[i]);
65     }
66     int c=1;
67     for(int i=n; i>=1; i--)
68     {
69         int h=g[i]/c;
70         int s=Get_num(h);
71         if(s<0)
72         {
73             ans-=s*i;
74             c*=h;
75         }
76     }
77     printf("%d\n",ans);
78     return 0;
79 }

时间: 2024-08-02 11:02:08

codeforces Upgrading Array的相关文章

codeforces A. Array题解

Vitaly has an array of n distinct integers. Vitaly wants to divide this array into three non-empty sets so as the following conditions hold: The product of all numbers in the first set is less than zero (?<?0). The product of all numbers in the secon

Codeforces 57C Array dp暴力找规律

题目链接:点击打开链接 先是计算非递增的方案, 若非递增的方案数为x, 则非递减的方案数也是x 答案就是 2*x - n 只需求得x即可. 可以先写个n3的dp,然后发现规律是 C(n-1, 2*n-1) 然后套个逆元即可. #include<iostream> #include<cstdio> #include<vector> #include<string.h> using namespace std; #define ll long long #def

Codeforces 494B Upgrading Array

http://codeforces.com/problemset/status 题意:给一个数组,和一个坏质数集合,可以无数次地让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以后的部分. 略坑,不知道第一次被什么卡了超时..

codeforces 402D D. Upgrading Array(dp+数论)

题目链接: codeforces 402D 题目大意: 给出一个数列,可以进行一种操作将某一个前缀除去他们的gcd,有一个函数f(x),f(1) = 0 , f(x) = f(x/p)+1,f(x) = f(x/p)-1(p是坏素数)问 ∑i≤nf(a[i]) 的最大值 题目分析: 首先根据那个递推式我们知道结果就是每个数的f(x)就是它的质因数当中的好素数的个数减去坏素数的个数. 然后我们知道如果某个gcd中好素数的个数小于坏素数的个数,那么我们把它除掉一定可以得到更好的结果,那么如果从最后一

CodeForces 402D Upgrading Array

http://codeforces.com/problemset/problem/402/D 题意: 给了一串序列,计算出这个序列最小的∑f(a[i]).定义了f(s),给了个公式.可以进行一系列的操作,这个操作为从1~r 这些数都除以他们的gcd.让你最后算出最后最小∑f(a[i])是多少.当然如果不需要进行操作就可以达到最小,也是可以的. 思路: 首先要看出所定义的f(s)的那个公式其实就是代表的是将s分解为质因数的乘积之后,好的素数有几个,坏的素数为几个,好的+1,坏的-1. 那么如果要使

Codeforces 402D Upgrading Array:贪心 + 数学

题目链接: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)的实际

Codeforces 1108E2 Array and Segments (Hard version) 差分, 暴力

Codeforces 1108E2 E2. Array and Segments (Hard version) Description: The only difference between easy and hard versions is a number of elements in the array. You are given an array \(a\) consisting of \(n\) integers. The value of the \(i\)-th element

网络流(最大流):CodeForces 499E Array and Operations

You have written on a piece of paper an array of n positive integers a[1], a[2], ..., a[n] and m good pairs of integers (i1, j1), (i2, j2), ..., (im, jm). Each good pair (ik, jk) meets the following conditions: ik + jk is an odd number and 1 ≤ ik < j

CodeForces 57C Array 组合计数+逆元

题目链接: http://codeforces.com/problemset/problem/57/C 题意: 给你一个数n,表示有n个数的序列,每个数范围为[1,n],叫你求所有非降和非升序列的个数. 题解: 由于对称性,我们只要求非降序的个数就可以了(n个数全部相等的情况既属于非升也属于非降) 我们在满足条件的n个数之前加一个虚节点1,在第n个数之后加一个n,那么考虑这n+2个数组成的非降序列: 假设序列里的第i个数为a[i],我们设xi=a[i+1]-a[i]+1,1<=i<=n+1,则