【hdu 6069】 Counting Divisors 【思维+数论】

本文介绍了一种计算特定范围内所有正整数的k次幂因子个数的方法,并提供了一个使用素数筛选算法实现的C++代码示例。

摘要生成于 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(1≤T≤15), denoting the number of test cases.

In each test case, there are 3 integers l,r,k(1≤l≤r≤1012,r−l≤106,1≤k≤107).

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

Source
2017 Multi-University Training Contest - Team 4
分析
1>首先明确 一个数的因数个数为多少 :
因为 n=p1^r1*p2^r2….pn^rn ;所以 因子个数为(r1+1)(r2+1)(rn+1) , 其k次方的因子个数为(r1*k+1)(r2*k+1)(rn*k+1) ,所以我们就要找素数来解。
2>剩下的不好描述看代码吧。

代码

#include<bits/stdc++.h>
#define LL long long
using namespace std;
const int MAXN = 1e6+500; 
const int MAXM = 1e5;
const LL mod =998244353;
bool su[MAXN];
int prm[MAXN],sz=0;
void init(){ // 这个素筛还是快一些 
     for(int i=2;i<MAXN;++i){
        if(!su[i]) prm[sz++]=i;
        for(int j=0;j<sz;++j){
            int t=i*prm[j];
            if(t>=MAXN) break;
            su[t]=1;
            if(i%prm[j]==0) break;
         }
     }
}

LL arr[MAXN],ans[MAXN];
LL size;
void solve(LL l,LL r,LL k){
    for(LL i=0;i<sz;i++){
        LL pos=(l+prm[i]-1)/prm[i]*prm[i]; // 这里通过计算可以求得 大于l的最小的可以被prm[i]整除的数字
        pos-=l;//得到位置
        while(pos<size){// 遍历区间,将prm[i]的倍数都筛掉 
            LL cnt=0;
            while(arr[pos]%prm[i]==0) { cnt++; arr[pos]/=prm[i];}
            ans[pos]=(ans[pos]*(cnt*k%mod+1))%mod;//更新个数
            pos+=prm[i];//优化,不断的加prm[i]一定是其倍数
        }
    }
//  puts("---------");
    LL sum=0;
    for(LL i=0;i<size;i++){
        if(arr[i]==1) ;// 说明这个数字可以被完全分解为1-1e6之内的素数
        //否则就说明 其剩下的一定是 一个素数,且次幂为1 
        else ans[i]=(ans[i]*(1*k%mod+1))%mod;

        sum=(sum+ans[i])%mod;
    }
    printf("%lld\n",sum%mod);
}
int main(){
    init();
    int t;cin>>t;
    while(t--){
        LL l,r,k;
        scanf("%lld%lld%lld",&l,&r,&k); 
        size=0;
        for(LL i=l;i<=r;i++) {
            ans[size]=1;
            arr[size++]=i;
        }
        solve(l,r,k);
     }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值