【2017多校】第四场C题:CountingDivisors

来源:互联网 发布:中国儿童编程网 编辑:程序博客网 时间:2024/05/21 08:53

预备知识:

一个大于1的正整数N,如果它的标准分解式为:N=Pa11Pa22Pann

那么它的正因数个数为σ0(N)=(1+a1)(1+a2)(1+an)


题目:Counting Divisors

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

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

思路:

比赛的时候被yh提醒可以应用算术基本定理算出一个数所有的因子个数,基本确定就是要围绕分解质因数来做了。最先想到的是PR。遍历返回的factors数组,记录每个质因子出现的次数(map实现)。

但是这样每个数都要遍历,再加上PR的时间复杂度,十有八九会T,交了一发果然T了……后来也没想到更好的算法,于是作罢。


赛后看到题解:“找到小于r的所有素数,然后在[l,r]区间遍历所有这些素数的倍数”。读起来比较好理解,但是看到标程的时候出现了很多问题……下面在代码里细讲,从主程序开始看:

#include<cstdio>typedef long long ll;const int N=1000010,P=998244353;int Case,i,j,k,p[N/10],tot,g[N],ans;ll n,l,r,f[N];bool v[N];inline void work(ll p)//进入work()函数{    for(ll i=l/p*p; i<=r; i+=p)//这里令i先除以p,再乘以p,就可以在小于等于L的区间里找到第一个能被p整除的数(重点)。    //然后我们每次都令i+=p,获得下一个能被p整除的数。        if(i>=l)        {            int o=0;            while(f[i-l]%p==0)                f[i-l]/=p,o++;            g[i-l]=1LL*g[i-l]*(o*k+1)%P;//应用算数基本定理。        }}int main(){    for(i=2; i<N; i++)    {        if(!v[i])            p[tot++]=i;        for(j=0; j<tot&&i*p[j]<N; j++)        {            v[i*p[j]]=1;            if(i%p[j]==0)                break;        }    }    //到这里,以上是素数筛。    scanf("%d",&Case);    while(Case--)    {        scanf("%lld%lld%d",&l,&r,&k);        n=r-l;        for(i=0; i<=n; i++)            f[i]=i+l,g[i]=1;//f数组记录从L往右第i个数,g表示这个数的因子个数,也就是我们要应用算数基本定理求出的。        for(i=0; i<tot; i++)//tot是[1~根号r]素数个数。        {            if(1LL*p[i]*p[i]>r)//如果质数的平方大于右边界了,那就直接跳出。            //因为如果这个质数的平方大于右边界,后面的质数的指数就一定是1或0了。                break;            work(p[i]);//我们找到一个符合条件的质数,跳入work()函数。        }        for(ans=i=0; i<=n; i++)        {            if(f[i]>1)//对没有被质数除尽的,和本身是大于根号r的质数,也要再算一遍。                g[i]=1LL*g[i]*(k+1)%P;            ans=(ans+g[i])%P;        }        printf("%d\n",ans);    }    return 0;}

感想

太弱了,分块筛的思想太重要了,又想起了上一次求区间第k大的题……不过能把要看的每一道题彻底搞懂,也算是收获不小。

原创粉丝点击