2017多校训练Contest4: 1003 Counting Divisors hdu6069

本文介绍了一种高效的算法,用于计算给定区间[l, r]内所有整数的k次幂约数个数之和,并通过模998244353的操作来解决可能的大数问题。该算法首先预处理出小于等于100万的所有质数及其莫比乌斯函数和欧拉函数值,然后利用这些信息高效地计算出答案。

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

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

注意到r-l<=10^6

所以我们考虑把l到r之间的所有数都分解质因数

如果一个个枚举的话复杂度是sqrt的,无法接受

因此我们考虑枚举每一个质因数对每个位置的贡献,这样复杂度就是log级别的了

最后k次方我们只要考虑把每个质因数个数翻k倍即可

如果p=2^x1*3^x2*……^z^xn

那么约数个数即为(x1+1)(x2+1)……(xn+1)

#include<map>
#include<cmath>
#include<queue>
#include<vector>
#include<cstdio>
#include<string>
#include<cstring>
#include<cassert>
#include<iostream>
#include<algorithm>
using namespace std;
long long mod=998244353;
int mu[1000101],fai[1000101];
long long f[1000101];
long long prime[1000101];//mu莫比乌斯 fai欧拉 
bool check[1000101]; 
int n,tot;
inline void findfai()
{
    memset(check,false,sizeof(check));
    fai[1]=1; mu[1]=1;
    int i,j;
    for(i=2;i<=1000000;i++)
    {
        if(!check[i])
        {
            tot++;
            prime[tot]=i;
            fai[i]=i-1; mu[i]=-1;
        }
        for(j=1;j<=tot;j++)
        {
            if(i*prime[j]>1000000)
                break;
            check[i*prime[j]]=true;
            if(i%prime[j]==0)
            {
                fai[i*prime[j]]=fai[i]*prime[j]; mu[i*prime[j]]=0;
                break;
            }
            else
                fai[i*prime[j]]=fai[i]*(prime[j]-1); mu[i*prime[j]]=-mu[i];
        }
    }
}
long long a[1000101];
int main()
{
    findfai();
    int T;
    scanf("%d",&T);
    while(T>0)
    {
        T--;
        long long l,r,k;
        scanf("%lld%lld%lld",&l,&r,&k);
        int i;
        long long j;
    //    memset(f,0,sizeof(f));
        for(i=1;i<=r-l+1LL;i++)
        {
            f[i]=1;
            a[i]=(long long)i+l-1LL;
        }
        for(i=1;i<=tot;i++)
        {
            long long ll=((l-1LL)/prime[i]+1LL)*prime[i];
            for(j=ll;j<=r;j+=prime[i])
            {
                int sum=0;
                while(a[j-l+1]%prime[i]==0)
                {
                    sum++;
                    a[j-l+1]/=prime[i];
                }
                f[j-l+1]=f[j-l+1]*((long long)sum*k+1LL)%mod;
            }
        }
        for(i=1;i<=r-l+1;i++)
            if(a[i]!=1)
                f[i]=f[i]*(k+1LL)%mod;
        long long ans=0;
        for(i=1;i<=r-l+1;i++)
            ans=(ans+f[i])%mod;
        printf("%lld\n",ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值