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-10;

long long prime[100003], p = 0;
bool is_prime[1000003];

void init()
{
    memset(is_prime, true, sizeof(is_prime));
    for(int i=2; i<=1000000; ++ i)
    {
        if(is_prime[i])
        {
            prime[p++] = i;
            for(int j=i; j<=1000000; j+=i)
                is_prime[j] = false;
        }
    }
}

void solve(int cases)
{
    long long n;
    scanf("%lld", &n);
    long long ans = 1;
    for(int i=0; i<p&&prime[i]*prime[i]<=n; ++ i)
    {
        int res = 0;
        while(n % prime[i] == 0)
        {
            n /= prime[i];
            res ++;
        }
        ans *= (res + 1);
    }
    if(n > 1)
        ans *= 2;
    printf("Case %d: %lld\n", cases, ans-1);
}

int main()
{
    int n;
    scanf("%d", &n);
    init();
    for(int i=1; i<=n; ++i)
    {
        solve(i);
    }
    return 0;
}

时间: 2024-11-03 22:42:54

Light OJ 1028 - Trailing Zeroes (I) (数学-因子个数)的相关文章

Light OJ 1028 Trailing Zeroes (I) 求n因子数

今天真机调试的时候莫名其妙遇到了这样的一个问题: This product type must be built using a provisioning profile, however no provisioning profile matching both the identity "iPhone Developer" and the bundle identifier..... 具体如下图所示: 十分蛋疼, 发现不管是从网上下的demo, 还是自己的过程.凡事真机测试的时候都

Light oj 1138 - Trailing Zeroes (III) 【二分查找 &amp;&amp; N!中末尾连续0的个数】

1138 - Trailing Zeroes (III) PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB You task is to find minimal natural number N, so that N! contains exactly Q zeroes on the trail in decimal notation. As you know N! = 1*2*...*N. F

Light oj 1138 - Trailing Zeroes (III) 【二分查找好题】【 给出N!末尾有连续的Q个0,让你求最小的N】

1138 - Trailing Zeroes (III) PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB You task is to find minimal natural number N, so that N! contains exactly Q zeroes on the trail in decimal notation. As you know N! = 1*2*...*N. F

light oj 1138 - Trailing Zeroes (III)(阶乘末尾0)

You task is to find minimal natural number N, so that N! contains exactly Q zeroes on the trail in decimal notation. As you know N! = 1*2*...*N. For example, 5! = 120, 120 contains one zero on the trail. Input Input starts with an integer T (≤ 10000)

LightOJ - 1028 Trailing Zeroes (I)

题意:T(<=1e4)组数据,求对于N(<=1e12),已知N在k进制表示下末位是0,求有多少个可能的k 相当与求解每个N的因数个数减1(代表除去1进制的情况) N=p1a1*p2a2*...*pnan,若N=m*n,那么m可表示为m=x*p1^(k), 0 <= k <= a1, 那么N的约数个数为(a1+1)(a2+1)...(an+1) 对于不大于1e12的数,若它为素数,答案恒为1:若它不为素数,那么它必然可以被不大于1e6的数整除,所以我们只需要预处理出不大于1e6的素数

[LintCode] Trailing Zeroes 末尾零的个数

Write an algorithm which computes the number of trailing zeros in n factorial. Have you met this question in a real interview? Yes Example 11! = 39916800, so the out should be 2 Challenge O(log N) time s

light oj1028 - Trailing Zeroes (I)

1028 - Trailing Zeroes (I) We know what a base of a number is and what the properties are. For example, we use decimal number system, where the base is 10 and we use the symbols - {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}. But in different bases we use differen

lightoj-1028 - Trailing Zeroes (I)(素数法求因子个数)

1028 - Trailing Zeroes (I) PDF (English) Statistics ForumTime Limit: 2 second(s) Memory Limit: 32 MBWe know what a base of a number is and what the properties are. For example, we use decimal number system, where the base is 10 and we use the symbols

[LeetCode]172.Factorial Trailing Zeroes

题目 Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. 分析 朴素解法: 首先求出n!,然后计算末尾0的个数.(重复÷10,直到余数非0) 该解法在输入的数字稍大时就会导致阶乘得数溢出,不足取. O(logn)解法: 考虑n!的质数因子. 后缀0总是由质因子2和质因子5相乘得来的.如果我们可以计数