BUPT Summer Journey #test7 B
来源:互联网 发布:java数组元素相加 编辑:程序博客网 时间:2024/04/29 17:06
B. 最长数链 2014新生暑假个人排位赛07
时间限制 1000 ms 内存限制 65536 KB
题目描述
现有正整数集A={i|i>0&&i<=n},求A的子集合B,使得对于任意x属于B,y属于B,x整除y或y整除x。优先考虑所含元素多的子集合,然后优先考虑所含元素和大的子集合,然后优先考虑字典序小的子集合。
输入格式
每组数据包含一行一个整数n(n<1e5).输入以EOF结束。
输出格式
每组数据输出一行,从小到大输出所求集合,注意行尾不要有空格。
输入样例
6100
输出样例
1 3 61 3 6 12 24 48 96
思路:即从下往上枚举整除的数,越小的数扩大集合元素的可能就越大。故从2搜,若数目相同就跳到3,由于数据范围这种贪心方法不会有错。代码:
#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<algorithm>//define LOCALusing namespace std;//bool sieve[100000];int n;int ans[100000];/*void primelist(int n){ int m=sqrt(100000+0.5); prime[0]=0; for(int i=2;i<=m;i++) if(!sieve[i]) { prime[++prime[0]]=i; for(int j=i*i;j<=n;j+=i)sieve[j]=1; }}*/void dfs(int x,int s){ //printf("x=%d s=%d\n",x,s);system("pause"); if(s>n)return; int p,j=1; //if(N==16)cout<<(log(N/2)/log(2))<<endl; int Index1=0,Index2=0; p=s;p=p*2;while(p<=n){p*=2;Index1++;} p=s;p=p*3;while(p<=n){p*=2;Index2++;} j=2; if(Index2>=Index1)j=3; ans[++ans[0]]=s;//printf("index1=%d index2=%d\n",Index1,Index2);printf("j=%d\n",j); dfs(x+1,s*j);}int main(){ #ifdef LOCAL freopen("input.txt","r",stdin); #endif // LOCAL //memset(sieve,0,sizeof(sieve)); //prime[0] //primelist(100000); //for(int i=1;i<=30;i++)printf("prime[%d]=%d\n",i,prime[i]); while(scanf("%d",&n)==1) { ans[0]=0; dfs(1,1); printf("%d",ans[1]); for(int i=2;i<=ans[0];i++)printf(" %d",ans[i]); printf("\n"); } return 0;}
0 0
- BUPT Summer Journey #test7 B
- BUPT Summer Journey #test7 A
- BUPT Summer Journey #test7 C
- BUPT Summer Journey #test7 D
- BUPT Summer Journey #test7 E
- BUPT Summer Journey #test1 B
- BUPT Summer Journey #test2 B
- BUPT Summer Journey #test3 B
- BUPT Summer Journey #test4 B
- BUPT Summer Journey #test5 B
- BUPT Summer Journey #test6 B
- BUPT Summer Journey #test8 B
- BUPT Summer Journey #test9 B
- BUPT Summer Journey #test11 B
- BUPT Summer Journey #test1 A
- BUPT Summer Journey #test1 C
- BUPT Summer Journey #test1 D
- BUPT Summer Journey #test1 E
- BUPT Summer Journey #test7 A
- mysql 启动 和 1067 错误的原因:my.ini 配置内容错误
- 再一次发现自己的无知与悲哀
- POJ_3259_Wormholes_最短路算法判环
- windows 服务调试
- BUPT Summer Journey #test7 B
- 设计模式读书笔记1--简单的模拟鸭子应用
- 读书笔记:《SEO教程:搜索引擎优化入门与进阶》(2)——关键字
- sed记录
- mysql抽取某张表的数据到文本文件
- ThinkPHP学习之项目配置与前后台公共配置项
- hdu1690Bus System--解题报告
- 字符串-01. 在字符串中查找指定字符
- 正则以空格,回车分割字符串为字符串数组