HDU6070 Counting Divisors(数论)

本文介绍了一道关于计算特定范围内整数的除数数量的算法题目,提供了详细的解析和实现代码。通过数学原理和高效算法,解决了给定区间内每个数的k次幂的除数计数问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Counting Divisors

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 3025    Accepted Submission(s): 1125


Problem 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))mod998244353

 

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
对于任意一个数n,我们都可以表示成n=p1c1p2c2....pkck这种形式,对于每一个p
c
都有(c+1)个因子,
所以n的因子个数(c1+1)*(c2+1)*...*(ck+1)个,因此我们只需要遍历[l,r]之间的所有数,对于每一个数求出他的p和c

又因为r<=1e12,所以r
最大为1e6,对于大于r 的值,直接乘k+1即可,因为一定为素数,所以他的因子个数是k+1个
de

#include<bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long LL;
const int N=1e6+10;
const LL MOD=998244353;
bool vis[N];
int prime[N/10],t,tot=0;
LL le,ri,k,num[N],sum[N];//sum记录约数的个数
void isprime()//筛N以内素数
{
    for(int i=2;i<N;i++)
    {
        if(!vis[i])prime[tot++]=i;
        for(int j=0;j<tot&&i*prime[j]<N;j++)
        {
            vis[i*prime[j]]=true;
            if(i%prime[j]==0)break;
        }
    }
}
LL solve()
{
    for(LL i=0;i<tot&&prime[i]*prime[i]<=ri;i++)
    {
        LL tmp=le;
        if(tmp%prime[i])//将tmp变成>=le的可以整除prime[i]的最小的数
        tmp=(tmp/prime[i]+1)*prime[i];
        for(LL j=tmp;j<=ri;j+=prime[i])
        {
            LL cnt=0;
            while(num[j-le]%prime[i]==0)
            {
                num[j-le]/=prime[i];
                cnt++;
            }
            sum[j-le]=(sum[j-le]*(cnt*k+1)%MOD)%MOD;
        }
    }
        LL ans=0;
        for(LL i=0;i<=ri-le;i++)
        {
            if(num[i]>1)//因子>sqrt(ri)的,本身一定是素数
               ans=(ans+sum[i]*(k+1)%MOD)%MOD;
            else
                ans=(ans+sum[i])%MOD;
        }

    return ans;
}
int main()
{
    isprime();
    scanf("%d",&t);
    while(t--)
    {
        scanf("%lld%lld%lld",&le,&ri,&k);
        for(LL i=0;i<=ri-le;i++)
            sum[i]=1,num[i]=le+i;
        printf("%lld\n",solve());
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值