您的位置:首页 > 其它

LightOJ 1035 Intelligent Factorial Factorization

2012-12-02 20:20 375 查看
题目链接 http://lightoj.com/volume_showproblem.php?problem=1035
题意:计算N!的素数分解式,N最大100,注意输出格式

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

const int NUM=105;
int prime[NUM],indexmax[NUM],factor[NUM];          //定义index[NUM]会编译错误……LightOJ是有多纠结……
int Num_Prime=0,id;
bool visit[NUM];
int n;

void Prime ()           //素数打表prime数组从1开始
{
    for (int i=2;i<NUM;i++)
		if (!visit[i])
		{
			prime[++Num_Prime]=i;
			for (int j=i+i;j<NUM;j+=i)
				visit[j]=true;
		}
}

void Input ()
{
	id=0;
	scanf ("%d",&n);
	memset(indexmax,0,sizeof(indexmax));
	memset(factor,0,sizeof(factor));
	for (int k=n;k>1;k--)                //factor数组从0开始
	{
		int temp=k,t;
		for (int i=1;i<=Num_Prime && prime[i] <= temp; i++)
		{
			t=0;
			while (temp%prime[i] == 0)
			{
				t++;
				temp/=prime[i];
			}
			if (t!=0)
			{
				if (indexmax[prime[i]] == 0)
					factor[id++]=prime[i];   //记录最小公倍数中的素因子
				indexmax[prime[i]]+=t;		     //记录素因子p[i]的个数
			}
		}
	}	
}

void Deal ()
{
	printf("%d =",n);
	sort(factor,factor+id);
	for (int i=0;i<id;i++)
	{
		printf(" %d (%d)",factor[i],indexmax[factor[i]]);
		if (i!=id-1)
			printf(" *");
	}
	printf("\n");
}

int main ()
{
	int T;
	scanf("%d",&T);
	Prime ();
	for (int cas=1;cas<=T;cas++)
	{
		Input ();
		printf("Case %d: ",cas);
		Deal ();
	}
	return 0;
}

/*
1
17

Out:
Case 1: 17 = 2 (15) * 3 (6) * 5 (3) * 7 (2) * 11 (1) * 13 (1) * 17 (1)
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: