HDU 6069 Counting Divisors (素数)

Description

In mathematics, the function d(n) denotes the number of divisors of positive integer n.

For example, d(12)=6 because 1,2,3,4,6,12 are all 12 ‘s divisors.

In this problem, given l,r and k, your task is to calculate the following thing :

>(i=lrd(ik)) mod 998244353>

 

Input

The first line of the input contains an integer T(1T15) , denoting the number of test cases.

In each test case, there are 3 integers l,r,k(1lr1012,rl106,1k107) .

 

Output

For each test case, print a single line containing an integer, denoting the answer.

 

Sample Input

3
1 5 1
1 10 2
1 100 3

 

Sample Output

10
48
2302

 

题意

给出 l,r,k ,求 [l,r] 中所有数的 k 次幂的约数个数之和,最终结果 mod 998244353 。

 

思路

SPOJ 中有两道类似的题目,其中一个 k = 2 ,另一个 k = 3 ,然后区间长度最大为 1012 ,用到了杜教筛洲阁筛

不过对于这道题来说,用不到那么高深的知识,因为区间长度只有 106

 

对于任意一个大于 1 的正整数 x ,其素因子至多只有一个处于 (x,x] 这个区间内,其余全部小于等于 x

另外,通过素数定理我们可以知道,任意一个正整数 x 可以表示为 x=pc11×pc22×...×pcnn ,其中 pi 为互不相同的质数,此时 x 的所有因子个数为 (c1+1)×(c2+1)×...×(cn+1)

同理, xk 所有因子个数为 (c1×k+1)×(c2×k+1)×...×(cn×k+1)

 

于是我们考虑枚举 r 以内的所有质数 i ,并在区间 [l,r] 中对 i 的倍数计算其含有多少 i 这个质因子,统计结果。

随后考虑每个数在 r 以外的质因子,若有,则统计这部分。

累加该区间内所有数的统计结果即为最终答案。

 

AC 代码

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

typedef __int64 LL;
const int maxn = 1e6+10;
const int mod = 998244353;
LL prime[maxn],tot;
bool visit[maxn];
LL l,r,k;
LL num[maxn],sum[maxn];

void getprime()
{
    tot=0;
    memset(visit,0,sizeof(visit));
    for(int i=2; i<maxn; i++)
    {
        if(!visit[i])
        {
            prime[tot++]=i;
            for(int j=i+i; j<maxn; j+=i)
                visit[j]=1;
        }
    }
}

void solve()
{
    for(int i=0; i<=r-l; i++)
    {
        sum[i]=1;
        num[i]=i+l;
    }
    for(int i=0; prime[i]*prime[i]<=r; i++)
        for(LL j=(l/prime[i]+(l%prime[i]?1:0))*prime[i]; j<=r; j+=prime[i])
            if(j>=l)
            {
                LL res=0;
                while(num[j-l]%prime[i]==0)
                {
                    num[j-l]/=prime[i];
                    res++;
                }
                sum[j-l]=sum[j-l]*(res*k+1)%mod;
            }
    LL ans=0;
    for(int i=0; i<=r-l; i++)
    {
        if(num[i]>1)
            sum[i]=sum[i]*(k+1)%mod;
        ans=(ans+sum[i])%mod;
    }
    cout<<ans<<endl;
}

int main()
{
    getprime();
    int T;
    while(cin>>T)
    {
        for(int i=0; i<T; i++)
        {
            cin>>l>>r>>k;
            solve();
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值