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.
思路:
一看数据量就觉得要打表,想法就是求出每个数的因子个数然后,找出比他小的并且和他因子个数的相同的数有多少。一开始打表方式不对,结果T了,看了别人写的改了一下。
ac代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<string>
#include<vector>
#define mod (1000000007)
using namespace std;
typedef long long ll;
const ll maxn=1000100;
int ans[maxn],num[maxn],tmp[maxn];
void init()
{
for(int i=1;i<maxn;i++)
{
for(int j=1;j*i<maxn;j++)
{
tmp[i*j]++;
}
}
for(int i=1;i<maxn;i++)
{
ans[i]=num[tmp[i]];
num[tmp[i]]++;
}
}
int main()
{
init();//少加了这个
int n;
while(~scanf("%d",&n))
{
printf("%d\n",ans[n]);
}
return 0;
}
TLE的代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<string>
#include<vector>
#define mod (1000000007)
using namespace std;
typedef long long ll;
ll ff(ll x)
{
ll ans=1;
for(int i=2;i<=x;i++)
{
int tt=0;
while(x%i==0)
{
tt++;
x/=i;
}
ans*=(tt+1);
}
if(x!=1)
ans*=2;
return ans;
}
ll ans[2000000];
ll num[2000000];
int main()
{
ll maxn=0;
for(int i=1;i<=100000;i++)
{
ll tmp=ff(i);
ans[i]=num[tmp];
num[tmp]++;
// vv[tmp].push_back(i);
// maxn=max(maxn,tmp);
}
// for(int i=1;i<=1000000;i++)
// if(!vv[i].empty())
// {
// sort(vv[i].begin(),vv[i].end());
// vector<ll> ::iterator it;
// int num=0;
// for(it=vv[i].begin();it!=vv[i].end();it++)
// {
// ans[(*it)]=num;
// num++;
// }
// }
int n;
while(~scanf("%d",&n))
{
printf("%lld\n",ans[n]);
}
return 0;
}