hdu6069 Counting Divisors(数论+约数定理)

题目链接
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 :
在这里插入图片描述

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
思路:方法都是很容易想到的,肯定是约数定理,k次方的话无非就是再乘k嘛,问题就是如何l和r很大,如何解决超时的问题?由于r-l不超过1e6,所以我么将【l,r】进行平移到【0,r-l】,然后打表1e6的素数,对于那些超过1e6的素数我们最后只要判断一下a【i】是否大于1就知道它本身是不是个素数了。有个细节注意一下,我们枚举素数的时候要从【l,r】中第一个是prime【i】的倍数开始算。

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=1e6+5;
const int mod=998244353;
int n,d,prime[maxn],top=0,pos[maxn],isprime[maxn]={0};
ll num[maxn],a[maxn];
void Prime(int n)
{
    for(int i=2;i<=n;++i)
    {
        if(!isprime[i]) prime[++top]=i,pos[i]=pos[i-1]+1;
        else pos[i]=pos[i-1]; 
        for(int j=1;j<=top;++j)
        {
            if(i*prime[j]>n) break;
            isprime[i*prime[j]]=1;
            if(i%prime[j]==0) break;
        }
    }
}
int main()
{
    int T;
    Prime(maxn);
    scanf("%d",&T);
    while(T--)
    {
        ll ans=0,l,r,k;
        scanf("%lld %lld %lld",&l,&r,&k);
        for(int i=0;i<=r-l;++i) num[i]=1,a[i]=i+l;
        for(int i=1;i<=top;++i)
        {
            ll s=(l/prime[i])*prime[i];
            if(s<l) s+=prime[i];//【l,r】中第一个是prime【i】的倍数
            for(ll j=s;j<=r;j+=prime[i])
            {
                ll cnt=0;
                while(a[j-l]%prime[i]==0)
                {
                    a[j-l]/=prime[i];
                    cnt++;
                }
                num[j-l]=(num[j-l])*(cnt*k+1)%mod;
            }
        }
        for(int i=0;i<=r-l;++i)
        {
            if(a[i]>1) num[i]=(num[i]*(k+1))%mod;
            ans=(ans+num[i])%mod;
         } 
         printf("%lld\n",ans);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值