Description
输入一个整数,求其约数之和。
例如10的约数和为 1+2+5+10=18
Format
Input
一个数字N,N<2^63-1
Output
如题
Samples
Input1
10
Copy
Output1
18
Copy
Input2
1099511627776
Copy
Output2
2199023255551
Copy
Limitation
1s, 1024KiB for each test case.
这题用约数和定理不会超时。不会约数和定理的同学自行去学习一下。
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<vector>
#include<math.h>
#include<iomanip>
#include<set>
#include<queue>
#include<stack>
#include<map>
#include<list>
using namespace std;
long long n, ans, ans1 = 1;
map<int, int>a;
int fun(long long x)//判断是否为素数函数
{
int a = 0;
for (long long i = 2; i <= sqrt(x); i++)
{
if (x % i == 0)
{
a++;
}
if (a >= 1)
{
return 0;
}
}
return 1;
}
int main()
{
cin >> n;
long long i = 2;
if (fun(n) == 1)//如果为素数就直接输出ta和ta本身,以免过大超时
{
cout << n + 1;
return 0;
}
while (n != 1)//用约数和定理直到n为1,期间把每个质数出现次数记录下来
{
if (n % i == 0)
{
a[i]++;
n = n / i;
}
if (n % i != 0)
{
i++;
}
}
map<int, int>::iterator it;
for (it = a.begin(); it != a.end(); it++)//求约数和
{
for (long long i = 0; i <= it->second; i++)
{
ans = ans + pow(it->first,i);
}
ans1 = ans * ans1;
ans = 0;
}
cout << ans1;
}