Light OJ 1028 - Trailing Zeroes (I) (数学-因子个数)

时间:2023-03-09 09:05:38
Light OJ 1028 - Trailing Zeroes (I) (数学-因子个数)

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1028

题目大意:n除了1有多少个因子(包括他本身)

解题思路:对于n的每个因子, 可以用n的所有素因子排列组合而来, n = (a1x1) * (a2 x2) * (a3x3)...*(anxn), 其中ai为n的素因子,那么n的因子的个数等同于(x1 + 1) * (x2 + 1) * (x3 + 1) ... * (xn + 1)中排列, 因为其中一种排列肯定为所有素因子的幂指数为0, 那么这个因子就是1, 这个最后去掉就好。 最后只求n的每个素因子的幂指数,即可求解

代码如下:

#include<bits/stdc++.h>
using namespace std;
const double eps = 1e-; long long prime[], p = ;
bool is_prime[]; void init()
{
memset(is_prime, true, sizeof(is_prime));
for(int i=; i<=; ++ i)
{
if(is_prime[i])
{
prime[p++] = i;
for(int j=i; j<=; j+=i)
is_prime[j] = false;
}
}
} void solve(int cases)
{
long long n;
scanf("%lld", &n);
long long ans = ;
for(int i=; i<p&&prime[i]*prime[i]<=n; ++ i)
{
int res = ;
while(n % prime[i] == )
{
n /= prime[i];
res ++;
}
ans *= (res + );
}
if(n > )
ans *= ;
printf("Case %d: %lld\n", cases, ans-);
} int main()
{
int n;
scanf("%d", &n);
init();
for(int i=; i<=n; ++i)
{
solve(i);
}
return ;
}