题目:
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
题意:求区间[l,r]中的元素i^k的因子数之和。
思路:由于数据过大,所以暴力是行不通的!!!根据定理(貌似叫约数定理):n=p1^a1×p2^a2×p3^a3*…*pm^am,n的因子数的个数就是(a1+1)(a2+1)(a3+1)…(am+1)。所以可以推理出n^k的因子数为(k*a1+1)(k*a2+1)(k*a3+1)…(k*am+1)。由于r最大值为1e12,所以它的质因子最大不超过1e6。把[1,1e6]的素数利用素数筛法求出。枚举每个素数在区间内的倍数,可以跳着枚举,计算出每个数对应的因子个数。若区间中的数不断模除因子后的最終结果不是1,则说明这个数本身是素数,素数的因子个数就等于枚举的因子个数*(k+1)累乘起来。。。吐槽:不知道为什么一直TLE、RE,然后去看别人的题解没找到错误,替换两行相同的代码后就AC了,真的醉了,玄学啊附上TLE代码一份,望大佬指教有何不同
CODE:
TLE:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
#define mod 998244353
#define maxn 1000001
bool isprime[maxn];
int cnt;
LL prime[maxn],f[maxn],num[maxn],l,r,k;
void Prime(){
cnt=0;
memset(isprime,true,sizeof(isprime));
isprime[1]=false;
for(int i=2;i<maxn;i++){
if(isprime[i]){
prime[cnt++]=i;
for(int j=2;j*i<maxn;j++) isprime[i*j]=false;
}
}
}
int main()
{
Prime();
int t;
scanf("%d",&t);
while(t--){
scanf("%lld%lld%lld",&l,&r,&k);
for(int i=0;i<=r-l;i++) f[i]=i+l,num[i]=1;
for(int i=0;i<cnt;i++){
LL st=(l/prime[i]+(l%prime[i]?1:0))*prime[i];
for(LL j=st;j<=r;j+=prime[i]){
LL len=0;
/*while(f[j-1]%prime[i]==0){
len++;
f[j-1]/=prime[i];
}
num[j-1]=(num[j-1]*((len*k+1)%mod))%mod;*/有毒的代码,换第一句RE,全换AC
}
}
LL sum=0;
for(int i=0;i<=r-l;i++){
if(f[i]!=1) num[i]=(num[i]*(k+1))%mod;
sum=(sum+num[i])%mod;
}
printf("%lld\n",sum);
}
return 0;
}
AC:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
#define mod 998244353
#define maxn 1000001
bool isprime[maxn];
int cnt;
LL prime[maxn],f[maxn],num[maxn],l,r,k;
void Prime(){
cnt=0;
memset(isprime,true,sizeof(isprime));
isprime[1]=false;
for(int i=2;i<maxn;i++){
if(isprime[i]){
prime[cnt++]=i;
for(int j=2;j*i<maxn;j++) isprime[i*j]=false;
}
}
}
int main()
{
Prime();
int t;
scanf("%d",&t);
while(t--){
scanf("%lld%lld%lld",&l,&r,&k);
for(int i=0;i<=r-l;i++) f[i]=i+l,num[i]=1;
for(int i=0;i<cnt;i++){
LL st=(l/prime[i]+(l%prime[i]?1:0))*prime[i];
for(LL j=st;j<=r;j+=prime[i]){
LL len=0;
while(f[j-l]%prime[i]==0) len++,f[j-l]/=prime[i];
num[j-l]=(num[j-l]*((len*k+1)%mod))%mod;
}
}
LL sum=0;
for(int i=0;i<=r-l;i++){
if(f[i]!=1) num[i]=(num[i]*(k+1))%mod;
sum=(sum+num[i])%mod;
}
printf("%lld\n",sum);
}
return 0;
}