AcWing 1295. X的因子链

原题链接

考察:唯一分解定理

思路:

        很容易想到最长公共子序列,dp只能求出长度,不能求出个数.我们模拟样例,100的约数 2 4 5 10 20 25 50 100. 最长是4, 2 4 20 100也可以是5 10 20 100可以发现开始的数必然是素数,而序列可以表示成 2,2*2,2*2*5,2*2*5*2.可以发现下一项是这一项再*一个质因数.假设有x个质因数,序列就是这些质因数进行排列,因为有些数字是相同的,所以是多重集的全排列,而最长长度就是质因数的个数和

 1 #include <iostream>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <vector>
 5 using namespace std;
 6 typedef long long LL;
 7 const int M = 1600;
 8 LL g[M];
 9 int main()
10 {
11     int n;
12     g[0] = 1;
13     for(int i=1;i<=20;i++) g[i] = g[i-1]*i;
14     while(scanf("%d",&n)!=EOF)
15     {
16         int ans = 0,sum = 0;
17         LL res = 1;
18         for(int i=2;i<=n/i;i++)
19         {
20             if(n%i==0)
21             {
22                 int t = 0;
23                 while(n%i==0) n/=i,t++;
24                 res *=g[t]; sum+=t;
25             }
26         }
27         if(n>1) sum++;
28         res = g[sum]/res;
29         printf("%d %lld\n",sum,res);
30     }
31     return 0;
32 }

 

posted @ 2021-02-27 19:30  acmloser  阅读(41)  评论(0编辑  收藏  举报