题目描述
y总的模板
- Java 版
import java.util.*;
import java.io.*;
public class Main{
public static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
public static void main(String[] args) throws IOException {
int n = Integer.parseInt(br.readLine());
String[] strs = br.readLine().split(" ");
for(int i = 0;i < n ;++i){
bw.write(Long.toString(numOfDivisors(Integer.parseInt(strs[i]))));
bw.write('\n');
}
bw.flush();
}
public static long numOfDivisors(int x){
long ans = 1;
Map<Integer,Integer> hash = new HashMap<>();
Integer tmp;
for(int i = 2;i <= x/i; ++i)
{
while(x % i == 0)
{
x /= i;
tmp = hash.get(i);
hash.put(i,tmp == null ? 1 : tmp + 1);
}
}
if(x > 1){
tmp = hash.get(x);
hash.put(x,tmp == null ? 1 : tmp + 1);
}
for (Map.Entry<Integer, Integer> entry : hash.entrySet()) {
Integer val = entry.getValue();
ans = ans * (val + 1);
}
return ans;
}
}
- C++
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int mod = 1e9 + 7;
// The number of divisors
LL numOfDivisors(int x){
LL ans = 1;
unordered_map<int,int> hash;
for(int i = 2;i <= x/i; ++i)
{
while(x % i == 0)
{
x /= i;
hash[i] ++;
}
}
if(x > 1) hash[x] ++;
for(auto i : hash) ans = ans*(i.second + 1) % mod;
return ans;
}
int main()
{
int n,x;
scanf("%d",&n);
while(n--)
{
scanf("%d", &x);
printf("%d\n",numOfDivisors(x));
}
return 0;
}