HDOJ(HDU) 2521 反素數(因子個數~)
阿新 • • 發佈:2019-01-05
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
就是找出那個區間中,因子最多的那個數,如果最多因子數相等,那麼輸出那個最小的數~
打表~把每個1-5000的因子的個數打表打出來!
再找那個區間中,因子數最多的就行!
import java.util.Scanner;
public class Main{
static int db[] = new int[5001];
public static void main(String[] args) {
dabiao();
Scanner sc= new Scanner(System.in);
int t =sc.nextInt();
while (t-->0){
int a =sc.nextInt();
int b =sc.nextInt();
int max=db[a];
int k=a;
for(int i=a;i<=b;i++){
if(db[i]>max){
k=i;
max=db[i];
}
}
System.out .println(k);
}
}
private static void dabiao() {
db[1]=1;
db[2]=2;
for(int i=3;i<db.length;i++){
db[i]=emirp(i);
}
}
private static int emirp(int m) {
int sum=0;
for(int i=1;i<=m;i++){
if(m%i==0){
sum++;
}
}
return sum;
}
}