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
4
5
6
2
1
2
题解:让找最小的正整数M使N/M是素数;
水暴力,但是完全暴力会超,就想着折半找,先找这个最小正整数,如果不行就找素数;都找到sqrt(N)结束,这样就省了好多时间;
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
bool is_prim(int x){
if(x == )return false;
for(int i = ; i <= sqrt(x); i++){
if(x % i == )return false;
}
return true;
}
int main(){
int N;
while(~scanf("%d",&N)){
if(N == || N == ){
puts("");continue;
}
int i;
int ans = ;
for(i = ; i <= sqrt(N); i++){
if(N % i == && is_prim(N/i)){
ans = i;
break;
}
}
if(ans){
printf("%d\n",ans);continue;
}
for(int j = sqrt(N); j > ; j--){
if(N % j == && is_prim(j)){
ans = N / j;
break;
}
}
printf("%d\n",ans);
}
return ;
}
Alexandra and Prime Numbers(思维)的更多相关文章
- hdu 5108 Alexandra and Prime Numbers(水题 / 数论)
题意: 给一个正整数N,找最小的M,使得N可以整除M,且N/M是质数. 数据范围: There are multiple test cases (no more than 1,000). Each c ...
- BestCoder19 1001.Alexandra and Prime Numbers(hdu 5108) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5108 题目意思:给出一个数正整数 N,N <= 1e9,现在需要找出一个最少的正整数 M,使得 ...
- hdu 5108 Alexandra and Prime Numbers
数论题,本质是求出n的最大质因子 #include<time.h> #include <cstdio> #include <iostream> #include&l ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- POJ 2739 Sum of Consecutive Prime Numbers(尺取法)
题目链接: 传送门 Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Description S ...
- algorithm@ Sieve of Eratosthenes (素数筛选算法) & Related Problem (Return two prime numbers )
Sieve of Eratosthenes (素数筛选算法) Given a number n, print all primes smaller than or equal to n. It is ...
- HDOJ(HDU) 2138 How many prime numbers(素数-快速筛选没用上、)
Problem Description Give you a lot of positive integers, just to find out how many prime numbers the ...
- Codeforces 385C Bear and Prime Numbers
题目链接:Codeforces 385C Bear and Prime Numbers 这题告诉我仅仅有询问没有更新通常是不用线段树的.或者说还有比线段树更简单的方法. 用一个sum数组记录前n项和, ...
- POJ2739 Sum of Consecutive Prime Numbers(尺取法)
POJ2739 Sum of Consecutive Prime Numbers 题目大意:给出一个整数,如果有一段连续的素数之和等于该数,即满足要求,求出这种连续的素数的个数 水题:艾氏筛法打表+尺 ...
随机推荐
- SSH方式登录github出现Permission denied (publickey)
今天在公司上传了代码,回到家pull,结果竟然出现了“Permission denied (publickey)“这种东西.第一反应是key不对,可是上次明明用key登录过,不可能不对啊,难道是文件被 ...
- Python 异步IO、IO多路复用
事件驱动模型 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...
- UIScreen类
CGRect screenBounds = [ [UIScreen mainScreen]bounds];//返回的是带有状态栏的Rect NSLog(@"%@", NSStrin ...
- Android WebView 软键盘挡住输入框
解决方法一: 在所在的Activity中加入 getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RES ...
- HTML中将背景颜色渐变
通过使用 css3 渐变可以让背景两个或多个指定的颜色之间显示平稳的过渡,由于用到css3所以需要考虑下浏览器兼容问题,例如:从左到右的线性渐变,且带有透明度的样式:#grad {background ...
- 关于找工作(二 Cover Letter)
准备好了简历,下一个文档就是cover letter了.其实对衡量你是否是一个好的候选人来说,cover letter的作用几乎是零(很多情况下主管技术工作的人或者雇人经理根本见不到cover let ...
- asp.net页面按Enter键IE不提交表单
//当按下回车键时,让指定的按钮获取指定的文本框的事件 this.txtFNick.Attributes.Add("onkeydown", " ...
- Comparator和Comparable在排序中的应用
http://blog.csdn.net/iisgirl/article/details/7269833
- [Red5]Red5之Flash流媒体服务器的安装与使用教程完整版(组图)
参看下面链接:http://www.cuplayer.com/player/PlayerCode/Red5/2013/0319/760.html
- html5增强元素--续
progress元素使用例子 <script> ; function progress_demo() { ) { i++; updateProgress(i); setTimeout(pr ...