当前位置:网站首页>AcWing 179.阶乘分解 题解
AcWing 179.阶乘分解 题解
2022-07-06 09:14:00 【爱吃章鱼的怪兽】
AcWing 197. 阶乘分解 题解
题目描述
给定整数 N N N , 试把阶乘 N ! N! N! 分解质因数,按照算术基本定理的形式输出分解结果中的 p i p_i pi和 c i c_i ci即可
输入格式
一个整数 N N N
输出格式
N ! N! N! 分解质因数后的结果,共若干行,每行一对 p i , c i p_i,c_i pi,ci ,表示含有 p i c I p_i^{c_I} picI项。按照 p i p_i pi 从小到大的顺序输出
数据范围
3 ≤ N ≤ 1 0 6 3 \le N \le 10^6 3≤N≤106
题解
对于 N ! N! N! 的质因子,我们可以判断,最大的质因子不超过 N N N
证明:
通过算数基本定理
对于任意正整数有
x = p 1 α 1 ⋅ p 2 α 2 ⋅ p 3 α 3 … ⋅ p n α n x=p_1^{\alpha_1} · p_2^{\alpha_2}·p_3^{\alpha_3}\dots·p_n^{\alpha_n} x=p1α1⋅p2α2⋅p3α3…⋅pnαn
其中 P i P_i Pi 为 x x x 的质因子
对于 1 1 1 ~ N N N 的每个整数都可以分解成如算数基本定理的形式
因此 N ! N! N! 的算数基本定理为前 N N N 个数的算术基本定理的乘积,其中不存在超过 N N N 的质因子
得证
因此我们只需要筛出 1 1 1~ N N N 的质数即可
首先利用线性筛筛出其中的质数
public static void init(int n)
{
Arrays.fill(isPrime,true);
for(int i=2;i<=n;i++)
{
if(isPrime[i])
Primes[cnt++]=i;
for(int j=0;j<cnt;j++)
{
int p=Primes[j];
if(p*i>n)
break;
isPrime[p*i]=false;
if(i%p==0)
break;
}
}
}
筛得质数后
我们计算每个质数的阶数
即计算 1 1 1 ~ N N N 中有多少个数有质数 p p p
易得有 N / P N/P N/P个数有质数p,这些数是p的倍数,因此这些数有因子 p p p
又根据 N / P 2 N/P^2 N/P2个数中含有因子 p 2 p^2 p2
依次类推,我们可以计算出当 N / P k N/P^k N/Pk时为0,即质数 P P P对应的最大阶数为 k k k
计算阶数的代码如下
for(int i=0;i<cnt;i++)
{
int res=0;
int p=Primes[i];
int tmp=n;
while(tmp>0)
{
res+=tmp/p;
tmp/=p;
}
System.out.println(Primes[i]+" "+res);
}
完整代码
import java.io.*;
import java.util.*;
public class Main{
static int n;
static final int N=(int)1e6+10;
static boolean[] isPrime=new boolean[N];
static int[] Primes=new int[N];
static int cnt=0;
public static void init(int n)
{
Arrays.fill(isPrime,true);
for(int i=2;i<=n;i++)
{
if(isPrime[i])
Primes[cnt++]=i;
for(int j=0;j<cnt;j++)
{
int p=Primes[j];
if(p*i>n)
break;
isPrime[p*i]=false;
if(i%p==0)
break;
}
}
}
public static void main(String[] agrs) throws IOException
{
BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
n=Integer.parseInt(reader.readLine());
init(n);
for(int i=0;i<cnt;i++)
{
int res=0;
int p=Primes[i];
int tmp=n;
while(tmp>0)
{
res+=tmp/p;
tmp/=p;
}
System.out.println(Primes[i]+" "+res);
}
}
}
边栏推荐
- Mysql 其他主机无法连接本地数据库
- MySQL flush operation
- 报错解决 —— io.UnsupportedOperation: can‘t do nonzero end-relative seeks
- 【博主推荐】C# Winform定时发送邮箱(附源码)
- Django运行报错:Error loading MySQLdb module解决方法
- 02-项目实战之后台员工信息管理
- Solve the problem that XML, YML and properties file configurations cannot be scanned
- CSDN博文摘要(一) —— 一个简单的初版实现
- One click extraction of tables in PDF
- Win10: how to modify the priority of dual network cards?
猜你喜欢
Neo4j installation tutorial
C language advanced pointer Full Version (array pointer, pointer array discrimination, function pointer)
CSDN问答标签技能树(五) —— 云原生技能树
[C language foundation] 04 judgment and circulation
IDEA 导入导出 settings 设置文件
Csdn-nlp: difficulty level classification of blog posts based on skill tree and weak supervised learning (I)
Summary of numpy installation problems
[recommended by bloggers] asp Net WebService background data API JSON (with source code)
CSDN Q & a tag skill tree (V) -- cloud native skill tree
Dotnet replaces asp Net core's underlying communication is the IPC Library of named pipes
随机推荐
Postman Interface Association
自动机器学习框架介绍与使用(flaml、h2o)
Installation and use of MySQL under MySQL 19 Linux
CSDN question and answer tag skill tree (I) -- Construction of basic framework
CSDN问答模块标题推荐任务(二) —— 效果优化
Mysql21 user and permission management
JDBC原理
数数字游戏
CSDN问答标签技能树(一) —— 基本框架的构建
[number theory] divisor
Ansible practical Series III_ Task common commands
Ansible practical Series II_ Getting started with Playbook
windows无法启动MYSQL服务(位于本地计算机)错误1067进程意外终止
Development of C language standard
记某公司面试算法题:查找一个有序数组某个数字出现的次数
安装numpy问题总结
Postman uses scripts to modify the values of environment variables
There are three iPhone se 2022 models in the Eurasian Economic Commission database
Asp access Shaoxing tourism graduation design website
[Li Kou 387] the first unique character in the string