首页 > 其他分享 >CF 264B(质因数分解)

CF 264B(质因数分解)

时间:2022-10-25 10:32:55浏览次数:51  
标签:因数分解 int max CF len MAXN ans 264B include


D. Good Sequences



time limit per test



memory limit per test



input



output



n 有 a1, a2, ..., an

问删后序列最长长度.



Input



n (1 ≤ n ≤ 105) 第二行序列 a1, a2, ..., an (1 ≤ ai ≤ 105ai < ai + 1).



Output



删后序列最长长度.



Sample test(s)



input



5 2 3 4 6 9



output



4



input



9 1 2 3 5 6 7 8 9 10



output



4



Note



In the first example, the following sequences are examples of good sequences: [2; 4; 6; 9], [2; 4; 6], [3; 9], [6]. The length of the longest good sequence is 4.



错解:枚举开头即可。X


#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<functional>
#include<algorithm>
#include<cctype>
using namespace std;
#define MAXN (100000+10)
int n,a[MAXN];
bool b[MAXN]={0};
int gcd(int a,int b){return (b==0)?a:gcd(b,a%b);};
int main()
{
scanf("%d",&n);for (int i=1;i<=n;i++) scanf("%d",&a[i]);
int ans=0;
for (int i=1;i<=n;i++)
if (!b[i])
{
int len=1;
b[i]=1;
int head=i,tail=i+1;
while (tail<=n)
{
if (gcd(a[tail],a[head])==1) tail++;
else {b[head]=1;head=tail;tail++;len++; }
}
ans=max(ans,len);
}
cout<<ans<<endl;

return 0;
}

更正:

枚举开头不行,因为一个开头可能跟有多个序列(2,6,9)/(2,4)←选这个不忧

故枚举质因数,证明下:

若a和b不互质,则必存在质数k,使k|a&&k|b

故Dp如下:

 f[i,j]=max(f[i-1,k]+1) (k| a[i] 且j|a[i] ) 最大值len=f[i,j] 

//  f[i,j]  表示到第i个数为止,结尾是质数 j 的倍数的最大长度。

+上滚动数组后,得到如下的Dp方程

计算: len=max(f[k])+1 (k| a[i] )

更新:f[j]=max(f[j],len) (j|a[i]) 


注意质因数的分解中 先分解到√n,若此时未除尽,那一部分也要算进去(肯定是质数,否则必能再分)

eg:14=2*7(7=√49>√14)



#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<functional>
#include<algorithm>
#include<cctype>
using namespace std;
#define MAXN (100000+10)
int n,a[MAXN],f[MAXN]={0},st[MAXN],size;
int gcd(int a,int b){return (b==0)?a:gcd(b,a%b);};
void fac_prime(int x)
{
size=0;
for (int j=2;j*j<=x;j++)
{
if (x%j==0)
{
while (x%j==0) x/=j;
st[++size]=j;
}
}
if (x>1) st[++size]=x;
}
int main()
{
scanf("%d",&n);
int ans=0;
for (int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
if (a[i]==1) {ans=max(ans,1);continue;}
fac_prime(a[i]);
int len=0;
if (st[1]==a[i]) {len=1;f[a[i]]=1;}
else
for (int j=1;j<=size;j++)
len=max(len,f[st[j]]+1);

for (int j=1;j<=size;j++) f[st[j]]=max(f[st[j]],len);
ans=max(ans,len);
// for (int j=1;j<=a[i];j++) cout<<f[j]<<' ';
// cout<<endl;
}
cout<<ans<<endl;
return 0;
}







标签:因数分解,int,max,CF,len,MAXN,ans,264B,include
From: https://blog.51cto.com/u_15724837/5794112

相关文章

  • CF 18A(近似直角三角形判断+向量直角公式+switch+istream&(..&P a))
    A.Triangletimelimitpertestmemorylimitpertestinputoutput判断一个格......
  • CF 312A(Whose sentence is it?-strstr(s,p))
    A.Whosesentenceisit?timelimitpertestmemorylimitpertestinputoutput......
  • CF 287A(IQ Test-枚举3个字符相等的矩阵)
    A.IQTesttimelimitpertestmemorylimitpertestinputoutputInthecity......
  • CF 286A(Lucky Permutation-数列找规律)
    A.LuckyPermutationtimelimitpertestmemorylimitpertestinputoutputp......
  • 1.1 WCF SOA架构和webservice
    1.什么是SOA?SOA全称:面向服务架构(serviceOrientedArchitecture),它是一种组件架构模式。一、定义1.WebService:严格来说是行业标准,不是技术,使用XML扩展标记语言来表示数据......
  • CF1716F
    与CF932E,CF1278F其实差不多捏。首先\(m\)中奇数个数是\(\left\lceil\frac{m}{2}\right\rceil\),偶数个数是\(\left\lfloor\frac{m}{2}\right\rfloor\)。下文为了方便......
  • [CF1753C]Wish I Knew How to Sort
    做题时间:2022.10.25\(【题目描述】\)给定一个长度为\(n\)的01序列\(a\)和一种操作,你需要用这种操作将序列从小到大排序。操作为:等概率随机选择两个位置\(i,j(i<j)\)......
  • CF1278F
    与CF932E其实是差不多的捏设\(p=\dfrac{1}{m},q=1-p\),那么枚举第一张是王牌的次数,有如下式子:\[\sum_{i=1}^{n}\binom{n}{i}p^iq^{n-i}i^k\]后面那个\(i^k\)可以展......
  • CF1744B Even-Odd Increments
    简要题意\(T\)组数据,每组数据给定一个长度为\(n\)的数列,有\(q\)次操作,共有两种操作:\(\texttt{0x}\),给数列中所有偶数加上\(x\);\(\texttt{1x}\),给数列中所有奇......
  • CF932E
    先介绍这样一个等式:\[n^m=\sum_{i=1}^{m}\begin{Bmatrix}m\\i\end{Bmatrix}\timesi!\times\binom{n}{i}\]等式左边的组合意义是\(m\)个不同的球放入\(n\)个不同的......