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. SSH方式登录github出现Permission denied (publickey)

    今天在公司上传了代码,回到家pull,结果竟然出现了“Permission denied (publickey)“这种东西.第一反应是key不对,可是上次明明用key登录过,不可能不对啊,难道是文件被 ...

  2. 后台数据导出为Excel

    数据导出的方法如下: 一.下载office的类库:microsoft.office.interop.excel.zip 根据电脑安装的office版本选择引入相应的类库,office2007选择12. ...

  3. pyqt 图片(label上显示

    # -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' from decimal import * from PyQt4.Q ...

  4. 改动mac环境变量,并配置gradle

    由于项目中要用到gradle命令,可是没有配置环境变量.这里记录一下解决过程. 过程例如以下: 1. 启动终端Terminal 2. 进入当前用户的home文件夹 输入cd ~ 3. 创建.bash_ ...

  5. C#实现防拷贝工具示例

    思路是用加密程序 对硬盘号,cpu号和MAC号取出字符串并加密 产生一个序列号 每次程序启动后重新产生这个序列号并比对,如果一致则验证通过 using System;using System.Coll ...

  6. elasticsearch,http://ip:9200访问不到的解决办法

    现在解压elasticsearch之后,启动,通过http://localhost:9200可以访问的到,但是http://ip:9200访问不到,怎么办呢?带你解决,带你飞 修改elasticsea ...

  7. UI开发学习中遇到的问题汇总

    1.给UIView设置圆角,边框,阴影绘制,需要使用layer 1)设置圆角cornerView.layer.cornerRadius = 20; //设置试图圆角的大小cornerView.laye ...

  8. linux中运行python

    1.在终端中输入: python    **.py 2.最爽的方法: emacs中配置python运行环境.

  9. 自动运行native2ascii 命令的Bat文件的编写

        使用eclipse开发,对于.properties文件的国际化,如果不使用插件对文件进行转码,则需要使用native2ascii命令自行对文件进行转码.     为了更方面的执行此操作,我将该 ...

  10. jQuery常用选择器汇总

    一.基本选择器 <body> <div> <div id="div1"> aaaaaaaaaaa</div> <div cla ...