HDU 6069 Counting Divisors(求因子)

来源:互联网 发布:sql server约束 编辑:程序博客网 时间:2024/05/16 04:33

Counting Divisors

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


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
31 5 11 10 21 100 3
 

Sample Output
10482302
 

Source
2017 Multi-University Training Contest - Team 4 
 
题意:
d(n)=n的因子数。 给你l r k,求如上的表达式的值。

POINT:
如何求每个(l-r)^k的因子我就不说了。
之前遍历每个l-r求出各个的因子数会TLE。
那么遍历他们可能存在的质因数,即从2 3 5……开始遍历。
遍历后的质数就不用考虑了,在过程中保存下答案就行了。
还有一个重点就是,大质数的因子数是k+1没错,但是要考虑到这个大质数有可能是别的树除下来得到的,所以要更新sum数组。这个在代码里注释。//大质数即大于1e6即没有打到表的。
思维还不够灵活,总是没有想到从反面考虑。
#include <stdio.h>#include <iostream>#include <string.h>#include <math.h>#include <algorithm>#include <map>using namespace std;#define LL long longconst LL MAXN=1e6+9;const LL p = 998244353;LL prime[MAXN+1];LL sum[MAXN],num[MAXN];void getPrime(){    memset(prime,0,sizeof(prime));    for(int i=2;i<=MAXN;i++)    {        if(!prime[i])prime[++prime[0]]=i;        for(int j=1;j<=prime[0]&&prime[j]<=MAXN/i;j++)        {            prime[prime[j]*i]=1;            if(i%prime[j]==0)                break;        }    }}int main(){    getPrime();    int cnt=(int)prime[0];    int T;    scanf("%d",&T);    while(T--)    {        LL l,r,k;        scanf("%lld %lld %lld",&l,&r,&k);        for(LL i=l;i<=r;i++)        {            sum[i-l]=1;            num[i-l]=i;        }        for(int i=1;i<=cnt;i++)        {            //求出l-r内第一个可以整除prime[i]的。            LL flag=l%prime[i];            LL fir;            if(!flag) fir=l;            else fir=(l-flag)+prime[i];            for(LL j=fir;j<=r;j+=prime[i])            {                int ci=0;                while(num[j-l]%prime[i]==0) ci++,num[j-l]/=prime[i];                (sum[j-l]*=ci*k+1)%=p;            }        }        LL ans=0;        for(LL i=l;i<=r;i++)        {            if(num[i-l]!=1) sum[i-l]=(sum[i-l]*(k+1))%p;//大质数不绝对是k+1,sum[i-l]可能大于1.这个wa了蛮久的.            (ans+=sum[i-l])%=p;        }        printf("%lld\n",ans);    }    return 0;}