您的位置:首页 > 大数据 > 人工智能

POJ 3421 X-factor Chains(数论)(筛法)()

2016-05-21 22:31 495 查看
X-factor Chains

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 6370Accepted: 1973
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

题意:给你一个长度为m的整数序列,即1 = X0, X1, X2,
…, Xm = X

这个序列满足条件:Xi < Xi+1 and Xi | Xi+1 where a | b means a perfectly
divides into b.每一个是前一个的倍数。(都是X的因子)

让你求最大长度,和最多有多少中这样长度的序列。

思路:首先筛法求素数;然后计算每个不同因子的指数和,即总因子数,就是最大长度了。

然后数量=幂和的阶乘/各个幂阶乘的和

已经AC

,心中痛苦啊,因为 const int N = 1100005;//此处的数据要重视,本人wa了n次啊,大于1100005,小于10^7。


#include<iostream>//poj 3421
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
const int N = 1100005;//此处的数据要重视,本人wa了n次啊
const int M = 10005;

int num,n,index;
int prime[M],flag
,sum[M]; //存每个素因子的指数,即个数

void Init()//筛法求素数
{
for(int i=2;i<=N;i++)
{
if(flag[i])
continue;
prime[num++]=i;
for(int j=2;i*j<=N;j++)
flag[i*j]=1;
}
}

int main()
{
Init();
while(scanf("%d",&n)!=EOF)
{
index=0;//又一次放错了位置,哭死了
memset(sum,0,sizeof(sum));//初始化

for(int i=0;i<num;i++)
{
if(n%prime[i]==0)//找到所有因子
{
while(n%prime[i]==0)
{
sum[index]++;//计算每个因子的指数和
n/=prime[i];
}
index++;//别放在大括号外面了
}
if(n==1)
break;
}
//求所有因子的指数和
ll s=0,ans=1;
for(int i=0;i<index;i++)
s+=sum[i];

//求
for(ll i=2;i<=s;i++)//次数使用int也可以
ans*=i;
for(int i=0;i<index;i++)
{
for(int j=2;j<=sum[i];j++)
{
ans/=j;
}
}
printf("%lld %lld\n",s,ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: