当前位置:网站首页>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);
}
}
}
边栏推荐
- Did you forget to register or load this tag 报错解决方法
- Invalid default value for 'create appears when importing SQL_ Time 'error reporting solution
- A brief introduction to the microservice technology stack, the introduction and use of Eureka and ribbon
- Classes in C #
- TCP/IP协议(UDP)
- Deoldify project problem - omp:error 15:initializing libiomp5md dll,but found libiomp5md. dll already initialized.
- SSM整合笔记通俗易懂版
- Esp8266 at+cipstart= "", "", 8080 error closed ultimate solution
- 【博主推荐】SSM框架的后台管理系统(附源码)
- QT creator create button
猜你喜欢
Asp access Shaoxing tourism graduation design website
Unable to call numpy in pycharm, with an error modulenotfounderror: no module named 'numpy‘
【博主推荐】asp.net WebService 后台数据API JSON(附源码)
[number theory] divisor
Some problems in the development of unity3d upgraded 2020 VR
Csdn-nlp: difficulty level classification of blog posts based on skill tree and weak supervised learning (I)
Deoldify项目问题——OMP:Error#15:Initializing libiomp5md.dll,but found libiomp5md.dll already initialized.
MySQL master-slave replication, read-write separation
CSDN博文摘要(一) —— 一个简单的初版实现
[Thesis Writing] how to write function description of jsp online examination system
随机推荐
Navicat 导出表生成PDM文件
图像识别问题 — pytesseract.TesseractNotFoundError: tesseract is not installed or it‘s not in your path
连接MySQL数据库出现错误:2059 - authentication plugin ‘caching_sha2_password‘的解决方法
MySQL master-slave replication, read-write separation
Remember a company interview question: merge ordered arrays
Are you monitored by the company for sending resumes and logging in to job search websites? Deeply convinced that the product of "behavior awareness system ba" has not been retrieved on the official w
Remember the interview algorithm of a company: find the number of times a number appears in an ordered array
MySQL21-用戶與權限管理
Principes JDBC
CSDN markdown editor
Ansible实战系列二 _ Playbook入门
记某公司面试算法题:查找一个有序数组某个数字出现的次数
CSDN问答标签技能树(一) —— 基本框架的构建
[number theory] divisor
数据库高级学习笔记--SQL语句
CSDN question and answer module Title Recommendation task (I) -- Construction of basic framework
Win10: how to modify the priority of dual network cards?
QT creator shape
Why can't I use the @test annotation after introducing JUnit
Knowledge Q & A based on Apache Jena