The number of divisors(約數) about Humble Numbers
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
這么簡單的題目沒有一次AC真的是天理難容啊!!
看了網上的代碼才知道了原來是題意理解不清楚,以后要多加注意。
本題屬于數學問題:
一個數,如果所有的因數都是2,3,5,7,那么這個數被稱為謙虛數,輸入一個謙虛數,打印該數的合法因數數量。
需要注意的是,這里輸入的對象是一個謙虛數,那么只要將該謙虛數不斷除以2,3,5,7,每除一次,參數加一,將所有素質數求積,最后輸出即可。
代碼為:
#include <stdio.h>
void main()
{
int a,b,c,d;
__int64 n,i,m;
while(scanf("%I64d",&n)!=EOF&&n!=0)
{
a=b=c=d=1;
while(n!=1)
{
while(n%2==0)
{
n=n/2;
a++;
}
while(n%3==0)
{
n=n/3;
b++;
}
while (n%5==0)
{
n=n/5;
c++;
}
while (n%7==0)
{
n=n/7;
d++;
}
}
printf("%d\n",a*b*c*d);
}
}
至于涉及到的數論知識,則是在于最后解的輸出,(所有素質數相乘)。例如:a個2為因數,那么這a個2會分成多種情況:1個2乘(a-1)個2,2個2乘(a-2)個2,3個2乘(a-3)個2……(a/2)個2乘(a-2)個2,總結起來,恰巧有a種情況,故乘積。