Project Euler Problem 675

ORZ foreverlasting get together, asked him for a long time before the QQ on this topic(So ​​we talked for a long time Gal)

Let's try to deduce what \ (S \) character, we use Dirichlet convolution to push:

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

This is well understood it, consider its significance to the combination

Then the same volume on both sides of the \ (the I \) are:

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

Later still the same, consider \ (d \ ast | \ mu | \) a combination of sense, the case of a positive and negative is actually \ (d (n ^ 2) \)

Thus we have \ (2 ^ \ Omega \ AST the I = D (n-2 ^) \) , i.e. \ (S (n) = d (n ^ 2) \)

Then clearly \ (S \) is now a multiplicative function , the answer is in the form of factorial, can be from \ (n-1 \) answers pushed \ (n-\) to

Consider a very violent process, every violent decomposition of the quality factor, the complexity is probably \ (O (n \ sqrt n ) \) of

Then you just need a little better computerI seem to have smelled the aroma of CPU

Then consider how to optimize this process, we found a similar routine, the reason why this method is slow because there will be unnecessary to enumerate, so we only need to look at each record number of minimum quality factor , then remove each direct you can, incidentally, contributed to count

This complexity is lost, ah, get together Kato said to be a \ (\ log \) , I feel even then less, after all, in addition to at least down to remove a \ (2 \)

Then we can quickly make out of this question (using their own laptop ran 2s came out)

#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;
}

Guess you like

Origin www.cnblogs.com/cjjsb/p/11518890.html
Recommended