HDU 2136 Largest prime factor (最大素因子序号,cin超时呀!!!)

Posted AC_Dreameng

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 2136 Largest prime factor (最大素因子序号,cin超时呀!!!)相关的知识,希望对你有一定的参考价值。

Largest prime factor

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10004    Accepted Submission(s): 3534


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


题意:求最大素因子的序号,素数序号即为在素数表中的位置,例如:第1个为 2 ,第2个为 3 ,第 3 个为 5,
特别规定:第 0 个为 1.
用了素数筛选法还是超时,网上查看别人的代码,几乎一模一样,最后发现,别人用的是scanf(),printf(),而我用的是cin,cout,一改,便A了.

AC代码:

#include <iostream>
#include <cstdio>
using namespace std;
#define maxn 1000000+5
int a[maxn];
int main()
{
    int ans=0;
    for(int i=2;i<maxn;i++)
    {
        if(!a[i])
        {
            ans++;
            for(int j=i;j<maxn;j+=i)
                a[j]=ans;
        }
    }
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        printf("%d\n",a[n]);
    }
    return 0;
}


以上是关于HDU 2136 Largest prime factor (最大素因子序号,cin超时呀!!!)的主要内容,如果未能解决你的问题,请参考以下文章

Python 操作Redis

python爬虫入门----- 阿里巴巴供应商爬虫

Python词典设置默认值小技巧

《python学习手册(第4版)》pdf

Django settings.py 的media路径设置

Python中的赋值,浅拷贝和深拷贝的区别