codeforce 246B——Good Sequences
来源:互联网 发布:曾经很火的网络歌曲 编辑:程序博客网 时间:2024/05/17 02:24
第一次的尝试的思路是,用dp进行进行搜索,不过在第21组TLE,这也很正常。因为为复杂度是O(n^2),而n的最大值为为10^5.
第二次尝试把所给的数进行素性拆分、定义flag数组,表示前面的每个数对应的最长序列的长度,统计素因子的对应的长度最大值,然后再用素因子的最大值更新所有素因子的长度值。然后数据读完答案就出来了。不过n=1要特殊处理。
#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>#include <algorithm>using namespace std;const int maxn=100005;int prime[maxn],flag[maxn],num;void sieve(){memset(flag,0,sizeof(flag));int i,j;for(i=2;i*i<=maxn;i++)if(!flag[i])for(j=i*i;j<=maxn;j+=i)flag[j]=1;} int gen_prime(){sieve();flag[0]=1,flag[1]=1;int count=0;for(int i=2;i<=maxn;i++)if(!flag[i])prime[count++]=i;return count;}int main(){int n,cnt;cnt=gen_prime();//生成10w以内的素数、并统计素数的个数;不过因为CF单组判断,所以浪费了时间。while(scanf("%d",&n)==1){memset(flag,0,sizeof(flag));for(int i=1;i<=n;i++){scanf("%d",&num);int ans=0,tmp=num;for(int j=0;j<cnt&&tmp!=1;j++){if(tmp%prime[j]==0)//查找素因子对应的长度最大值{flag[prime[j]]++;while(tmp%prime[j]==0)tmp/=prime[j];if(ans<flag[prime[j]])ans=flag[prime[j]];}}for(int j=0;j<cnt&&num!=1;j++){if(num%prime[j]==0)//更新素因子对应值flag[prime[j]]=ans;while(num%prime[j]==0)num/=prime[j];}}int anwser=0;if(n==1)anwser=1;else {for(int j=0;j<cnt;j++)if(anwser<flag[prime[j]])anwser=flag[prime[j]];}printf("%d\n",anwser);} return 0;}
0 0
- codeforce 246B——Good Sequences
- Codeforce 题目264B Good Sequences(DP,相邻两数不互质的LIS)
- Codeforces 264B (Good Sequences)
- Codeforcrs 264B Good Sequences
- Good Sequences(codeforces 264B)
- codeforces 264B Good Sequences
- Codeforce 450B Jzzhu and Sequences
- CF #div.1 B.good sequences
- codeforces 264b Good Sequences dp
- codeforces #264B Good Sequences (dp)
- CodeForces 264B Good Sequences dp
- Codeforces 264B Good Sequences (dp)
- Codeforces 264B Good Sequences【dp+思维】
- codeforces 264B B. Good Sequences(dp+数论)
- Codeforce 189B——Counting Rhombi
- DP Good Sequences
- Codeforce Good Bye 2013
- 【又一坑爹水题】CodeForce 18B——Platform
- Python 正则表达式查找重复单词
- MFC控件随对话框大小改变而改变
- Maven管理Jar包时,导出项目原理啊
- ssh实现分页查询
- 我这有套安卓的视频给大家分享下
- codeforce 246B——Good Sequences
- Matlab学习----------GUI数据管理
- sql server关于正常状态突然出现'80004005'的问题解决
- 去除svn蓝色问号
- JAVA采用JDBC执行批处理操作注意的有关问题
- java定时任务,每天定时执行任务
- 直接选择排序——MW
- 设置EditText的android:imeOptions来改变软键盘Enter键图标
- 狄利克雷分布