题目链接http://acm.hdu.edu.cn/showproblem.php?pid=6069

题意:求题目中给出的式子。

解法:

2017多校第4场 HDU 6069 Counting Divisors 素筛,暴力,优化_i++



#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 1e6+10;
const int mod = 998244353;
LL l, r, v[maxn], ans[maxn];
int prime[maxn],k,tot;
bool isprime[maxn];
void init(){
tot=0;
memset(isprime,1,sizeof(isprime));
for(int i=2; i<maxn; i++){
if(isprime[i])
for(LL j=(LL)i*i; j<maxn; j+=i)
isprime[j]=0;
}
for(int i=2; i<maxn; i++)
if(isprime[i])
prime[++tot] = i;
}
void work(LL p){
LL st=l/p*p;
if(st!=l)st+=p;
LL en=r/p*p;
for(LL i=st;i<=en; i+=p){
int cnt=0;
while(v[i-l]%p==0){
cnt++;
v[i-l]/=p;
}
ans[i-l]*=k*cnt+1;
ans[i-l]%=mod;
}
}
int main()
{
init();
int T;
scanf("%d",&T);
while(T--){
scanf("%lld%lld%d",&l,&r,&k);
for(LL i=l; i<=r; i++){
v[i-l]=i;
ans[i-l]=1;
}
for(int i=1; i<=tot; i++){
work(prime[i]);
}
LL sum=0;
for(LL i=l; i<=r; i++){
if(v[i-l]>1){
ans[i-l]*=k+1;
}
sum+=ans[i-l];
sum%=mod;
}
printf("%lld\n", sum);
}
return 0;
}