首页 > 代码库 > HDU6069 Multi-4 素数筛

HDU6069 Multi-4 素数筛

被这一道题打崩,打表的题目啊~~~~

类似大区间的素数筛法(POJ2689)

题目链接

约数和定理:d(n) = (a1 + 1)(a2 + 1)……(an + 1){ai 指的是 质因数分解后质数pi的个数}

代码:

<link href="http://www.mamicode.com/a50.css" rel="stylesheet" type="text/css" media="screen">
<link href="http://www.mamicode.com/a50.css" rel="stylesheet" type="text/css" media="screen"> <link href="http://www.mamicode.com/a50.css" rel="stylesheet" type="text/css" media="screen">
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <iostream>
#define input freopen("Multi_4_1003.in", "r", stdin)
using namespace std;
const int N = 1e6 + 5;
typedef long long LL;
const int mod = 998244353;
int prime[N];
int primecnt;
bool vis[N];
LL ans[N];
LL num[N];

void getPrime()
{
    primecnt = 0;
    for(int i = 2; i <= N; i++)
    {
        if(!vis[i])
            prime[primecnt++] = i;
        for(int j = 0; j < primecnt && prime[j] <= N/i; j++)
        {
            vis[i*prime[j]] = 1;
            if(i % prime[j] == 0) break;
        }
    } 
}
LL l,r,k;
int t;
int main()
{
    // input;
    getPrime();
    scanf("%d", &t);
    while(t--)
    {
        scanf("%lld%lld%lld", &l, &r, &k);
        int m = sqrt(r+0.5);
        for(LL i = l; i <= r; i++)
        {
            ans[i-l] = 1;
            num[i-l] = i;
        }
        for(int i = 0; i < primecnt && prime[i] <= m; i++)
        {
            LL p = prime[i];
            LL d = l / p + (l % p > 0);
            if(d == 1) d = 2;
            for(LL j = d*p; j <= r; j += p)
            {
                if(num[j-l] % p == 0)
                {
                    LL cnt = 0;
                    while(num[j-l] % p == 0){
                        cnt++;
                        num[j-l] /= p;
                    }
                    cnt = (cnt*k+1) % mod;
                    ans[j-l] = (ans[j-l] * cnt) % mod;
                }
            }
        }
        LL res = 0;
        for(LL i = l; i <= r; i++)
        {
            if(num[i-l] > 1){
                res = (res + ans[i-l]*(k+1))%mod;
            }
            else
                res = (res + ans[i-l])%mod;
        }
        printf("%lld\n",res);    
    }
}




HDU6069 Multi-4 素数筛