Give a natural number n (1 <= n <= 500000), please tell the summation of all its proper divisors.
Definition: A proper divisor of a natural number is the divisor that is strictly less than the number.
e.g. number 20 has 5 proper divisors: 1, 2, 4, 5, 10, and the divisor summation is: 1 + 2 + 4 + 5 + 10 = 22.
Input
An integer stating the number of test cases, and that many lines follow each containing one integer between 1 and 500000.
Output
One integer each line: the divisor summation of the integer given respectively.
Sample Input
3
2
10
20
Sample Output
1
8
22
题意:求一个数的因子和
思路:因式分解 n= p1^e1*p2^e2...*pk^ek,p1,..pt都是素数.
则所有约数和(包括本身)为(1+p1+p1^2+..+p1^e1)*(1+p2+p2^2+...+p2^e2)*...*(1+pt+pt^2+... +pt^ek)
ac代码:
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stack>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#define MAXN 510000
#define LL long long
#define ll __int64
#define INF 0xfffffff
#define mem(x) memset(x,0,sizeof(x))
#define PI acos(-1)
#define mod 1000000007
using namespace std;
int v[MAXN];
int prime[MAXN];
void isprime()
{
int i,j;
mem(v);
v[1]=1;
int cnt=0;
for(i=2;i<MAXN;i++)
{
if(v[i]==0)
{
prime[cnt++]=i;
for(j=i*2;j<MAXN;j+=i)
v[j]=1;
}
}
}
int main()
{
isprime();
int t,n,m,i,j,num;
int cas=0;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
int nn=n;
int ans=1;
for(i=0;prime[i]*prime[i]<=n;i++)
{
if(n%prime[i]==0)
{
int k=1;
while(n%prime[i]==0)
{
n/=prime[i];
k*=prime[i];
}
//printf("k=%d\n",k);
ans=ans*(k*prime[i]-1)/(prime[i]-1);//等比数列求和公式
//printf("%d\n",ans);
}
if(n==1)
break;
}
if(n>1)
{
ans*=(n+1);
}
printf("%d\n",ans-nn);//不包括其本身,去掉
}
return 0;
}