Very Simple Counting
Let f(n) be the number of factors of integer n.
Your task is to count the number of i(1 <= i < n) that makes f(i) = f(n).
Input
One n per line (1 < n <= 1000000).
There are 10000 lines at most.
Output
For each n, output counting result in one line.
Sample Input
4
5
Sample Output
0
2
Hint
f(1) = 1, f(2) = f(3) = f(5) = 2, f(4) = 3.
题意:
就是让你求位于n之前和n因子个数相同的数字的数目。
分析:
因为n是1e6的数据,可以打表做
首先枚举每个数的因子,一开始这个枚举我没有明白,因为思想总在筛素数那里,实际上就是一个一个因子进行枚举从而得到一个数,那么这个数的因子肯定会加1,只不过只要相乘大于maxn就可以停了
之后动态更新记录因子数相同的有多少个的数字,那么每次用到的时候都是小于所给n的和n因子数相同的个数,之后再更新
直接看代码吧,具体解释见注释
code:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 1e6+10;
int f[maxn] = {0};//f[i]表示i的因子个数
int s[maxn] = {0};//s[i]表示1-i之间和i因子数相同的数有多少
int c[maxn] = {0};//c[i]表示因子个数为i个的数有多少个
void init(){
//求出每个数的因子数
for(int i = 1; i < maxn; i++){
for(int j = 1; i * j < maxn; j++){
f[i*j]++;//注意这里是枚举每个数的因子数,和分解质因子筛素数写法无关,一开始想了好久没明白过来。。
}
}
for(int i = 1; i < maxn; i++){
s[i] = c[f[i]];//数字i的因子数为f[i],c[f[i]]就表示目前已有的和i的因子数相同的个数,恰好就是所求
c[f[i]]++;//不断更新c数组,思想很巧妙
}
}
int main(){
init();
int n;
while(~scanf("%d",&n)){
printf("%d\n",s[n]);
}
return 0;
}