hdu-5108-Alexandra and Prime Numbers(求最大质因数) (BestCoder Round #19)

Alexandra and Prime Numbers

                                       Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Problem Description

Alexandra has a little brother. He is new to programming. One day he is solving the following problem: Given an positive integer N, judge whether N is prime.
The problem above is quite easy, so Alexandra gave him a new task: Given a positive integer N, find the minimal positive integer M, such that N/M is prime. If such M doesn‘t exist, output 0.
Help him!

Input

There are multiple test cases (no more than 1,000). Each case contains only one positive integer N.
$N \leq 1,000,000,000$.
Number of cases with $N > 1,000,000$ is no more than 100.

Output

For each case, output the requested M, or output 0 if no solution exists.

Sample Input

3
4
5
6

Sample Output

1

2

1

2

质因子数论里是指能整除给定正整数质数

质因数分解 将一个正整数表示成质因数乘积的过程和得到的表示结果叫做质因数分解

这个题就可以把n分解,分解成n的质因数的乘积。取最大的一个即可。

这里需要注意的是代码中i的范围,i<sqrt(n)就可以。因为一个数分解出来的质因数项肯定不会有超过两个大于√n的质因数存在=大于√n的质因数至多有1个。

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<math.h>
using namespace std;
int main()
{
     int n, i;
     while(~scanf("%d", &n))
     {
         if(n==1)//1没有质因子,特殊处理。
         {
             printf("0\n");
             continue;
         }
         int maxx=1, t = n;
         for(i=2; i<=(int)sqrt(n*1.0); i++)//需要加‘=’,比如n=4;
         {
             while(t%i==0)//因为质因数中有重复,比如336->24 x 3 x 7;
             {
                 t/=i;
                 maxx = i;
             }
         }
        maxx = max(maxx, t);//当t(!=1)本身为素数时,最大质因子是本身。
        printf("%d\n", n/maxx);
    }
    return 0;
}

最后加上素因数表:http://zh.wikipedia.org/wiki/%E7%B4%A0%E5%9B%A0%E5%AD%90%E8%A1%A8

时间: 2024-10-11 18:55:32

hdu-5108-Alexandra and Prime Numbers(求最大质因数) (BestCoder Round #19)的相关文章

hdu 5108 Alexandra and Prime Numbers

http://acm.hdu.edu.cn/showproblem.php?pid=5108 找出最大质因子就可以. 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #define ll long long 5 #define N 100000 6 using namespace std; 7 8 ll n; 9 10 11 int main() 12 { 13 while(scanf(

hdu 5108 Alexandra and Prime Numbers (水题,暴力)

Problem Description Alexandra has a little brother. He is new to programming. One day he is solving the following problem: Given an positive integer N, judge whether N is prime. The problem above is quite easy, so Alexandra gave him a new task: Given

HDu 2138 How many prime numbers 高效Miller素数测试

题目就是给出一组数,让我们测试其中有多少个是素数. 求素数有测试sqrt(n)个数的方法,有筛子方法,不过对于本题这样的题目来说就都不是高效的. 本题使用Miller Rabin素数测试法,效率奇高,对于不是极其大的整数测试都几乎是常数时间.令人神往的算法啊. 网上有个程序,好像是什么吉林的模板程序,不过我一直没看懂他是什么思路写的,是个AC的程序,不过却是错误的,呵呵,因为程序一直把9当做素数. 于是上网查找了其中原理,自己写了个程序,效率和他的差不多一样,通过时间基本无差别,不过我的思路是按

Alexandra and Prime Numbers(思维)

Alexandra and Prime Numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1658    Accepted Submission(s): 565 Problem Description Alexandra has a little brother. He is new to programming. One

HDU-5108-Alexandra and Prime Numbers (BestCoder Round #19)

Alexandra and Prime Numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 319    Accepted Submission(s): 120 Problem Description Alexandra has a little brother. He is new to programming. One

HDOJ(HDU) 2138 How many prime numbers(素数-快速筛选没用上、)

Problem Description Give you a lot of positive integers, just to find out how many prime numbers there are. Input There are a lot of cases. In each case, there is an integer N representing the number of integers to find. Each integer won't exceed 32-

HDU 2138 How many prime numbers(Miller_Rabin法判断素数 【*模板】 用到了快速幂算法 )

How many prime numbers Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 12955    Accepted Submission(s): 4490 Problem Description Give you a lot of positive integers, just to find out how many pr

BestCoder18 1001.Alexandra and Prime Numbers(hdu 5108) 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5108 题目意思:给出一个数正整数 N,N <= 1e9,现在需要找出一个最少的正整数 M,使得 N/M 是素数.如果找不到就输出0. 一开始有想过将所有 <= 1e9 的素数求出来的,不过绝对超时就放弃了:然后就开始从题目中挖掘简便的处理方法.受到求素数的方法启发,枚举的因子 i 如果在 i * i <= N 之内都没有找到符合条件的素数,那么那些 > N 的因子就更不可能了.于是时间

B - Alexandra and Prime Numbers

Alexandra has a little brother. He is new to programming. One day he is solving the following problem: Given an positive integer N, judge whether N is prime. The problem above is quite easy, so Alexandra gave him a new task: Given a positive integer