hdu 1492 the numbers of divisors about humble numbers

The number of divisors(约数) about Humble Numbers

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1314    Accepted Submission(s): 637


Problem Description
A number whose only prime factors are 2,3,5 or 7 is called a humble number. The sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 24, 25, 27, ... shows the first 20 humble numbers. 

Now given a humble number, please write a program to calculate the number of divisors about this humble number.For examle, 4 is a humble,and it have 3 divisors(1,2,4);12 have 6 divisors.

 

Input
The input consists of multiple test cases. Each test case consists of one humble number n,and n is in the range of 64-bits signed integer. Input is terminated by a value of zero for n.
 

Output
For each test case, output its divisor number, one line per case.
 

Sample Input
4 12 0
 

Sample Output
3 6
 

Author
lcy
 

Source
 

Recommend
LL


先求出 humble number 的 2,3,5,7 的因数个数,分别为 twos, threes, fives, sevens,然后从这些因数里取出若干个,这若干个相乘就得到一个 humble number 的因数。

用生成函数:

2: a = (1 + x + x ^ 2 + ... + x ^ twos)

3: b = (1 + x + x ^ 2 + ... + x ^ threes)

5: c = (1 + x + x ^ 2 + ... + x ^ fives)

7: d = (1 + x + x ^ 2 + ... + x ^ sevens)


然后 a * b * c * d

求 ak * x ^ k 的系数 ak,把所有系数加起来!


#include <cstdio>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <algorithm>

const int P_MAX = 100000; // power maximum

int PolyMult (int *a, int pa, int *b, int pb) {
    static int c [P_MAX];
    int pc = pa + pb;
    memset (c, 0, sizeof (*c) * (pc + 1));
    for (int i=0; i<=pa; ++i) {
        for (int k=0; k<=pb; ++k) {
            assert (i + k < P_MAX);
            c [i + k] += a [i] * b [k];
        }
    }
    memcpy (a, c, sizeof (*c) * (pc + 1));
    return pc;
}

int CountFactor (unsigned long long n, int fac) {
    int cnt = 0;
    while (n % fac == 0) {
        n /= fac;
        ++cnt;
    }
    return cnt;
}

int main () {
    static int e [P_MAX];
    std::fill (e, e + P_MAX, 1);

    static int g [P_MAX]; // generator
    unsigned long long n;

#ifdef ONLINE_JUDGE
    while (scanf ("%I64u", &n) == 1 && n) {
#else
    while (scanf ("%llu", &n) == 1 && n) {
#endif
        int twos = CountFactor (n, 2);
        int threes = CountFactor (n, 3);
        int fives = CountFactor (n, 5);
        int sevens = CountFactor (n, 7);

        memset (g, 0, sizeof (*g) * (twos + threes + fives + sevens + 1));
        g [0] = 1;
        int pg = 0; // power of g

        pg = PolyMult (g, pg, e, twos);
        pg = PolyMult (g, pg, e, threes);
        pg = PolyMult (g, pg, e, fives);
        pg = PolyMult (g, pg, e, sevens);

        int sum = 0;
        for (int i=0; i<=pg; ++i) {
            sum += g [i];
        }

        printf ("%d\n", sum);
    }
    return 0;
}


发布了94 篇原创文章 · 获赞 1 · 访问量 7万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章