Counting Divisors
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)Total Submission(s): 2358 Accepted Submission(s): 848
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
Source
训练时对这题完全没有想法,连因数是什么都不太清楚了,完全没有头绪。
这两天学了一些和因素质数有关的东西,大概整理如下:
因数:
一般定义在整数上,设A为整数,B为非零整数,若存在整数Q,使得A = QB ,则称B是A的因数,记作B|A。
分解质因数:
每个合数(指非素数)都可以写成几个质数相乘的形式,其中每个质数都是这个合数的因数,叫做这个合数的分解质因数。分解质因数只针对合数。
步骤:从最小的质数除起,一直除到结果为质数为止。
一个合数的因数个数与质因数个数之间的关系:
设 N 分解质因数后可写为p1c1p2c2...pmcm ,数 N 的因数个数为(c1+1)(c2+1)...(cm+1)。其中pi代表不同的质数
由约数定义可知p1^a1的因数有:p1^0, p1^1, p1^2......p1^a1 ,共(a1+1)个;同理p2^a2的因数有(a2+1)个......pk^ak的约数有(ak+1)个。
根据乘法原理:n的因数的个数就是(a1+1)(a2+1)(a3+1)…(ak+1)。
反证法:假如N为合数,因为一个合数可分为多个质数之积,设其中最小质数为a,则N可表示为N=a*b,因为区间[2,(int)√N]内没有质因子,所以a要>√N,则b<√N,又因为除质因子外其他所有因子都是一个或多个质因子的积,则其一定比最小质因子要来得大,即b≥a,相互矛盾,所以N不能是合数。
题意:求
(∑i=lrd(ik))mod998244353
,d(x)就是数x的因数个数
N = p1^c1 * p2^c2 * ... * pm^cm => N^k = p1^(k*c1) * p2^(k*c2) * ... * pm^(k*cm)
d(N) = (c1+1)*(c2+1)*...*(cm+1)
d(N^k) = (k*c1+1)*(k*c2+1)*...*(k*cm+1)
知道这些就可以直接暴力求[l,r]直接每个数的因数个数了,但是效率比较低。
所以要提高效率:
1、将[1,1e6]内所有的质数找出并存起来,减少每次找质数的步骤
2、从 找能把该数整除的质数 变到 找能被该质数整除的数
#include<iostream>
#include<string.h>
#include<stdio.h>
#include<math.h>
#define LL long long
using namespace std;
const LL maxn = 1e6 + 1000,mod = 998244353;
bool Isprime[maxn];
LL cnt,prime[maxn],t,l,r,k,ans[maxn],num[maxn];//ans 数组保存[l,r]区间内每个数的因子个数,num 数组保存[l,r]区间内每个数剩余的数
void Prime()
{
cnt = 0;
memset(Isprime, 1, sizeof(Isprime));
for(int i=2;i<maxn;i++)
{
if(Isprime[i])
{
prime[cnt++] = i;
for(int j=i*2;j<maxn;j+=i)
Isprime[j] = false;
}
}
}
int main()
{
Prime();
scanf("%lld",&t);
while(t--)
{
scanf("%lld%lld%lld",&l,&r,&k);
memset(ans,0,sizeof ans);
for(LL i=l;i<=r;i++)
num[i-l] = i,ans[i-l] = 1;
for(LL i=0;i<cnt;i++)
{
if(prime[i]>sqrt(r))
break;
LL pos = l / prime[i] * prime[i];//pos 为 ≥l 的第一个能被prime[i]数
if(l%prime[i])
pos += prime[i];
for(LL j=pos;j<=r;j+=prime[i])
{
LL c = 0;//c 代表该数可被prime[i]质数整除的次数
while(num[j-l] % prime[i] == 0)
{
c++;
num[j-l] /= prime[i];
}
ans[j-l] *= c * k + 1;
ans[j-l] %= mod;
}
}
LL Ans = 0;
for(LL i=l;i<=r;i++)
{
if(num[i-l]>1) ans[i-l]*=k+1;//说明i为素数
Ans+=ans[i-l];
Ans%=mod;
}
printf("%lld\n",Ans);
}
return 0;
}