HDU——2521反素數

反素數

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


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
3 2 3 1 10 47 359
 

Sample Output
2 6 240
Hint
2的因子爲:1 2 10的因子爲:1 2 5 10
 

Source
HDU 2008-10 Programming Contest
水題,貼代碼:
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;

int List[5005];

void antip(int x)//計算因子個數
{
    int temp,i,ans = 0;
    temp = (int)sqrt(x);
    for(i = 1;i <= temp;i++)
    {
        if(x % i == 0)
            ans += 2;
    }
    if(temp * temp == x) ans--;
    List[x] = ans;
}

int cal(int a,int b)//求因子個數最大的那個數
{
    int i,ans = 0;;
    for(i = a;i <= b;i++)
    {
        if(List[i] == 0)
        {
            antip(i);
        }
        ans = (List[i] > List[ans]) ? i : ans;
    }
    return ans;
}
int main()
{
    int n;
    int a,b;
    int ans;
    scanf("%d",&n);
    while(n--)
    {
        scanf("%d%d",&a,&b);
        ans = cal(a,b);
        printf("%d\n",ans);
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章