题目链接:点击打开链接
题意:两个整数a, b。求出a, a - 1, a - 2........b +1这些整数最多能被拆分成多少个数相乘。
思路:数量最多,当然都要拆成素数。因为是要查1e6次区间,所以自然能想到前缀和。要计算每个数最多拆成几个素因子,可以筛出来,然后再计算前缀和。
记得以前写过筛素因子...结果一时没能想起来...
代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn = 5e6+5;
int sum[maxn];
void init()
{
for(int i = 2; i < maxn; i++)
{
if(!sum[i])
for(int j = i; j < maxn; j += i)
sum[j] = sum[j/i]+1;
}
for(int i = 1; i < maxn; i++)
sum[i] += sum[i-1];
}
int main(void)
{
init();
int t, a, b;
cin >> t;
while(t--)
{
scanf("%d%d", &a, &b);
printf("%d\n", sum[a]-sum[b]);
}
return 0;
}