HDU2521 反素数【因子数量+打表】

来源:互联网 发布:mysql建立索引 编辑:程序博客网 时间:2024/06/05 11:17

反素数

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6686    Accepted Submission(s): 4001

Problem Description
反素数就是满足对于任意i(0<i<x),都有g(i)<g(x),(g(x)是x的因子个数),则x为一个反素数。现在给你一个整数区间[a,b],请你求出该区间的x使g(x)最大。
Input
第一行输入n,接下来n行测试数据
输入包括a,b, 1<=a<=b<=5000,表示闭区间[a,b].
Output
输出为一个整数,为该区间因子最多的数.如果满足条件有多个,则输出其中最小的数.
 Sample Input
32 31 1047 359
 Sample Output
26240
Hint
2的因子为:1 210的因子为:1 2 5 10
 Source
HDU 2008-10 Programming Contest


问题链接:HDU2521 反素数。

题意简述:参见上文。

问题分析

题目为反素数,实际上与素数似乎没有关系,不过是一个定义而已。

关键是计算整数的因子个数,求区间的中的x使得g(x)即x的因子个数为最大。

程序说明:本程序采用ACM题最普通的套路,为避免重复计算就先打表。把功能封装到函数中也是一种值得推荐的做法。


AC的C语言程序如下:

/* HDU2521 反素数 */#include <stdio.h>#define N 5000int apcount[N+1];         /* antiprime count */int getapcount(int n){    int count = 1, i;    for(i=2; i<=n/2; i++)        if(n % i == 0)            count++;    /* 因子个数计数 */    if(n != 1)        count++;        /* 不是1则自身因子需要加上 */    return count;}void setapcount(int n){    int i;    apcount[0] = 0;    for(i=1; i<=n; i++)        apcount[i] = getapcount(i);}int main(void){    setapcount(N);    int n, a, b, max, maxval, i;    scanf("%d", &n);    while(n--) {        scanf("%d%d", &a, &b);        max = maxval = 0;        for(i=a; i<=b; i++)            if(apcount[i] > maxval) {                maxval = apcount[i];                max = i;            }        printf("%d\n", max);    }    return 0;}




原创粉丝点击