Project Euler Problem 675

ORZ foreverlasting聚聚,QQ上问了他好久才会了这题(所以我们又聊了好久的Gal)

我们先来尝试推导一下\(S\)的性质,我们利用狄利克雷卷积来推:

\[2^\omega=I\ast|\mu|\]

这个很好理解吧,考虑一下它的组合意义即可

然后两边同卷上\(I\)有:

\[2^\omega \ast I=I\ast I\ast |\mu|=d\ast |\mu|\]

后面还是同样,考虑\(d\ast |\mu|\)的组合意义,一正一反的情况下其实就是\(d(n^2)\)

因此我们有了\(2^\omega\ast I=d(n^2)\),即\(S(n)=d(n^2)\)

那么显然\(S\)现在是个积性函数了,答案又是阶乘的形式,因此可以从\(n-1\)的答案推到\(n\)来

考虑一个非常暴力的过程,每次暴力分解质因数,复杂度大概是\(O(n\sqrt n)\)的

然后你只需要一台好一点的电脑我仿佛已经闻到了CPU的香气

然后考虑怎么优化这个过程,我们发现类似于某个套路,这种方法之所以慢是因为会出现不必要的枚举,因此我们只需要记录一下每个数的最小质因数,然后每次直接除去即可,顺带把贡献算一下

这样的复杂度很迷啊,加藤聚聚说是一个\(\log\)的,我感觉还要再少点,毕竟向下除至少去掉一个\(2\)

那么我们就可以很快的做掉这道题了(用自己的笔记本跑了2s就出来了)

#include<cstdio>
#define RI register int
#define CI const int&
using namespace std;
const int N=10000000,mod=1000000087;
int prime[N+5],cnt,mnp[N+5],bkt[N+5],inv[(N<<1)+5],ret=1,ans;
#define Pi prime[j]
inline void init(void)
{
    RI i,j; for (mnp[1]=1,i=2;i<=N;++i)
    {
        if (!mnp[i]) mnp[i]=i,prime[++cnt]=i;
        for (j=1;j<=cnt&&1LL*i*Pi<=N;++j)
        {
            mnp[i*Pi]=Pi; if (i%Pi==0) break;
        }
    }
    for (inv[0]=inv[1]=1,i=2;i<=(N<<1)+1;++i)
    inv[i]=1LL*inv[mod%i]*(mod-mod/i)%mod;
}
#undef Pi
inline void inc(int& x,CI y)
{
    if ((x+=y)>=mod) x-=mod;
}
inline int sum(CI x,CI y)
{
    int t=x+y; return t>=mod?t-mod:t;
}
int main()
{
    freopen("ans.txt","w",stdout);
    init(); for (RI i=2;i<=N;++i)
    {
        ret=1LL*ret*inv[bkt[i]+1]%mod; inc(bkt[i],2); ret=1LL*ret*(bkt[i]+1)%mod;
        for (int x=i;x!=mnp[x];x/=mnp[x])
        {
            if (x/mnp[x]==mnp[x])
            {
                ret=1LL*ret*inv[bkt[x]+1]%mod*inv[bkt[mnp[x]]+1]%mod;
                inc(bkt[mnp[x]],sum(bkt[x],bkt[x]));
                ret=1LL*ret*(bkt[mnp[x]]+1)%mod; bkt[x]=0;
            } else
            {
                ret=1LL*ret*inv[bkt[x]+1]%mod*inv[bkt[mnp[x]]+1]%mod*inv[bkt[x/mnp[x]]+1]%mod;
                inc(bkt[mnp[x]],bkt[x]); inc(bkt[x/mnp[x]],bkt[x]);
                ret=1LL*ret*(bkt[mnp[x]]+1)%mod*(bkt[x/mnp[x]]+1)%mod; bkt[x]=0;
            }
        }
        inc(ans,ret);
    }
    return printf("%d",ans),0;
}

原文地址:https://www.cnblogs.com/cjjsb/p/11518890.html

时间: 2024-10-30 14:43:36

Project Euler Problem 675的相关文章

[Perl 6][Project Euler] Problem 9 - Special Pythagorean triplet

[Perl 6][Project Euler] Problem 9 - Special Pythagorean triplet Description A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, a^2 + b^2 = c^2 For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2. There exists exactly one Pythagore

Project Euler: Problem 14 Longest Collatz sequence

The following iterative sequence is defined for the set of positive integers: n → n/2 (n is even) n → 3n + 1 (n is odd) Using the rule above and starting with 13, we generate the following sequence: 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1 It can b

Project Euler problem 68

题意需要注意的一点就是, 序列是从外层最小的那个位置顺时针转一圈得来的.并且要求10在内圈 所以,这题暴力的话,假定最上面那个点一定是第一个点,算下和更新下就行. #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <cstdlib> #include <ctime> #include <set> #i

Project Euler problem 69

考察欧拉函数的一道题 首先要知道 [定理]正整数n(n≥2)可以唯一分解成素数乘积,即:n =p[1]^r1 * p[2] ^r2 * p[3]^r3. *...* p[s]^rs 其次欧拉函数有两个性质,可以用来编程,单独求phi函数: ① phi(m) =  m ( 1- 1/p[1]) ( 1- 1/p[2])-( 1- 1/p[s]) ② phi(p^k) = p^k – p^(k-1) = (p-1)p^(k-1) 然后 m/phi(m)  = (p[1] *p[2]*p[3]*

Project Euler:Problem 12 Highly divisible triangular number

The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be: 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ... Let us list the factors of the fir

Project Euler: Problem 9 Special Pythagorean triplet

A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, a2 + b2 = c2 For example, 32 + 42 = 9 + 16 = 25 = 52. There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc. 首先联立式子,消除c,得到关于ab的等式5

Project Euler:Problem 11 Largest product in a grid

In the 20×20 grid below, four numbers along a diagonal line have been marked in red. 08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08 49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00 81 49 31 73 55 79 14 29 93 71 40 67 53 88 30

Project Euler: Problem 17 Number letter counts

If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total. If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be

Python练习题 048:Project Euler 021:10000以内所有亲和数之和

本题来自 Project Euler 第21题:https://projecteuler.net/problem=21 ''' Project Euler: Problem 21: Amicable numbers Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n). If d(a) = b and d(b) = a, where a ≠ b