当前位置:网站首页>AcWing 179. Factorial decomposition problem solution
AcWing 179. Factorial decomposition problem solution
2022-07-06 11:16:00 【Octopus loving monster】
AcWing 197. Factorial decomposition Answer key
List of articles
Title Description
Given integer N N N , Try factorial N ! N! N! Decomposing prime factor , According to the form of the basic theorem of arithmetic, output the p i p_i pi and c i c_i ci that will do
Input format
An integer N N N
Output format
N ! N! N! The result of decomposing the prime factor , There are several lines , Each row of a pair of p i , c i p_i,c_i pi,ci , Means to contain p i c I p_i^{c_I} picI term . according to p i p_i pi Sequential output from small to large
Data range
3 ≤ N ≤ 1 0 6 3 \le N \le 10^6 3≤N≤106
Answer key
about N ! N! N! The quality factor of , We can judge , The maximum quality factor does not exceed N N N
prove :
Through the basic theorem of arithmetic
For any positive integer, there are
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
among P i P_i Pi by x x x The quality factor of
about 1 1 1 ~ N N N Each integer of can be decomposed into the form of basic theorem of arithmetic
therefore N ! N! N! The basic theorem of arithmetic is the former N N N The product of the basic arithmetic theorem of numbers , There is no more than N N N The quality factor of
Obtain evidence
So we just need to sift out 1 1 1~ N N N The prime number of
First, use the linear sieve to screen out the prime numbers
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;
}
}
}
After sifting the prime number
We calculate the order of each prime
Computation 1 1 1 ~ N N N How many numbers in have prime numbers p p p
Easy to get N / P N/P N/P Numbers have prime numbers p, These numbers are p Multiple , So these numbers have factors p p p
According to N / P 2 N/P^2 N/P2 The number contains factors p 2 p^2 p2
By analogy , We can calculate when N / P k N/P^k N/Pk When is 0, Prime number P P P The corresponding maximum order is k k k
The code for calculating the order is as follows
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);
}
Complete code
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);
}
}
}
边栏推荐
猜你喜欢
Error connecting to MySQL database: 2059 - authentication plugin 'caching_ sha2_ The solution of 'password'
[Thesis Writing] how to write function description of jsp online examination system
Django运行报错:Error loading MySQLdb module解决方法
Some problems in the development of unity3d upgraded 2020 VR
MySQL master-slave replication, read-write separation
基于apache-jena的知识问答
1. Mx6u learning notes (VII): bare metal development (4) -- master frequency and clock configuration
CSDN question and answer module Title Recommendation task (I) -- Construction of basic framework
In the era of DFI dividends, can TGP become a new benchmark for future DFI?
Neo4j installation tutorial
随机推荐
PyCharm中无法调用numpy,报错ModuleNotFoundError: No module named ‘numpy‘
Pytorch基础
Why is MySQL still slow to query when indexing is used?
Development of C language standard
Csdn-nlp: difficulty level classification of blog posts based on skill tree and weak supervised learning (I)
CSDN question and answer module Title Recommendation task (I) -- Construction of basic framework
Unable to call numpy in pycharm, with an error modulenotfounderror: no module named 'numpy‘
Some problems in the development of unity3d upgraded 2020 VR
Project practice - background employee information management (add, delete, modify, check, login and exit)
Error reporting solution - io UnsupportedOperation: can‘t do nonzero end-relative seeks
LeetCode #461 汉明距离
[ahoi2009]chess Chinese chess - combination number optimization shape pressure DP
TCP/IP协议(UDP)
Installation and use of MySQL under MySQL 19 Linux
引入了junit为什么还是用不了@Test注解
AcWing 179.阶乘分解 题解
Neo4j installation tutorial
[download app for free]ineukernel OCR image data recognition and acquisition principle and product application
02 staff information management after the actual project
【博主推荐】C#生成好看的二维码(附源码)