天天看点

hdu 1215 求约数和 唯一分解定理的基本运用

http://acm.hdu.edu.cn/showproblem.php?pid=1215

题意:求解小于n的所有因子和

利用数论的唯一分解定理。

若n = p1^e1 * p2^e2 * ……*pn^en(任何一个数都可以分解成素数乘积)

则n的因子个数为  (1+e1)(1+e2)……(1+en)

n的各个因子的和为(1+p1+p1^2+……+p1^e1)(1+p2+p2^2+……+p2^e2)……(1+pn+pn^2+……+pn^en)

(把式子化简就知道为什么了)

ac代码:

#include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
ll pow(ll x,ll n)
{
    ll f=1;
    for(ll i=1;i<=n;i++)
    {
        f*=x;
    }
    return f;
}
ll solve(ll n)
{
    ll ans=1;
    for(ll i=2;i*i<=n;i++)
    {
        if(n%i==0)
        {
            //cout<<i<<endl;
            ll temp=1;
            ll ret=1;
            while(n%i==0)
            {
                temp+=pow(i,ret++);
                n/=i;
            }
            ans*=temp;
        }
    }
     if(n>1) ans*=(n+1);
    return ans;
}
int main()
{
    ll t;
    cin>>t;
    while(t--)
    {
        ll n;
        cin>>n;
        cout<<solve(n)-n<<endl;
    }
    return 0;
}      

转载于:https://www.cnblogs.com/z1141000271/p/7183180.html

php