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 的因子就更不可能了。于是时间复杂度就降了下来。接着是排除那些 N 除不尽的因子,即 N % i != 0。剩下的就是那些能除得尽的因子,枚举 N / i 和 N / (N/i),较小的那个先保存下来,直到枚举完所有因子,最少的那个就是答案。

wa了3次......

第一次是遗漏:如果 N/i 是 N 的因子,那么 N/(N/i) 也是 N 的因子。

第二次是错误地认为:如果从小到大枚举因子,那么第一次遇到的就是所求的最少正整数 M。举个例子就知道错了。例如 12,答案为3。如果这样想,求出来的结果会是6,因为如果按从小到大的顺序枚举,当遇到 2 的时候,因子分别是 2 和6,而12/6 = 2 是素数。

第三次就是保存结果的时候只保存 N/(N/i)较小的,脑一定是注水了 = =,i也需要的 !!

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 #include <cstdlib>
 5 using namespace std;
 6
 7 bool is_prime(int x)
 8 {
 9     if (x == 1)
10         return false;
11     if (x == 2)
12         return true;
13     for (int i = 2; i * i <= x; i++)
14     {
15         if (x % i == 0)
16             return false;
17     }
18     return true;
19 }
20
21 int main()
22 {
23     int n;
24     while (scanf("%d", &n) != EOF)
25     {
26         int ans = 0;
27         for (int i = 1; i * i <= n; i++)
28         {
29             if (n % i)
30                 continue;
31             if (is_prime(n/i))
32                 ans = (ans != 0 ? min(ans, i) : i);
33             if (is_prime(n/(n/i)))
34                 ans = (ans != 0 ? min(ans, n/i) : n/i);
35         }
36         printf("%d\n", ans);
37     }
38     return 0;
39 }

时间: 2024-07-31 22:06:47

BestCoder18 1001.Alexandra and Prime Numbers(hdu 5108) 解题报告的相关文章

BestCoder7 1001 Little Pony and Permutation(hdu 4985) 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4985 题目意思:有 n 个数,对于第 i 个数给出 σ(i) 的值.求出互不相交的循环的个数,并输出每个循环包含的数字. 还是不太懂吧?反正比赛的时候我也没看懂 >__< ! 据说,这条题目用到了置换群 + 循环 的知识,黑书上 P246 有相应介绍. 先来说明下这幅图的意思,搞明白题意就发现这条题是很好做的. 它表示:σ(1) = 2, σ(2) = 5, σ(3) = 4, σ(4) = 3,

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 2112 HDU Today 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2112 题目意思:又是求最短路的,不过结合埋字符串来考查. 受之前1004 Let the Balloon Rise 学到用map 的解法做之后,有点蠢蠢欲动,当时见到要用字典树做有点吓坏了(之前看过下,非一般难理解,所以暂时放下了).于是,死就死吧,硬住头皮用map做,反反复复修改终于过了. 首先是memory limit exceeded,因为无读清题目意思,直接开10000 * 10000的数组

I - How many prime numbers HDU - 2138

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-bit signed integer,

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

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

BestCoder24 1001.Sum Sum Sum(hdu 5150) 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5150 题目意思:就是直接求素数. 不过 n = 1,也属于答案范围!!只能说,一失足成千古恨啊----- 1 #include <iostream> 2 #include <cstdio> 3 #include <cstdlib> 4 #include <cstring> 5 using namespace std; 6 7 const int maxn = 1

BestCoder13 1001.Beautiful Palindrome Number(hdu 5062) 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5062 题目意思:给出 N,找出 1 - 10^N 中满足 Beautiful Palindrome Numbers (BPN)的数量有多少. 满足 BPN 的条件有两个:(1)回文串   (2)对称的部分从左到右递增排放. (1)版本 1 (比较麻烦,建议看版本2)        46ms 1 #include <iostream> 2 #include <cstdio> 3 #inc