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.
InputThe 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.
OutputFor each test case, output its divisor number, one line per case.
Sample Input
4
12
0
Sample Output
3
6
题意:这道题就是求一个数的因子个数,只是这个数的范围很大;
解题思路:
1.最开始的思路:这无非就是一个循环解决的事,应为想着循环的范围就在2到根号n,以为这样就行了,而且代码也简单,但是事实告诉我,其实在很大数的n的开根号之后还是很大,所以就超时了;
2.AC解题思路:这里用到了一个公式,是整数的唯一分解,比如,n可以分解为n=(p1^k1) * (p2^k2)… (pn^kn);然后因子个数就为(1+k1)*(1+k2)…(1+kn),这样就解决了超时问题;
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
long long n,i;
while(~scanf("%lld",&n)&&n)
{
long long sum=1;
for(i=2;i<=n;i++)
{
if(n%i==0)
{
long long cnt=0;
while(n%i==0)
{
cnt++;
n=n/i;
}
sum=sum*(1+cnt);
}
}
printf("%lld\n",sum);
}
return 0;
}