CF 1285 F. Classical?

Subject to the effect

Given array \ (A \) , selected from the two numbers, so that the least common multiple of the maximum, find the least common multiple of the value.

Limitations: the length \ (n-(2 \ n-Leq \ Leq 10 ^. 5) \) , \ (. 1 \ Le a_i \ Le. 5 ^ 10 \)

explain

Seemingly classic title, but requires some pre-knowledge.

Pre-knowledge. 1 \ (O (n-\ log \ n-) \) seeking function Mobius

Although the method of seeking the presence of an approximately linear, but this method is very short codes.
code show as below:

    mu[1]=1;
    for(int i=1;i<N;i++){
        for(int j=2*i;j<N;j+=i){
            mu[j]-=mu[i];
        }
    }

Why this approach is right?
This is guaranteed by the following theorem:

定理 1
\[ \Sigma_{d | n} \mu (d)=\begin{cases} 0 & n \neq 1 \\ 1 & n = 1 \end{cases}\]

Proof:
the n-1 = Clearly.
When n> 1, of the n uniquely decomposed, with k prime numbers:
\ [n = \ Pi ^ {k} _ {I =. 1} {P_i} ^ {m_i} \]
Now consider all factors of n, If the factor a \ (m_i \ GEQ 2 \) , defined by a Mobius function, its value is 0, the value considered \ (m_i = 0,1 \) factor.

Suppose \ (m_i \) in the number 1 is \ (U \) a, all \ (\ m_i) distribution with a \ (C_k ^ u \) species, then.
\ [\ Sigma_ {d | n
} \ mu (d) = \ Sigma _ {u = 1} ^ {k} (-1) ^ k C_k ^ u \] There binomial theorem:
\ [\ Sigma _ {u = 1} ^ {k
} (-1) ^ k C_k ^ u = 0 \] therefore:
\ [\ Sigma_ {D |} n-\ MU (D) = 0 \]

2 pre-knowledge

Theorem 2 Given the number of columns \ (A \) and a number \ (X \) , the number of columns \ (A \) with \ (X \) number of prime is:
\ [\ sum_ {D | X} \ MU (d) cnt_d \]
wherein, \ (cnt_d \) means that the number of columns \ (a \) how many number is \ (D \) multiples.

Limited to my level, we can not prove this theorem. But I know that probably means is that if \ (d = 1 \) so the total number is \ (x \) months, assuming that \ (x \) has a prime factor 2,3,5,11, you need to lose 2,3 concurrently, the number of multiples of 5 and 11, but this is only a factor of two the number multiple of three (6) has been repeated lost, so to be added back, and so is the coefficient Mobius function.

Pre-knowledge 3

A number \ (n-\) number of factors are \ (O (n \ log \ n) \) level.

step 1

Because of \ (the LCM (X, Y) = \ FRAC {XY} {G} \) , where \ (G \) represents \ (X \) and \ (Y \) is the greatest common factor. The next approach is that we want to enumerate g, and then attempt to quickly update the answer.

Enumeration \ (G \) , for all \ (G \) multiple \ (B \) , calculated \ (b / g \) into an array inside. In this array inside, if the two numbers \ (u \) and \ (v \) are relatively prime, then you can use \ (uvg \) updated the answer.

This is our current idea. Here, the assumption can instantly (may wish to \ (O (c) \) time) to find the maximum number of two arrays inside prime, feasible?

of course can. Because, \ (a_i \) of the range is \ (10 ^ 5 \) , \ (G \) Maximum enumeration \ (10 ^ 5 \) , the maximum number of times each is inserted it is an array factor number \ (d \) , so this step, the time spent is \ (O (10 ^ 5 * d) \) .

Step 2

Now sub-question is: to give you an array quickly find the two prime number, and their product is the greatest.

First, we need an empty stack.

Now, this array, we get a shot descending order. Now the big left little to the right.

Now this process from left to right, get a number \ (the X-\) , ready to throw the stack. However, before you throw first look, the stack and there is no number x relatively prime, which is calculated using the pre-knowledge of 2, 3, which requires a pre-knowledge \ (O (log \ n) \) time.

If the stack there \ (\ sigma \) th prime number, then pop \ (\ sigma \) th and (x \) \ prime number.
And encountered a number is prime, as far as possible so that the answer to the biggest update, let \ (\ Sigma \) -.

This is not to miss the optimal solution, because of the number to be inserted behind than it is now \ (x \) is still small, but the number of pop-up, pop-up is smaller than the last number.

Code

https://codeforces.com/contest/1285/submission/69300157

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int N = 1e5+1;

int n,m;
int mu[N];
int S[N],t;
vector<int> d[N];
int exist[N];
vector<int> v[N];   // v[g][i] := g*v[g][i] 的值是存在于a[i]的
int cnt[N];

int main() {
    ios::sync_with_stdio(0);
    cin>>n;
    mu[1]=1;
    for(int i=1;i<N;i++){
        for(int j=i;j<N;j+=i){
            if(i!=j) mu[j]-=mu[i];
            d[j].push_back(i);
        }
    }
    long long ans = 0;
    for(int x,i=1;i<=n;i++) {
        cin>>x;
        exist[x]++;
        ans = max(ans,1ll*x);
    }
    for(int g=1;g<N;g++) {
        for(int j=g;j<N;j+=g) {
            for(int k=0;k<exist[j];k++) v[g].push_back(j/g);
        }
        reverse(v[g].begin(),v[g].end());
        // if(v[g].size()) cout<<g<<" - ";
        for(auto x:v[g]) {
            // if(v[g].size()) cout<<x<<" "<<endl;
            int sigma = 0;
            for(auto u:d[x]) sigma += mu[u] * cnt[u];
            // cout<<sigma<<endl;
            while(sigma>0 && t) {
                int u = S[--t];
                if(__gcd(u,x)==1) {
                    ans = max(ans,1ll * g * u * x);
                    sigma--;
                }
                for(auto w:d[u]) {
                    cnt[w]--;   
                }
            }
            S[t++] = x;
            for(auto u:d[x]) cnt[u]++;
        }
        // if(v[g].size()) cout<<endl;
        while(t) {
            int u = S[--t];
            for(auto w:d[u]) {
                cnt[w]--;
            }
        }
    }
    cout<<ans<<endl;
    return 0;
}

Guess you like

Origin www.cnblogs.com/scnucjh/p/12232545.html