To find a prime number and n is O (n)

\(\mathcal{AIM}\)

We know that:
For a composite number \ (X \) have \ (x = p ^ {a_1
} _1 * p ^ {a_2} _2 * ... * p ^ {a_n} _n \) gives a present \ (n- \) find \ (X \ in [. 1, n-] \) , all \ (X \) decomposed \ (P \) the number of power and
, for example,
\ (n-= 12 is \)
\ (2 = 2 ^. 1 \ )
\ (3 = 3 ^ 1 \)
\ (4 = 2 ^ 2 \)
\ (5 = 5 ^ 1 \)
\ (6 = 2 ^ 1 * 3 ^ 1 \)
\ (7 = 7 ^ 1 \)
\ (8 = 2 ^ 3 \)
\ (9 = 3 ^ 2 \)
\ (10 = 2 ^ 1 * 5 ^ 1 \)
\ (11 = 11 ^ 1 \)
\ (12 = 2 ^ 2 * 3 ^ 1\)

digital The number of
2 10
3 5
5 2
7 1
11 1

\(\mathcal{Resolvent}\)

For a composite number \ (X \) have \ (x = p ^ {a_1 } _1 * p ^ {a_2} _2 * ... * p ^ {a_n} _n \)

\ (O (n \ sqrt n) \)

This is the most simple idea, which first record number is prime, then \ (n \) within all the numbers broken down

int cnt;
int prime[maxn],num[maxn];//prime -> 求出来的质数   num -> 每个数出现个数
bool vis[maxn];//欧拉筛里看其是否是质数
ols(n);//这是欧拉筛
for (int i=1;i<=n;++i)
    for (int j=1;j*j<=i&&j<=cnt;++j){
        int t=i;
        while (t%prime[j]==0)   ++num[prime[j]],t/=prime[j];
    }

\ (O (nlog_2n) \)

Can direct consideration of the overall demand
this method is also useful for a number of other questions

int cnt;
int prime[maxn],num[maxn];
bool vis[maxn];
ols(n);
for (int i=1;i<=cnt;++i){
    int t=prime[i],mi=1;//mi -> mi次幂
    while (t<=n){
        num[prime[i]]+=n/t*mi;
        t*=prime[i],++mi;
    }
}

\ (O (n) \)

For a number \ (x \)
\ (the X-/ p_1 \) clearly than \ (x \) small, if we know (x / p_1 \) \ answer, then \ (x \) contribution was \ ( x / p_1 \) contributions coupled with \ (p_1 \) is a contribution
but we \ (x / p_1 \) answers survive only increase the complexity
so we can turn cycle, \ (the X-\) first to \ (p_1 \) plus a contribution, then we can consider more than a \ (x / p_1 \) the
calculation of \ (x / p_1 \) when the answer will be one more, obviously we can always pass along, so that every the number only to contribute their minimum quality factor can be calculated

int cnt;
int prime[maxn],num[maxn],come[maxn];//come[i] -> i的最小质因子
bool vis[maxn];
ols(n);
for (int i=n;i>=2;--i){
    if (vis[i]){//如果是个合数
        num[come[i]]+=num[i];//最小质因子加上当前这个数要计算次数
        num[i/come[i]]+=num[i];//加上这个数需计算次数
        num[i]=0;//当前这个数没了
    }
}

Guess you like

Origin www.cnblogs.com/Morning-Glory/p/10961055.html