FZUOJ Problem 1607 Greedy division (求因子个数)

Problem 1607 Greedy division

Accept: 478    Submit: 1715
Time Limit: 1000 mSec    Memory Limit : 32768 KB

Problem Description

Oaiei has inherited a large sum of wealth recently; this treasure has n pieces of golden coins. Unfortunately, oaiei can not own this wealth alone, and he must divide this wealth into m parts (m>1). He can only get one of the m parts and the m parts have equal coins. Oaiei is not happy for only getting one part of the wealth. He would like to know there are how many ways he can divide the wealth into m parts and he wants as many golden coins as possible. This is your question, can you help him?

Input

There are multiply tests, and that will be 500000. For each test, the first line is a positive integer N(2 <= N <= 1000000), N indicates the total number of golden coins in the wealth.

Output

For each test, you should output two integer X and Y, X is the number of ways he can divide the wealth into m parts where m is large than one, Y is the number of golden coins that he can get from the wealth.

Sample Input

568

Sample Output

1 13 33 4

Hint

There are huge tests, so you should refine your algorithm.




题意:有n个钱,然后要平分,即每一份的数量都相同,问有多少种分法,每份最多多少钱


思路:转换为求n的因子个数和最小的因子问题,用快速求一个数因子个数的方法

一个数n = p1^a1*p2^a2...pk^ak,那么他的因子个数为(a1+1)*(a2+1)*(a3+1)*...*(ak+1),为了快速,先打一个素数表


总结:写了一发超时,然后发现了这个方法。。记住了


ac代码:

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stack>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#define MAXN 1010000
#define LL long long
#define ll __int64
#define INF 0xfffffff
#define mem(x) memset(x,0,sizeof(x))
#define PI acos(-1)
#define mod 1000000007
using namespace std;
int v[MAXN];
int prime[MAXN];
void isprime()
{
	int i,j;
	mem(v);
	v[1]=1;
	int cnt=0;
	for(i=2;i<MAXN;i++)
	{
		if(v[i]==0)
		{
			prime[cnt++]=i;
			for(j=i*2;j<MAXN;j+=i)
			v[j]=1;
		}
	}
}
int main()
{
	isprime();
	int t,n,m,i,j,num;
	int cas=0;
//	for(i=0;i<=5;i++)
//	printf("%d %d\n",v[prime[i]],prime[i]);
//	printf("\n");
	while(scanf("%d",&n)!=EOF)
	{
		
		int bz=0;
		int ans;
		int nn=n;
		int cnt=1;
		for(i=0;prime[i]*prime[i]<=n;i++)
		{
			if(n%prime[i]==0)
			{
				if(bz==0)
				{
					bz=1;
					ans=prime[i];
			    }
			    num=0;
				while(n%prime[i]==0)
				{
					n/=prime[i];
					num++;
				}
				cnt=cnt*(num+1);
			}
			if(n==1)
			break;
		}
		if(n>1)
		cnt=cnt*2;
		if(v[nn]==0)
		printf("1 1\n");
		else
		printf("%d %d\n",cnt-1,nn/ans);
	}
	return 0;
}


你可能感兴趣的:(FZUOJ Problem 1607 Greedy division (求因子个数))