【约数定理+数学】HDU - 6069 C - Counting Divisors

C - Counting Divisors

 HDU - 6069

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

For example, d(12)=6because 1,2,3,4,6,12are 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 33 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

如果一个1e12次方量级的数字a,在被[1, 1e6]内的素数分解了之后,剩下的数字还不是1,那么剩下来的那个数字肯定是一个大于1e6的质数

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1e6+10;
const ll mod=998244353;
bool vis[maxn];
ll prime[maxn];
int tot=0;
ll l,r,k;
struct node
{
    ll x,d;
}a[maxn];

void init() //O(n)素数筛
{
    vis[1]=1;
    for(int i=2;i<maxn;i++)
    {
        if(!vis[i]) prime[++tot]=i;
        for(int j=1;j<=tot&&i*prime[j]<maxn;j++)
        {
            vis[i*prime[j]]=1;
            if(i%prime[j]==0) break;
        }
    }
}

void work(ll p)
{
    ll idx=(l+p-1)/p*p; //找到l-r中第一个p的倍数
    for(ll i=idx;i<=r;i+=p) //idx-r,每p个增加,计算这个数的素因子p的个数,更新d
    {
        ll t=i-l+1,cnt=0;
        while(a[t].x%p==0)
        {
            a[t].x/=p;
            cnt++;
        }
        a[t].d*=(cnt*k%mod+1);
        a[t].d%=mod;
    }
}

int main()
{
    init();
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%lld%lld%lld",&l,&r,&k);
        ll c=0;
        for(ll i=l;i<=r;i++)
        {
            a[++c].x=i,a[c].d=1; 
        }

        for(int i=1;i<=tot;i++)
        {
            if(prime[i]*prime[i]>r) break; //剪枝,我们只计算比sqrt(r)小的素因子
            work(prime[i]);
        }

        ll ans=0;
        for(int i=1;i<=c;i++)
        {
            if(a[i].x!=1) a[i].d=a[i].d*(k+1)%mod; //只会有1个或0个比sqrt(x)大的素因子
            ans=(ans+a[i].d)%mod;
        }
        printf("%lld\n",ans);
    }
    return 0;
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值