【CUGBACM15级BC第19场 A】hdu 5108 Alexandra and Prime Numbers
来源:互联网 发布:win10 10核优化 编辑:程序博客网 时间:2024/05/22 17:20
Alexandra and Prime Numbers
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2291 Accepted Submission(s): 755
Total Submission(s): 2291 Accepted Submission(s): 755
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!
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≤1,000,000,000 .
Number of cases withN>1,000,000 is no more than 100.
Number of cases with
Output
For each case, output the requested M, or output 0 if no solution exists.
Sample Input
3456
Sample Output
1212
题意:
给一个n(<=10Y),然后让找到一个最小的m使得n/m是一个素数.
思路:
先用sqrt(n)的时间把所有的因子都求出来,然后在排序,枚举,就行了
给一个n(<=10Y),然后让找到一个最小的m使得n/m是一个素数.
思路:
先用sqrt(n)的时间把所有的因子都求出来,然后在排序,枚举,就行了
#include <iostream>#include <set>#include <map>#include <stack>#include <cmath>#include <queue>#include <cstdio>#include <bitset>#include <string>#include <vector>#include <iomanip>#include <cstring>#include <algorithm>#include <functional>#include <ctime>#define PI acos(-1)#define eps 1e-8#define inf 0x3f3f3f3f#define debug(x) cout<<"---"<<x<<"---"<<endltypedef long long ll;using namespace std;const int NP = 32000;int ispri[NP] = {}, prime[NP], pcnt = 0;void getprime(){ ispri[0] = ispri[1] = 1; for (long long i = 2; i < NP; i++) if (ispri[i] == 0) { prime[++pcnt] = i; for (long long j = i * i; j < NP; j += i) { ispri[j] = 1; } }}int a[1000000] = {}, icnt = 0;void pdec(int n){ for (int i = 1; prime[i]*prime[i] <= n; i++) if (n % prime[i] == 0) { a[++icnt] = prime[i]; n /= prime[i]; i--; } if (n != 1) { a[++icnt] = n; }}int YZ[100000] , yzs;void DB(int now){ yzs = 0; int maxx = (int)sqrt(now); for (int i = 1 ; i <= maxx ; i ++) { if (now % i == 0) { YZ[++yzs] = i; YZ[++yzs] = now / i; } } if (maxx * maxx == now) { yzs --; }}int dd(int x){ if (x <= 1) { return 0; } int i, m = floor(sqrt(x) + 0.5); for (i = 2; i <= m; i++) if (x % i == 0) { return 0; } return 1;}int main(){ int n; while (cin >> n) { if (n <= 1) { printf("0\n"); continue; } int flag = 0; DB(n); sort(YZ + 1 , YZ + yzs + 1); for (int i = 1; i <= yzs; i++) { int xx = n / YZ[i]; if (dd(xx)) { cout << YZ[i] << endl; flag = 1; break; } } if (flag == 0) { cout << "0" << endl; } } return 0;}
我们还是用另外一种方法去处理这道题,那就是质因数分解。
质因数分解:就是把一个合数分解成几个素数相乘的形式。例如:
48 = 2*2*2*3
58 = 2*3*3*3
由此我们可以将N一直除以一个比它自己本身小的质数,按照质因数分解的方法,不停的进行分解,我们要找到一个分解出的最大的素数P,因为只有这样,我们才能使得M最小。
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#include <stack>#include <queue>#include <string>#include <vector>#include <set>#include <map>#include <bitset>#include <cassert>using namespace std;typedef long long LL;const int N = 50;int n;void work(){ int i , m , x = 0; m = n; for (i = 2 ; i * i <= n ; ++ i) { if (n % i == 0) { while (n % i == 0) { n /= i; } x = max(x , i); } } if (n > 1) { x = max(x , n); } printf("%d\n" , x ? m / x : 0);}int main(){ while (~scanf("%d", &n)) { work(); } return 0;}
阅读全文
0 0
- 【CUGBACM15级BC第19场 A】hdu 5108 Alexandra and Prime Numbers
- 【CUGBACM15级BC第15场 A】hdu 5083 Love
- 【CUGBACM15级BC第17场 A】hdu 5100 Chessboard
- 【CUGBACM15级BC第23场 A】hdu 5146 Sequence
- 【CUGBACM15级BC第8场 A】hdu 4989 Summary
- 【CUGBACM15级BC第31场 A】hdu 5178 pairs
- 【CUGBACM15级BC第1场 A】hdu 4857 逃生
- 【CUGBACM15级BC第11场 A】hdu 5054 Alice and Bob
- 【CUGBACM15级BC第22场 A】hdu 5142 NPY and FFT
- 【CUGBACM15级BC第25场 A】hdu 5154 Harry and Magical Computer
- 【CUGBACM15级BC第27场 A】hdu 5162 Jump and Jump...
- 【CUGBACM15级BC第7场 A】hdu 4985 Little Pony and Permutation
- HDU-#5108 Alexandra and Prime Numbers
- HDU 5108 Alexandra and Prime Numbers
- hdu-5108(Alexandra and Prime Numbers)
- HDU-5108-Alexandra and Prime Numbers (BestCoder Round #19)
- 【CUGBACM15级BC第14场 A】Harry And Physical Teacher
- 【CUGBACM15级BC第11场 B】hdu 5055 Bob and math problem
- [编程题] 疯狂队列
- Docker教程(二) Docker环境安装
- 杂七杂八话LTE(四十五):初识VoLTE
- 通过SQL语句(alter table)来增加、删除、修改字段
- R语言2——时间序列分析
- 【CUGBACM15级BC第19场 A】hdu 5108 Alexandra and Prime Numbers
- MPP_Develop_Reference
- java事务全解析(五)--Template模式
- 字体菜单栏图标过小时,屏幕密度的修改方法
- Ionic2学习笔记(3):Pipe
- oracle database 12c Release 2-Managing Undo 翻译(第一章节)
- lintcode--
- 对ASP.NET网站高性能和多并发的设计的讨论
- apache1