设为首页 加入收藏

TOP

POJ_3421_X-factor Chains(素数筛法)
2015-11-21 01:00:43 来源: 作者: 【 】 浏览:2
Tags:POJ_3421_X-factor Chains 素数
X-factor Chains
Time Limit: 1000MS ? Memory Limit: 65536K
Total Submissions: 5659 ? Accepted: 1786

Description

Given a positive integer X, an X-factor chain of length m is a sequence of integers,

1 = X0, X1, X2, …, Xm = X

satisfying

Xi < Xi+1 and Xi | Xi+1 where a | b means a perfectly divides into b.

Now we are interested in the maximum length of X-factor chains and the number of chains of such length.

Input

The input consists of several test cases. Each contains a positive integer X (X ≤ 220).

Output

For each test case, output the maximum length and the number of such X-factors chains.

Sample Input

2
3
4
10
100

Sample Output

1 1
1 1
2 1
2 2
4 6

?

题意:给你一个数X,将X分解成1~X的因子数列,前一个数可以整数后一个数,求满足条件的最大链长以及有多少条这样长的链。

分析:很容易想到了素因数分解。不难推出,我们要求的最大链长就等于素因子的个数,最长链条数就是这些素因子的排列组合数。题目数据量较大,所以先打个素数表。

题目链接:http://poj.org/problem?id=3421

代码清单:

?

#include
  
   
#include
    #include
    
      #include
     
       #include
      
        #include
       
         #include
        
          #include
         
           #include
          
            #include
           
             #include
            
              #include
             
               #include
              
                using namespace std; typedef long long ll; typedef unsigned long long ull; typedef unsigned int uint; const int maxp = 10000 + 5; const int maxn = 1100000 + 5; int sum,X; int prime[maxp]; bool vis[maxn]; int num[maxp],k; //存每个素因子的个数 void init(){ //素数筛法打表 sum=0; memset(vis,true,sizeof(vis)); for(int i=2;i<=maxn;i++){ if(!vis[i]) continue; prime[sum++]=i; if(i>(int)sqrt(maxn)) continue; for(int j=i*i;j<=maxn;j+=i) vis[j]=false; } } ll get_max(){ //得到最大链长,即素因子的个数 ll ans=0; for(int i=0;i
               
                

?

?

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇leetcode | Next Permutation 下一篇hdu1873看病要排队(优先队列)

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: