题解 | #约数的个数#
约数的个数
http://www.nowcoder.com/practice/04c8a5ea209d41798d23b59f053fa4d6
11ms,好帅~
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
//复习:约数的个数:所有质因子的指数+1之后相乘(原理见视频 记忆犹新 算上零次方因子)
void Func_67(void){
int n;
while(scanf("%d",&n) != EOF){
long a[n];
long amax = 0;
for(int i=0; i<n; i++){
scanf("%ld",&a[i]);
if(a[i] > amax){
amax = a[i];
}
}
int l = (long)sqrt(amax);
bool Isprime[l];
vector<long> prime;
fill(Isprime, Isprime+l, true);
for(int i=2; i<l; i++){
if(Isprime[i] == false){
continue;
}else{
for(int j=i*i; j<l; j+=i){
Isprime[j] = false;
}
prime.push_back(i);
}
}
for(int i=0; i<n; i++){
int ans = 1;
for(int j=0; j<prime.size(); j++){
int exp = 0;
while(a[i]%prime[j]==0){
exp++;
a[i] /= prime[j];
}
ans *= exp+1;
}
if(a[i]>1){
ans *= 2;
}
printf("%d\n",ans);
}
}
}
int main(){
Func_67();
return 0;
}