码迷,mamicode.com
首页 > 其他好文 > 详细

[Project Euler 429] Sum of squares of unitary divisors(数论)

时间:2018-08-23 22:14:46      阅读:293      评论:0      收藏:0      [点我收藏+]

标签:ros   href   thml   present   type   pow   ram   clu   find   

题目链接:https://projecteuler.net/problem=429

题目:

A unitary divisor dd of a number nn is a divisor of nn that has the property gcd(d,n/d)=1

The unitary divisors of 4!=24 1,3,8 and 24.

The sum of their squares is $1^2+3^2+8^2+24^2$=650.

Let S(n) represent the sum of the squares of the unitary divisors of nn. Thus S(4!)=650

Find S(100000000!) modulo 1000000009.

题解:

技术分享图片

第二行的"所以 S(n2) "改成"所以 S(n!)"

代码如下:

#include<algorithm>
#include<cstring>
#include<cstdio>
#include<iostream>
using namespace std;
typedef long long ll;

const int N=1e8+15;
const int mod=1e9+9;
ll n,tot,ans;
ll prime[N],vis[N];
void get_prime()
{
    for (ll i=2;i<=n;i++)
    {
        if (!vis[i]) prime[++tot]=i;
        for (ll j=1;j<=tot&&prime[j]*i<=n;j++)
        {
            vis[prime[j]*i]=1;
            if (i%prime[j]==0) break;
        }
    }
}
ll mul(ll a,ll b)
{
    ll res=0;
    for (;b;b>>=1,a=(a+a)%mod) if (b&1) res=(res+a)%mod;
    return res;
}
ll qpow(ll a,ll b)
{
    ll res=1;
    for (;b;b>>=1,a=mul(a,a)%mod) if (b&1) res=res*a%mod;
    return res;
}
ll solve(ll a,ll b)
{
    ll res=0;
    while (a)
    {
        a/=b;
        res+=a;
    }
    return res;
}
int main()
{
    //scanf("%lld",&n);
    n=1e8;
    ans=1;
    get_prime();
    for (ll i=2;i<=n;i++)
    {
        if (!vis[i])
        {
            ll power=solve(n,i);
            ans=1ll*ans*(qpow(i,power*2)+1)%mod;
        }
    }
    printf("%lld",ans);
    return 0;
}

本博客内容转自http://www.cnblogs.com/LzyRapx/p/8280943.html

[Project Euler 429] Sum of squares of unitary divisors(数论)

标签:ros   href   thml   present   type   pow   ram   clu   find   

原文地址:https://www.cnblogs.com/xxzh/p/9526372.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!