The number of divisors(約數(shù)) 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
這么簡(jiǎn)單的題目沒(méi)有一次AC真的是天理難容啊!!
看了網(wǎng)上的代碼才知道了原來(lái)是題意理解不清楚,以后要多加注意。
本題屬于數(shù)學(xué)問(wèn)題:
一個(gè)數(shù),如果所有的因數(shù)都是2,3,5,7,那么這個(gè)數(shù)被稱為謙虛數(shù),輸入一個(gè)謙虛數(shù),打印該數(shù)的合法因數(shù)數(shù)量。
需要注意的是,這里輸入的對(duì)象是一個(gè)謙虛數(shù),那么只要將該謙虛數(shù)不斷除以2,3,5,7,每除一次,參數(shù)加一,將所有素質(zhì)數(shù)求積,最后輸出即可。
代碼為:
#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);
}
}
至于涉及到的數(shù)論知識(shí),則是在于最后解的輸出,(所有素質(zhì)數(shù)相乘)。例如:a個(gè)2為因數(shù),那么這a個(gè)2會(huì)分成多種情況:1個(gè)2乘(a-1)個(gè)2,2個(gè)2乘(a-2)個(gè)2,3個(gè)2乘(a-3)個(gè)2……(a/2)個(gè)2乘(a-2)個(gè)2,總結(jié)起來(lái),恰巧有a種情況,故乘積。