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. N≤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

题解:让找最小的正整数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(思维)的更多相关文章

  1. hdu 5108 Alexandra and Prime Numbers(水题 / 数论)

    题意: 给一个正整数N,找最小的M,使得N可以整除M,且N/M是质数. 数据范围: There are multiple test cases (no more than 1,000). Each c ...

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5108 题目意思:给出一个数正整数 N,N <= 1e9,现在需要找出一个最少的正整数 M,使得 ...

  3. hdu 5108 Alexandra and Prime Numbers

    数论题,本质是求出n的最大质因子 #include<time.h> #include <cstdio> #include <iostream> #include&l ...

  4. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  5. POJ 2739 Sum of Consecutive Prime Numbers(尺取法)

    题目链接: 传送门 Sum of Consecutive Prime Numbers Time Limit: 1000MS     Memory Limit: 65536K Description S ...

  6. 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 ...

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

  8. Codeforces 385C Bear and Prime Numbers

    题目链接:Codeforces 385C Bear and Prime Numbers 这题告诉我仅仅有询问没有更新通常是不用线段树的.或者说还有比线段树更简单的方法. 用一个sum数组记录前n项和, ...

  9. POJ2739 Sum of Consecutive Prime Numbers(尺取法)

    POJ2739 Sum of Consecutive Prime Numbers 题目大意:给出一个整数,如果有一段连续的素数之和等于该数,即满足要求,求出这种连续的素数的个数 水题:艾氏筛法打表+尺 ...

随机推荐

  1. vim 的配色方案

    浅色: http://www.vimninjas.com/2012/09/14/10-light-colors/ 深色: http://www.vimninjas.com/2012/08/26/10- ...

  2. Android UI布局之FrameLayout

    一个FrameLayout对象就好比一块屏幕上提前预定好的空白区域.然后能够填充一些元素到里边.例如说一张图片等.须要注意的是,全部的元素都被放置在FrameLayout区域最左边上的区域.并且无法为 ...

  3. [转]Laravel 4之路由

    Laravel 4之路由 http://dingjiannan.com/2013/laravel-routing/ Laravel 4路由是一种支持RESTful的路由体系, 基于symfony2的R ...

  4. 利用Python进行数据分析——数据规整化:清理、转换、合并、重塑(七)(1)

    数据分析和建模方面的大量编程工作都是用在数据准备上的:载入.清理.转换以及重塑.有时候,存放在文件或数据库中的数据并不能满足你的数据处理应用的要求.很多人都选择使用通用编程语言(如Python.Per ...

  5. [转]IDENT_CURRENT、SCOPE_IDENTITY、@@IDENTITY 差異對照表

    本文转自:http://www.dotblogs.com.tw/hunterpo/archive/2009/09/04/10421.aspx IDENT_CURRENT.SCOPE_IDENTITY ...

  6. 修改tomcat访问路径

    <Context path="/pc" docBase="/data/www/8084/kabao-pc-consume/" reloadable=&qu ...

  7. HTML 表格入门

    每个表格都是由 table 标签开始. 每个表格行由 tr 标签开始. 每个表格数据由 td 标签开始. 这样是一行三列: <table border="1"> < ...

  8. SICP阅读笔记(一)

    2015-08-25   008   Foreword    QUOTE: I think that it's extraordinarily important that we in compute ...

  9. setTimeout的时间设为0的问题

    javascript是单线程执行的,当某一段代码正在执行的时候,所有的后续任务都必须等待,形成一个队列, 一旦当前任务执行完毕,再从队列中取出下一个任务.这常被称为”阻塞式执行“. 如果代码中设定一个 ...

  10. 【mysql】1206 SQLSTATE: HY000 (ER_LOCK_TABLE_FULL) 问题

    最近在做一个项目,其中一需求是:部分数据库中的数据需要定时删除掉(满足一定条件,比如7天前的数据都不保留) 最初的执行方法: 使用Quartz定时执行数据库操作,进行数据删除,数据库操作使用delet ...