Counting Divisors
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)Total Submission(s): 3025 Accepted Submission(s): 1125
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 :
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).
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
对于任意一个数n,我们都可以表示成n=p1c1∗p2c2∗....∗pkck这种形式,对于每一个p
c
都有(c+1)个因子,所以n的因子个数(c1+1)*(c2+1)*...*(ck+1)个,因此我们只需要遍历[l,r]之间的所有数,对于每一个数求出他的p和c
c
都有(c+1)个因子,所以n的因子个数(c1+1)*(c2+1)*...*(ck+1)个,因此我们只需要遍历[l,r]之间的所有数,对于每一个数求出他的p和c
又因为r<=1e12,所以r√
最大为1e6,对于大于r√ 的值,直接乘k+1即可,因为一定为素数,所以他的因子个数是k+1个
最大为1e6,对于大于r√ 的值,直接乘k+1即可,因为一定为素数,所以他的因子个数是k+1个
#include<bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long LL;
const int N=1e6+10;
const LL MOD=998244353;
bool vis[N];
int prime[N/10],t,tot=0;
LL le,ri,k,num[N],sum[N];//sum记录约数的个数
void isprime()//筛N以内素数
{
for(int i=2;i<N;i++)
{
if(!vis[i])prime[tot++]=i;
for(int j=0;j<tot&&i*prime[j]<N;j++)
{
vis[i*prime[j]]=true;
if(i%prime[j]==0)break;
}
}
}
LL solve()
{
for(LL i=0;i<tot&&prime[i]*prime[i]<=ri;i++)
{
LL tmp=le;
if(tmp%prime[i])//将tmp变成>=le的可以整除prime[i]的最小的数
tmp=(tmp/prime[i]+1)*prime[i];
for(LL j=tmp;j<=ri;j+=prime[i])
{
LL cnt=0;
while(num[j-le]%prime[i]==0)
{
num[j-le]/=prime[i];
cnt++;
}
sum[j-le]=(sum[j-le]*(cnt*k+1)%MOD)%MOD;
}
}
LL ans=0;
for(LL i=0;i<=ri-le;i++)
{
if(num[i]>1)//因子>sqrt(ri)的,本身一定是素数
ans=(ans+sum[i]*(k+1)%MOD)%MOD;
else
ans=(ans+sum[i])%MOD;
}
return ans;
}
int main()
{
isprime();
scanf("%d",&t);
while(t--)
{
scanf("%lld%lld%lld",&le,&ri,&k);
for(LL i=0;i<=ri-le;i++)
sum[i]=1,num[i]=le+i;
printf("%lld\n",solve());
}
}