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
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.
先求出 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;
}