X-factor Chains(POJ3421 素数)

本文探讨了X-factorChains问题的求解方法,通过分解正整数X为素数因子,利用埃氏筛法寻找所有可能的素数,并计算X-factorChains的最大长度及相应链条的数量。
X-factor Chains
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 6212 Accepted: 1928

Description

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

1 = X0X1X2, …, 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
100=2*2*5*5;
所以最长为4,然后对2,2,5,5排列组合,但一直RE,TLE
 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 using namespace std;
 5 #define LL long long
 6 #define Max ((1<<20)+2)
 7 bool a[Max];
 8 int prime[Max];
 9 int num[Max];
10 LL init[25];
11 void get_prime()    //埃氏筛法选取素数
12 {
13     int i,j,p=0;
14     memset(a,1,sizeof(a));
15     a[0]=a[1]=0;
16     for(i=2;i<=Max;i++)
17     {
18         if(a[i]==1)
19         {
20             prime[p++]=i;
21             for(j=i*2;j<Max;j+=i)
22                 a[j]=0;
23         }
24     }
25     init[1]=1;
26     for(i=2;i<=20;i++)
27         init[i]=i*init[i-1];
28     return;
29 }
30 int main()
31 {
32     int ans,n,i,j,k,u;
33     get_prime();
34     LL p,t;
35     freopen("in.txt","r",stdin);
36     while(scanf("%d",&n)!=EOF)
37     {
38         ans=0,k=0,t=1;
39         i=0;
40         while(n>1)
41         {
42             if(n%prime[i]==0)
43             {
44                 u=0;
45                 while(n%prime[i]==0)
46                 {
47                     n=n/prime[i];
48                     u++;
49                 }
50                 t*=init[u];
51                 ans+=u;
52                 k++;
53             }
54             if(a[n]==1)
55             {
56                 ans++;
57                 break;
58             }
59             i++;
60         }
61         p=init[ans];
62         LL s=p/t;
63         printf("%d %I64d\n",ans,s);
64     }
65     return 0;
66 }

 

转载于:https://www.cnblogs.com/a1225234/p/5187585.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值