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. [Protractor] Use protractor to catch errors in the console

    For any reason, there is an error in your code, maybe something like undefined error. Protractor sti ...

  2. C. Robot(BFS)

    C. Robot Time Limit: 3000ms Case Time Limit: 3000ms Memory Limit: 262144KB 64-bit integer IO format: ...

  3. Qt之操作Excel

    Visual Basic for Applications(VBA)是一种Visual Basic的一种宏语言,主要能用来扩展Windows的应用程式功能,特别是Microsoft Office软件. ...

  4. 伪元素first-letter(首字母变大)

    让首字母变大 <p>Do you like to ride a bicycle?</p> p:first-letter{ font-size: 34px; }

  5. Installing the Eclipse Plugin

    Installing the Eclipse Plugin Android offers a custom plugin for the Eclipse IDE, called Android Dev ...

  6. Intel Core i7的整体操作

    Intel Core i7的整体操作(我们也称呼为Nehalem,他的项目代码名) 主要分成2个部分-指令控制单元Instruction Control Unit(ICU),负责从存储器读出指令序列, ...

  7. php 出现 500 Internal Server Error错误问题解决

    set_time_limit(0); //设置超时时间 chmod 777 filename //设置文件权限 问题根本不在这,读取数据太多,数组是很站内存的. 内存设置大些就ok了 ini_set( ...

  8. ssh整合web.xml过滤器和监听器的配置 .

    延迟加载过滤器 Hibernate 允许对关联对象.属性进行延迟加载,但是必须保证延迟加载的操作限于同一个 Hibernate Session 范围之内进行.如果 Service 层返回一个启用了延迟 ...

  9. Centos下搭建 nginx+uwsgi+python

    python做web应用最麻烦的还是配置服务器了,此话不假,光中间件就有好几种选择,fastcgi.wsgi.uwsgi,难 免让人眼花缭乱. 而听说uwsgi的效率是fastcgi和wsgi的10倍 ...

  10. 梯田(dfs)

    梯田 Time Limit: 2000 ms   Memory Limit: 256 MBTotal Submission: 26   Submission Accepted: 5   Descrip ...