LightOJ 1035 Intelligent Factorial Factorization

题目:

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这个函数的,请点击打开我的博客

时间: 2024-08-08 05:21:03

LightOJ 1035 Intelligent Factorial Factorization的相关文章

lightoj 1074 spfa判断负环

Extended Traffic Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice LightOJ 1074 Appoint description:  System Crawler  (2016-05-03) Description Dhaka city is getting crowded and noisy day by day. Certain

lightoj Beginners Problems

很多以前写的丑代码 1000 - Greetings from LightOJ #include<math.h> #include<stdio.h> #include<stdlib.h> #include<string.h> int main(void) { int a,b,n,Case; scanf("%d",&n); Case=0; while(Case++,n--) { scanf("%d%d",&

LightOJ Beginners Problems 部分题解

相关代码请戳 https://coding.net/u/tiny656/p/LightOJ/git 1006 Hex-a-bonacci. 用数组模拟记录结果,注意取模 1008 Fibsieve's Fantabulous Birthday. 找规律题,左边列是1 3平方 5平方......下边行是1 2平方 4平方......,找到当前数被包夹的位置,然后处理一下位置关系,注意奇偶. 1010 Kinghts in Chessboard. 规律题,对于m,n大于2的情况下,使用交叉放置的方法

LightOJ 1030 Discovering Gold【概率】

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1030 题意:基础概率题. 代码: #include <stdio.h> #include <string.h> #include <vector> #include <string> #include <algorithm> #include <iostream> #include <iterator>

LightOJ - 1370 Bi-shoe and Phi-shoe

题目链接:http://lightoj.com/login_main.php?url=volume_showproblem.php?problem=1370 题目大意:给N个数a[i], N <= 1e6,问使 Φ(x) >= a[i] 成立的最小x的所有x的和是多少. 解题思路:我们知道的是对于素数 m 来说,phi[m] = m - 1.另一方面,对于一个合数 m 来说, phi[m] < phi[x] , x > m && x 是素数. 因此,我们可以认为,每

lightoj 1057 - Collecting Gold(状压dp)

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1057 题解:看似有点下记忆话搜索但是由于他是能走8个方向的也就是说两点的距离其实就是最大的x轴或y轴的差.然后只有15个藏金点状压一下加dfs就行了. #include <iostream> #include <cstring> #include <cstdio> #include <cmath> #define inf 0X3f3f3f

51Nod - 1035 最长的循环节

51Nod - 1035 最长的循环节 正整数k的倒数1/k,写为10进制的小数如果为无限循环小数,则存在一个循环节,求<=n的数中,倒数循环节长度最长的那个数,假如存在多个最优的答案,输出所有答案中最大的那个数. 1/6= 0.1(6) 循环节长度为1 1/7= 0.(142857) 循环节长度为6 1/9= 0.(1)  循环节长度为1 Input 输入n(10 <= n <= 1000) Output 输出<=n的数中倒数循环节长度最长的那个数 Input示例 10 Outp

LeetCode172 Factorial Trailing Zeroes. LeetCode258 Add Digits. LeetCode268 Missing Number

数学题 172. Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. (Easy) 分析:求n的阶乘中末位0的个数,也就是求n!中因数5的个数(2比5多),简单思路是遍历一遍,对于每个数,以此除以5求其因数5的个数,但会超时. 考虑到一个数n比他小

POJ 1035 Spell checker

题目链接 http://poj.org/problem?id=1035 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 24142   Accepted: 8794 Description You, as a member of a development team for a new spell checking program, are to write a module that will check the cor