博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Alexandra and Prime Numbers(思维)
阅读量:6914 次
发布时间:2019-06-27

本文共 1798 字,大约阅读时间需要 5 分钟。

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 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.
N1,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

题解:让找最小的正整数M使N/M是素数;

水暴力,但是完全暴力会超,就想着折半找,先找这个最小正整数,如果不行就找素数;都找到sqrt(N)结束,这样就省了好多时间;

代码:

#include
#include
#include
#include
#include
using namespace std;bool is_prim(int x){ if(x == 1)return false; for(int i = 2; i <= sqrt(x); i++){ if(x % i == 0)return false; } return true;}int main(){ int N; while(~scanf("%d",&N)){ if(N == 0 || N == 1){ puts("0");continue; } int i; int ans = 0; for(i = 1; i <= sqrt(N); i++){ if(N % i == 0 && is_prim(N/i)){ ans = i; break; } } if(ans){ printf("%d\n",ans);continue; } for(int j = sqrt(N); j > 1; j--){ if(N % j == 0 && is_prim(j)){ ans = N / j; break; } } printf("%d\n",ans); } return 0;}

 

转载地址:http://nbicl.baihongyu.com/

你可能感兴趣的文章
Windows Server 2012 NIC功能
查看>>
Goldengate双向复制配置
查看>>
sshd 已死 但是subsys被锁或者Sshd dead but subsys locked
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
在主引导记录(MBR)的救援模式下如何重新安装GRUB引导装载程序
查看>>
我的友情链接
查看>>
git的基本使用和示例
查看>>
用户管理
查看>>
从输入 URL 到页面加载完的过程中都发生了什么事情?
查看>>
揭秘Windows Server2012 核心虚拟化技术Hyper-V
查看>>
java参数传递(值传递还是引用传递)
查看>>
去除文本中重复的行方法
查看>>
On Stack Replacement and JIT
查看>>
CDN业务检测(蓝汛/帝联)
查看>>
Storm数据流模型的分析及讨论
查看>>
VR风暴将至虚拟现实的中国故事该怎么写?
查看>>
java多线程
查看>>
jvisualvm 远程监控Linux下的tomcat
查看>>
Django使用ldap认证登录
查看>>