HDU1164 Eddy's research I【素因子分解+筛选法+欧拉函数】

Eddy's research I

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10795    Accepted Submission(s): 6651


Problem Description
Eddy's interest is very extensive, recently he is interested in prime number. Eddy discover the all number owned can be divided into the multiply of prime number, but he can't write program, so Eddy has to ask intelligent you to help him, he asks you to write a program which can do the number to divided into the multiply of prime number factor .

 

Input
The input will contain a number 1 < x<= 65535 per line representing the number of elements of the set.
 

Output
You have to print a line in the output for each entry with the answer to the previous question.
 

Sample Input
  
  
11 9412
 

Sample Output
  
  
11 2*2*13*181


问题链接HDU1164 Eddy's research I

问题描述参见上文。

问题分析

  这是一个整数因子分解问题。有两种解法,一是按照欧拉函数计算的思想实现,二是采用筛选法。


  方法一:采用计算欧拉函数的基本思想进行分解,即从小因子开始逐步分解。

  这周方法需要注意的是,对于需要分解的整数n,最后被分解到只剩下1时,不需要输出,即不输出1。


  方法二:使用Eratosthenes筛选法计算必要的素数放在数组中备用。然后使用这些素数,从小因子开始逐步分解整数。由于素数事先被计算出来,只计算一次,可以重复使用,运行速度相对比较快。


程序说明(略)


AC的C语言程序如下(方法二):

#include <stdio.h>
#include <math.h>

// Eratosthenes筛选法
void sieveofe(int p[], int n)
{
    int i, j;

    p[0] = 0;
    p[1] = 0;
    p[2] = 1;

    // 初始化
    for(i=3; i<=n; i++) {
        p[i++] = 1;
        p[i] = 0;
    }
    int max = sqrt(n);
    for(i=3; i<=max; i++){
        if(p[i]) {
            for(j=i+i; j < n; j+=i)    //进行筛选
                p[j]=0;
        }
    }

    // 整理数组p,将素数放在前面的单元中
    p[0] = 2;
    j = 1;
    for(i=3; i<=n; i++)
        if(p[i])
            p[j++] = i;
}

// 利用计算得到的素数(数组p中),进行分解(超时)
void divide(int p[], int n)
{
    int count = 0, i = 0;
    while(n > 1) {
        while(n % p[i] == 0) {
            n /= p[i];
            if(++count == 1)
                printf("%d", p[i]);
            else
                printf("*%d", p[i]);
        }
        i++;
    }
    printf("\n");
}

#define MAXN 65535
int p[MAXN+1];

int main(void)
{
    sieveofe(p, MAXN);

    int n;
    while(scanf("%d", &n) != EOF) {
        divide(p, n);
    }

    return 0;
}

 

AC的C语言程序如下(方法一):

#include <stdio.h>
#include <math.h>

// 欧拉函数法
void divide(int n)
{
    int count = 0, i;

    while(n%2 == 0) {
        n /= 2;
        if(++count == 1)
            printf("2");
        else
            printf("*2");
    }

    for(i=3; i*i<=n; i+=2) {
        if(n%i == 0) {
            n /= i;
            if(++count == 1)
                printf("%d", i);
            else
                printf("*%d", i);
            while(n%i == 0) {
                n /= i;
                printf("*%d", i);
            }
        }
    }
    if(n != 1) {
        if(++count == 1)
            printf("%d", n);
        else
            printf("*%d", n);
    }
    printf("\n");
}

int main(void)
{
    int n;
    while(scanf("%d", &n) != EOF)
        divide(n);

    return 0;
}



  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值