LightOJ 1035 Intelligent Factorial Factorization

来源:互联网 发布:知足知不足的阅读答案 编辑:程序博客网 时间:2024/05/22 03:13

题目:

Description

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)


这个题目,首先要分析哪些素数会输出。

结论是很简单的,不超过n的素数都会输出,超过n的都不会输出。

而且,2一定在里面,也就是说,2一定是第一个,这样就很方便了。

代码:

#include<iostream>#include<stdio.h>using namespace std;int degree_in_fact(int m, int p){if (m)return degree_in_fact(m / p, p) + m / p;return 0;}bool isprime(int n){for (int i = 3; i*i <= n; i += 2)if (n%i == 0)return false;return true;}int main(){int t, n;cin >> t;for (int cas = 1; cas <= t; cas++){cin >> n;cout << "Case " << cas << ": " << n << " = 2 (" << degree_in_fact(n, 2) << ")";for (int i = 3; i <= n; i += 2)if (isprime(i))
cout << " * " << i << " (" << degree_in_fact(n, i) << ")";cout << endl;}return 0;}

不知道degree_in_fact这个函数的,请点击打开我的博客

2 0
原创粉丝点击