• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
dwtfukgv
博客园    首页    新随笔    联系   管理    订阅  订阅
HDU 2136 Largest prime factor (素数打表。。。)

题意:给你一个数,让你求它的最大因子在素数表的位置。

析:看起来挺简单的题,可是我却WA了一晚上,后来终于明白了,这个第一层循环不是到平方根,

这个题和判断素数不一样,只要明白了这一点,就很简单了。

代码如下:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <cstring>
#include <map>
#include <cctype>
#include <cmath>

using namespace std;
typedef long long LL;
const int maxn = 1000000 + 5;
int a[maxn];

void init(){
    memset(a, 0, sizeof(a));
    int cnt = 1;
    for(int i = 2; i < maxn; ++i)  if(!a[i]){//这个地方不是sqrt(maxn+0.5)
        a[i] = cnt++;
        for(int j = i + i; j < maxn; j += i)
            a[j] = a[i];//这个是不断更新的。
    }
}

int main(){
    init();
    int n;
    while(~scanf("%d", &n))
        printf("%d\n", a[n]);
    return 0;
}

 

posted on 2016-06-03 20:16  dwtfukgv  阅读(161)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3