质数筛与质因数分解

const int MAXN = 1e7;
int p[MAXN + 5], ptop;
bool pn[MAXN + 5];

void sieve() {
    int n = MAXN;
    pn[1] = 1;
    for(int i = 2; i <= n; i++) {
        if(!pn[i])
            p[++ptop] = i;
        for(int j = 1; j <= ptop; j++) {
            int t = i * p[j];
            if(t > n)
                break;
            pn[t] = 1;
            if(i % p[j] == 0)
                break;
        }
    }
    printf("ptop=%d\n", ptop);
    /*for(int i = 1; i <= ptop; ++i)
        printf("%d:%d\n", i, p[i]);*/
}
int fac[105][2], ftop;

void get_fac(int n) {
    ftop = 0;
    for(int i = 1; i <= ptop; ++i) {
        if(n % p[i] == 0) {
            fac[++ftop][0] = p[i];
            fac[ftop][1] = 0;
            while(n % p[i] == 0) {
                n /= p[i];
                ++fac[ftop][1];
            }
        }
    }
    if(n > 1) {
        fac[++ftop][0] = n;
        fac[ftop][1] = 1;
    }
}
posted @ 2020-01-22 13:08  KisekiPurin2019  阅读(78)  评论(0编辑  收藏  举报