1295. X的因子链
题目链接
1295. X的因子链
输入正整数 \(X\),求 \(X\) 的大于 \(1\) 的因子组成的满足任意前一项都能整除后一项的严格递增序列的最大长度,以及满足最大长度的序列的个数。
输入格式
输入包含多组数据,每组数据占一行,包含一个正整数表示 \(X\)。
输出格式
对于每组数据,输出序列的最大长度以及满足最大长度的序列的个数。
每个结果占一行。
数据范围
\(1≤X≤2^{20}\)
输入样例:
2
3
4
10
100
输出样例:
1 1
1 1
2 1
2 2
4 6
解题思路
分解质因数,组合
先分解质因数:\(a=b_1^{c_1}\times b_2^{c_2}\times \dots b_n^{c_n}\),可知要使其整除序列最长,即第一个数能整除后面所有数,不妨猜想这个数为 \(b_1,b_2,\dots,b_n\) 中的任意一个,则可知其最长长度为 \(c_1+c_2+\dots +c_n\),反证,如果不是,由于最大数一定是 \(a\),则第一个数一定是 \(b_1,b_2,\dots,b_n\) 中的组合,矛盾,故最长长度 \(s\) 即为 \(c_1+c_2+\dots +c_n\),方案数即为组合数:\(C_s^{c_1} \times C_{s-c_1}^{c_2}\times \dots \times C_{s-c_1-c_2-\dots-c_{n-2}}^{n-1}\)
- 时间复杂度:\(O(logn)\)
代码
// Problem: X的因子链
// Contest: AcWing
// URL: https://www.acwing.com/problem/content/1297/
// Memory Limit: 64 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)
// %%%Skyqwq
#include
//#define int long long
#define help {cin.tie(NULL); cout.tie(NULL);}
#define pb push_back
#define fi first
#define se second
#define mkp make_pair
using namespace std;
typedef long long LL;
typedef pair PII;
template bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; }
template bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; }
template void inline read(T &x) {
int f = 1; x = 0; char s = getchar();
while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); }
while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar();
x *= f;
}
const int N=1000005;
int n,m,a[N],b[N],c[N];
void div()
{
for(int i=2;i*i<=n;i++)
if(n%i==0)
{
int t=0;
b[++m]=i;
while(n%i==0)t++,n/=i ;
c[m]=t;
}
if(n>1)b[++m]=n,c[m]=1;
}
int C(int m,int n)
{
int res=1;
for(int i=1,j=m;i<=n;i++,j--)res*=j,res/=i;
return res;
}
int main()
{
while(cin>>n)
{
m=0;
div();
int res=0,cnt=1;
for(int i=1;i<=m;i++)res+=c[i];
int s=res;
for(int i=1;i