【ZOJ - 3286】Very Simple Counting (打表)

6 篇文章 0 订阅

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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值