AcWing 197. factorial decomposition (sieve) punch

Given an integer N, the test factorial N! Decomposition of the quality factor, the output in the form of decomposition of the Fundamental Theorem of Arithmetic  P I PI and  C I CI can.

Input Format

An integer N.

Output Format

N! Prime factor decomposition result, a total number of lines, one per line P I , C I PI, CI, containing from P C I I PICI items. According to P I in ascending order of the output pi.

data range

1N1061≤N≤106

Sample input:

5

Sample output:

2 3
3 1
5 1


Meaning of the questions: We require a factorial inside each number into its prime factors Minimalist quality factor is multiplied by the number of
ideas: we certainly can not ask a single number a, we should seek a range of a number of prime factors is how much we first prime number within a given range determined by sieving
and then we ask such a given range of the even number, it is actually / 2 can be, can take more than 3, 4 take the remainder is the same reason,
and 1-10 out 5, it becomes a factor of 2 (246 810) / 2 - "(1,2,3,4,5) and out again here, this operation is repeated the number of stars factor 2

#include<bits/stdc++.h>
#define maxn 1000005
#define mod 1000000007
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
int prime[maxn];
int vis[maxn];
int num;
map<ll,ll> mp;
void shai(ll r){
    vis[1]=1;
    for(int i=2;i<=r;i++){
        if(vis[i]==0){
            prime[num++]=i;
            for(int j=2*i;j<=r;j+=i){
                vis[j]=1; 
            } 
        }
    }
}
int main(){
    ll n;
    scanf("%lld",&n);
    shai(n);
    for(int i=0;i<num;i++){
        if(prime[i]>n) break;
        ll w=n;
        ll sum=0;
        while(w){
            sum+=w/prime[i];
            w/=prime[i];
        }
        printf("%lld %lld\n",prime[i],sum);
    }
}

 

Guess you like

Origin www.cnblogs.com/Lis-/p/10994940.html