Intelligent Factorial Factorization LightOJ - 1035

题目

Given an integer N, you have to prime factorize N! (factorial N).

Input

Input starts with an integer T (≤ 125), denoting the number of test cases.

Each case contains an integer N (2 ≤ N ≤ 100).

Output

For each case, print the case number and the factorization of the factorial in the following format as given in samples.

Case x: N = p1 (power of p1) * p2 (power of p2) * ...

Here x is the case number, p1, p2 ... are primes in ascending order.

Sample Input

3

2

3

6

Sample Output

Case 1: 2 = 2 (1)

Case 2: 3 = 2 (1) * 3 (1)

Case 3: 6 = 2 (4) * 3 (2) * 5 (1)


思路

素数分解。。。


代码

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <set>
#include <map>
#include <vector>

#define MAX_N 0x3F3F3F3F

using namespace std;

typedef long long ll;

map<int, int> pfactMap;
void primeFact(int n);

int main()
{
	int T, N;

	cin >> T;
	for (int i = 1; i <= T; i++) {
		cin >> N;
		pfactMap.clear();
		for (int n = N; n > 1; n--) {
			primeFact(n);
		}

		cout << "Case " << i << ": ";
		cout << N << " = ";
		map<int, int>::iterator it;
		int s;
		for (it = pfactMap.begin(), s = 0; it != pfactMap.end(); it++, s++) {
			cout << (it)->first << " (" << (it)->second << ")";
			if (s != pfactMap.size()-1)  cout << " * ";
		}
		cout << endl;
	}
    return 0;
}

void primeFact(int n) {
	for (int i = 2; i*i <= n; i++) {
		if (n % i == 0) {
//			pfactMap.insert(n%i);
			pfactMap[i]++;
			primeFact(n/i);
			return;
		}
	}
//	pfactMap.insert(n);
	pfactMap[n]++;
	return;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值