X-factor Chains(POJ3421 素数)

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 }

 

你可能感兴趣的:(X-factor Chains(POJ3421 素数))