Largest prime factor

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 6990    Accepted Submission(s): 2471

Problem Description
Everybody knows any number can be combined by the prime number.

Now, your task is telling me what position of the largest prime factor.

The position of prime 2 is 1, prime 3 is 2, and prime 5 is 3, etc.

Specially, LPF(1) = 0.
 
Input
Each line will contain one integer n(0 < n < 1000000).
 
Output
Output the LPF(n).
 
Sample Input
1
2
3
4
5
 
Sample Output
0
1
2
1
3
 
Author
Wiskey
 
Source
 
Recommend
威士忌   |   We have carefully selected several similar problems for you:  2133 2135 1215 2137 2134 
 

我的思路 先打了素数表 再用二分查找去找最大的素数。。结果果断TLE了

代码在此:

#include<stdio.h>
int YNprime[1000001];
int prime[200000];
int totprime=1;
int getprime(int maxN)
{
int i,j,k;
for(i=2;i<=maxN;i++)
if(YNprime[i]==0)
{
prime[totprime++]=i;
for(j=2;i*j<=maxN;j++)
YNprime[i*j]=1;
}
return 1;
}
int find(int s,int t,int N)
{
int i;
int m=(s+t)/2;
if(s==t) return s;
if(prime[m]>N) return find(s,m,N);
if(prime[m]<N) return find(m+1,t,N);
else return m;
}
int main()
{
freopen("a.in","r",stdin);
freopen("a.out","w",stdout);
int n,i,ans,k;
getprime(1000000);
while(scanf("%d",&n)!=EOF)
{
ans=0;
if(n==1) {printf("0\n");continue;}
k=find(1,totprime-1,n);
for(i=k+10;i>=1;i--)
if(n%prime[i]==0) {ans=i;break;}
printf("%d\n",ans);
}
return 0;
}

结果发现沙茶了

筛选法的时候就能直接找最大的素数

#include <cstdio>
#include <iostream> using namespace std; #define Maxl 1000005
int prime[Maxl];
int rank[Maxl];
int main()
{
int k = 0, i, j;
for (i =2; i < Maxl; i++)
{
if (prime[i] == 0)
{
rank[i] = ++k;
for (j = i; j < Maxl; j += i)
{
prime[j] = i;
}
}
}
prime[1] = 0;
int n;
while(scanf("%d", &n) == 1)
{
if(n == 1)
{
printf("0\n");
continue;
}
int k = prime[n];
printf("%d\n", rank[k]);
}
}

【沙茶了+筛选保存最大质因数】【HDU2136】Largest prime factor的更多相关文章

  1. [暑假集训--数论]hdu2136 Largest prime factor

    Everybody knows any number can be combined by the prime number. Now, your task is telling me what po ...

  2. [HDU2136] Largest prime factor(素数筛)

    传送门 题意 给出若干个数n(n<=1000000),求每个n的最大质因子的排名. 质数的排名:如果素数p是第k小的素数,那么p的排名就是k. 思路 乍一看不知道怎么搞. 其实可以想想我们怎么筛 ...

  3. 数学--数论--HDU2136 Largest prime factor 线性筛法变形

    Problem Description Everybody knows any number can be combined by the prime number. Now, your task i ...

  4. The largest prime factor(最大质因数)

    1. 问题: The prime factors of 13195 are 5, 7, 13 and 29.What is the largest prime factor of the number ...

  5. R语言学习——欧拉计划(3)Largest prime factor 求最大质因数

    The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 60085 ...

  6. HDU 2136 Largest prime factor(查找素数,筛选法)

    题目梗概:求1000000以内任意数的最大质因数是第几个素数,其中 定义 1为第0个,2为第1个,以此类推. #include<string.h> #include<stdio.h& ...

  7. HDOJ(HDU) 2136 Largest prime factor(素数筛选)

    Problem Description Everybody knows any number can be combined by the prime number. Now, your task i ...

  8. hdu-2136 Largest prime factor---巧用素数筛法

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2136 题目大意: 每个素数在素数表中都有一个序号,设1的序号为0,则2的序号为1,3的序号为2,5的 ...

  9. HDU-4690 EBCDIC 映射,模拟,沙茶

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4690 纯沙茶模拟题... //STATUS:C++_AC_93MS_228KB #include &l ...

随机推荐

  1. Android系统匿名共享内存Ashmem(Anonymous Shared Memory)在进程间共享的原理分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6666491 在前面一篇文章Android系统匿 ...

  2. SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的访问的解决方案

    今天写了一个excel表的导入功能,结果在excel表中的内容导入到页面时报错:SQL  Server 阻止了对组件 'Ad Hoc Distributed Queries' 的  STATEMENT ...

  3. xml入门简介--两天学会xml

    前言 在很久以前,笔者曾见到过1000+页的xml书,里面还有n多的概念,XSL,Xquery,让人头痛.无奈最近需要用到,所以在w3c恶补了一下.以下大致整理了一下相关概念,但是对XSL等派生语言没 ...

  4. 关于iOS9之后的loadViewIfNeeded

    iOS9之前 有些时候因为需要手动调用loadview 但是有风险,系统不再调用viewDidLoad 所以手动调用loadview是错误的 iOS9之后出现了loadViewIfNeeded解决了这 ...

  5. sqlcommand循环内使用

    using (SqlConnection conn = new SqlConnection()) { SqlCommand comm= new SqlCommand(); conn.Connectio ...

  6. MVC(Model View Controller)框架

    MVC框架 同义词 MVC一般指MVC框架 MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用一 ...

  7. 初识KMP

    KMP简介 KMP是一种由Knuth(D.E.Knuth).Morris(J.H.Morris)和Pratt(V.R.Pratt)设计的字符串匹配算法.对目标串T[0:n-1]中查找与之匹配的模式串P ...

  8. codeforces 336D Vasily the Bear and Beautiful Strings(组合数学)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Vasily the Bear and Beautiful Strings Vas ...

  9. 当nginx 500 伪静态错误时,记录解决方法rewrite or internal redirection cycle while processing

    错误日志::rewrite or internal redirection cycle while processing "/index.php/index.php/index.php/in ...

  10. ie textarea不支持maxlength textarea限制长度

    //ie textarea不支持maxlength $('#verify_note').bind('input propertychange', function() { if (this.value ...