Very Simple Counting(巧妙打表)

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值