文章标题

来源:互联网 发布:淘宝店铺如何转让 编辑:程序博客网 时间:2024/05/22 14:22

Counting Divisors

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 :

(d((l)^k)+d(l+1)^k)+......+d(r^k)) mod 998244353

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≤10^12, r−l≤10^6, 1≤k≤10^7)
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

题意:求 l^k 到 r^k 的各因子的总和 。
分析, 根据素数定理,任意数都能由质数来表示,x=p1^c1*p2^c2…….*pi^ci
其中,p1到pi是不相等的质数,那么x的因子个数为
num=(c1+1)(c2+1)…….(ci+1);

那么,相应的 x^k的因子个数为
num=(c1*k+1)*(c2*k+1)……(ci*k+1);

同时,我们在求质因子时,只需要在根号x前找,x至多只有一个大于根号x 的因子。

枚举根号 r 内的质数 i ,在区间 [l,r] 寻找含有 i 的数(巧妙之处,因为 l 到 r 是连续的,故增加 i 的倍数 即下一个含有 i 的数)。

随后考虑每个数在 根号 x 以外的质因子,若有,则统计这部分。

累加该区间内所有数的统计结果即为最终答案。

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;typedef __int64 LL;const int maxn = 1e6+10;const int mod = 998244353;LL prime[maxn],tot;bool visit[maxn];LL l,r,k;LL num[maxn],sum[maxn];void getprime(){    tot=0;    memset(visit,0,sizeof(visit));    for(int i=2; i<maxn; i++)    {        if(!visit[i])        {            prime[tot++]=i;            for(int j=i+i; j<maxn; j+=i)                visit[j]=1;        }    }}void solve(){    for(int i=0; i<=r-1; i++)    {        sum[i]=1;        num[i]=i+l;//储存区间数[l,r]    }    for(int i=0; prime[i]*prime[i]<=r; i++)        for(LL j=(l/prime[i]+(l%prime[i]?1:0))*prime[i]; j<=r; j+=prime[i])            if(j>=l)            {                LL res=0;                while(num[j-l]%prime[i]==0)                {                    num[j-l]/=prime[i];                    res++;                }                sum[j-l]=sum[j-l]*(res*k+1)%mod;            }    LL ans=0;    for(int i=0; i<=r-l; i++)    {        if(num[i]>1)            sum[i]=sum[i]*(k+1)%mod;        ans=(ans+sum[i])%mod;    }    cout<<ans<<endl;}int main(){    getprime();    int T;    while(cin>>T)    {        for(int i=0; i<T; i++)        {            cin>>l>>r>>k;            solve();        }    }    return 0;}
原创粉丝点击