求約數個數(模板)

整數的惟一分解定理

對於一個大於1正整數n能夠分解質因數java

 

約數個數

spa

其中a一、a二、a3…ak是p一、p二、p3,…pk的指數。3d

eg:code

給定n個正整數aiai,請你輸出這些數的乘積的約數個數,答案對109+7取模。xml

輸入格式

第一行包含整數n。blog

接下來n行,每行包含一個整數aiai。get

輸出格式

輸出一個整數,表示所給正整數的乘積的約數個數,答案需對109+7109+7取模。it

數據範圍

1n1001≤n≤100,
1ai21091≤ai≤2∗109
class

輸入樣例:

3
2
6
8

輸出樣例:

12

代碼:
import java.util.*;
public class Main{
        static final int N=(int)1e9+7;
        static Map<Integer, Integer> map=new HashMap<Integer, Integer>();
        static int t;
        public static void main(String[] args) {
                Scanner scan=new Scanner(System.in);
                t=scan.nextInt();
                while(t-->0){    
                       //求解每一個質因子的指數
                        int n=scan.nextInt();
                        for(int i=2;i<=n/i;i++){
                                if(n%i==0){
                                    while(n%i==0){
                                        n/=i;
                                        if(map.get(i)==null) map.put(i, 1);
                                            else map.put(i, map.get(i)+1);
                                    }
                                }
                        }
                        if(n>1) {
                            if(map.get(n)==null) map.put(n, 1);
                            else map.put(n, map.get(n)+1);
                        }
                }
                //求約數個數
                long res=1;
                Set<Integer> key=map.keySet();
                Iterator<Integer> it=key.iterator();
                while(it.hasNext()){
                     int s=it.next();
                     res=res*(map.get(s)+1)%N;
                }
                System.out.println(res);
                
        }
}
相關文章
相關標籤/搜索