LightOJ 1035 Intelligent Factorial Factorization(素筛,唯一分解定理)

Intelligent Factorial Factorization

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的阶乘

题解
简单的质因子分解

代码

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <queue>
#include <math.h>
#include <string>
#include <string.h>
#include <map>
#include <set>
#include <unordered_map>
//#include <tr1/unordered_map>

using namespace std;
#define me(x,y) memset(x,y,sizeof x)
#define MIN(x,y) x < y ? x : y
#define MAX(x,y) x > y ? x : y

typedef long long ll;
typedef unsigned long long ull;

const long double INF = 0x3f3f3f3f;
const int MOD = 1e9+7;
const double eps = 1e-06;
const long double PI = std::acos(-1);
const int M=32768;
const int maxn = 1e3;


bool isprime[maxn+10];
vector<int> prime;
int sum_prime[maxn+10];

void euler(){
	isprime[1] = 1;
	for(int i = 2; i <= maxn; ++i){
		if(!isprime[i]) prime.push_back(i);
		for(int j = 0; j < prime.size() && prime[j]*i <= maxn; ++j){
			isprime[prime[j]*i]=1;
			if(i%prime[j] == 0) break;
		}
	}
}

int main() {
    int t,ca=1;
	cin>>t;
	euler();
	while(t--){
		ll n,sum=1;
		me(sum_prime,0);
		cin>>n;
		for(int k = 2; k <= n; ++k){
			int kk = k;
			for(int i = 0; i < prime.size() && kk >= (prime[i]*prime[i]); ++i){
				if(kk%prime[i]==0){
					int ans=0;
					while(kk%prime[i]==0){
						ans++;
						kk /= prime[i];
					}
					sum_prime[prime[i]] += ans;
				}
			}
			if(kk > 1){
				sum_prime[kk] += 1;
			}
		}
		
		printf("Case %d: %d =",ca++,n);
		for(int i = 2; i <= maxn; ++i){
			if(sum_prime[i]){
				if(i != 2) printf(" *");
				printf(" %d (%d)",i,sum_prime[i]);
			}
		}
		cout<<endl;
	}
    return 0;
}

/*

*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值