FZU1607 Greedy division

FZU1607 Greedy division

Accept: 317    Submit: 1248
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

5
6
8

 Sample Output

1 1
3 3
3 4

 Hint

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

********************************************************************************

题目大意:给你一个数n,求出n的因数的个数(1除外),以及n/(n最小的非1因数)。

解题思路:唉,基本我着手数论的我也开始也数论了,情何以堪!第一题写这道,TLE了那么久,唉,不爽。

一直TLE,所以参考了别人的代码。

原来素数筛选法可以这样写:

int k=sqrt((double)n)+1;
for(int i=2;i<=n;i++)
{
    if(isprm[i])continue;
    minf[i]=i;
    if(i>k)continue;
    for(int j=i*2;j<=n;j+=i)
         if(isprm[j]==0)
            isprm[j]=1;
}

当然,这只是小优化。

然后,原来,分解质因数可以在素数筛选法的时候进行。不懂这个的我,TLE了,明白了这个我的就AC了。

#include <stdio.h>
#include <string.h>
#include <math.h>
#define N 1000005

int isprm[N];
int minf[N];

int main()
{
    memset(isprm,0,sizeof(isprm));
    int ddd=sqrt(1000000.0)+1;
    for(int i=2;i<=1000000;i++)
    {
        if(isprm[i])continue;
        minf[i]=i;
        if(i>ddd)continue;
        for(int j=i*2;j<=1000000;j+=i)
            if(isprm[j]==0)
                isprm[j]=1,minf[j]=i;
    }
    int n,m;
    while(scanf("%d",&m)!=EOF)
    {
        n=m;
        int ans=1;
        while(n>1)
        {
            int temp=1;
            int k=minf[n];
            while(n%k==0)
            {
                n/=k;
                temp++;
            }
            ans*=temp;
        }
        printf("%d %d\n",ans-1,m/minf[m]);
    }
    return 0;
}

  

posted on 2011-10-29 19:29  Fatedayt  阅读(233)  评论(0编辑  收藏  举报

导航