我们定义 F(x)是满足 x mod(a*b) == 0这样的a,b的组数。现在给你一个n,你需要求出 F(n)
输入格式:多组数据,每组第一行有一个整数n, 0 < n <= 10^11。
输出格式:每组输出一行,满足条件的(a,b)对数
输入样例 输出样例
1 1
2 3
3 3
4 6
思路为求出x的素因子集合,并记录其次数(如当x的素因子集合为p1,p2,....pn,其次数分别为num1,num2,...numn;
符合题目的a,b的表达示如下:
a=1*p1^(n1)*p2^(n2)*.....*pn^(nn)
b=1*^p1^(m1)*p2^(m2)*....*pn^(mn)
其中(n1+m1)<=num1,(n2+m2)<=num2,....(nn+mm)<=numn
n1,n2,...,nn,m1,m2,...,mn可以取0;
则共有(a,b)对数量为
((num1+1)+(num)+...+1)*(num2+1)+(num2)+...+1)*...*((numn+1)+numn+...+1)对
代码如下(C++):
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
__int64 n;
int b[200] = { 0 };
while (cin >> n)
{
if (n == 1)//n=1时,直接输出答案,并终止此次循环
{
cout << 1 << endl;
continue ;
}
int len = 0;
if ((n % 2) == 0)//单独算2,是为了之后累加变量时可以直接加2,提高速度。
{
b[len++] = 1;
n /= 2;
while ((n % 2) == 0)
{
n /= 2;
b[len - 1]++;
}
}
__int64 temp = 3;
while (temp<=n)
{
if (temp >(sqrt(( double )n)))
{
b[len++] = 1;
break ;
}
if ((n%temp) == 0)
{
b[len++] = 1;
n /= temp;
while ((n%temp) == 0)
{
n /= temp;
b[len - 1]++;
}
}
temp += 2;
}
int sum = 1;
for ( int i = 0; i < len; i++)
{
sum *= (b[i] + 2)*(b[i] + 1) / 2;
}
cout << sum << endl;
}
return 0;
}
数因子集合
这题与上一题核心差不多,就是算素因子集合,不同的是,这次只要素因子,而不用计算其数量。
代码如下:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
__int64 a, b;
int c[200] = { 1 };
int d[200] = { 1 };
while (cin >> a)
{
cin >> b;
int len = 0;
if ((a % 2) == 0)
{
c[len++] = 2;
a /= 2;
}
while ((a % 2) == 0)
{
a /= 2;
}
int temp = 3;
while (temp <= a)
{
if ((a%temp) == 0)
{
c[len++] = temp;
a /= temp;
while ((a%temp) == 0)
{
a /= temp;
}
temp += 2;
}
else{
temp+=2;
}
}
int j = 0;
if ((b % 2) == 0)
{
d[j++] = 2;
b /= 2;
}
while ((b % 2) == 0)
{
b /= 2;
}
temp = 3;
while (temp <= b)
{
if ((b%temp) == 0)
{
d[j++] = temp;
b /= temp;
while ((b%temp) == 0)
{
b /= temp;
}
temp += 2;
}
else{
temp += 2;
}
}
if (len != j)
{
cout << "NO" << endl;
continue;
}
int i = 0;
for (; i < len; i++)
{
if (c[i] != d[i])
{
break;
}
}
if (i == len)
{
cout << "YES" << endl;
}
else{
cout << "NO" << endl;
}
}
return 0;
}