C - Counting Divisors
In mathematics, the function d(n)denotes the number of divisors of positive integer n.
For example, d(12)=6because 1,2,3,4,6,12are 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 33 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
如果一个1e12次方量级的数字a,在被[1, 1e6]内的素数分解了之后,剩下的数字还不是1,那么剩下来的那个数字肯定是一个大于1e6的质数。
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1e6+10;
const ll mod=998244353;
bool vis[maxn];
ll prime[maxn];
int tot=0;
ll l,r,k;
struct node
{
ll x,d;
}a[maxn];
void init() //O(n)素数筛
{
vis[1]=1;
for(int i=2;i<maxn;i++)
{
if(!vis[i]) prime[++tot]=i;
for(int j=1;j<=tot&&i*prime[j]<maxn;j++)
{
vis[i*prime[j]]=1;
if(i%prime[j]==0) break;
}
}
}
void work(ll p)
{
ll idx=(l+p-1)/p*p; //找到l-r中第一个p的倍数
for(ll i=idx;i<=r;i+=p) //idx-r,每p个增加,计算这个数的素因子p的个数,更新d
{
ll t=i-l+1,cnt=0;
while(a[t].x%p==0)
{
a[t].x/=p;
cnt++;
}
a[t].d*=(cnt*k%mod+1);
a[t].d%=mod;
}
}
int main()
{
init();
int T;
scanf("%d",&T);
while(T--)
{
scanf("%lld%lld%lld",&l,&r,&k);
ll c=0;
for(ll i=l;i<=r;i++)
{
a[++c].x=i,a[c].d=1;
}
for(int i=1;i<=tot;i++)
{
if(prime[i]*prime[i]>r) break; //剪枝,我们只计算比sqrt(r)小的素因子
work(prime[i]);
}
ll ans=0;
for(int i=1;i<=c;i++)
{
if(a[i].x!=1) a[i].d=a[i].d*(k+1)%mod; //只会有1个或0个比sqrt(x)大的素因子
ans=(ans+a[i].d)%mod;
}
printf("%lld\n",ans);
}
return 0;
}