zoukankan      html  css  js  c++  java
  • bzoj 4916: 神犇和蒟蒻 (杜教筛+莫比乌斯反演)

    题目大意:

      读入n。

      第一行输出“1”(不带引号)。

      第二行输出$sum_{i=1}^n iphi(i)$。

    题解:

      所以说那个$summu$是在开玩笑么=。=

      设$f(n)=nphi(n),F(n)=sum_{i=1}^{n}f(i)$。

      设$g=(f*id)$,则$g(n)=sum_{d|n}id(frac{n}{d})f(d)=n^2$。

      设$G(n)=sum_{i=1}^n g(i)=frac{n(n+1)(2n+1)}{6}$。

      同时将$G$完全展开我们得到:

    $G(n)=sum_{i=1}^{n}sum_{d|i}d*f(frac{i}{d})$

    $=sum_{d=1}^{n}dsum_{i=1}^{lfloor frac{n}{d} floor}f(i)$

    $=sum_{d=1}^{n}dF(lfloor frac{n}{d} floor)$

      由此可得:$$F(n)=frac{n(n+1)(2n+1)}{6}-sum_{i=2}^{n}F(lfloor frac{n}{i} floor)i$$

    代码:

     

     1 #define Troy
     2 
     3 #include <bits/stdc++.h>
     4 
     5 using namespace std;
     6 
     7 inline int read(){
     8     int s=0,k=1;char ch=getchar();
     9     while(ch<'0'|ch>'9')    ch=='-'?k=-1:0,ch=getchar();
    10     while(ch>47&ch<='9')    s=s*10+(ch^48),ch=getchar();
    11     return s*k;
    12 }
    13 
    14 const int N=1e6+5,mod=1e9+7;
    15 
    16 int phi[N],prim[N],num,vis[N],F[N],f[N],n,re6=166666668,re2=500000004;
    17 map<int,int> mp;
    18 
    19 inline int calc(int x){
    20     if(x<N) return F[x];
    21     else    if(mp.count(x)) return mp[x];
    22     int ret=x*1ll*(x+1)%mod*(2ll*x+1)%mod*re6%mod;
    23     for(register int i=2,j;i<=x;i=j+1){
    24         j=x/(x/i);
    25         ret=(ret-(j-i+1ll)*(i+j)/2%mod*calc(x/i)%mod)%mod;
    26         if(ret<0)   ret+=mod;
    27     }
    28     return mp[x]=ret;
    29 }
    30 
    31 int main(){
    32     n=read();puts("1");
    33     register int i,j,k;
    34     phi[1]=1;
    35     for(i=2;i<N;++i){
    36         if(!vis[i]) prim[++num]=i,phi[i]=i-1;
    37         for(j=1;(k=prim[j]*i)<N;++j){
    38             vis[k]=true;
    39             if(i%prim[j]){
    40                 phi[k]=phi[i]*1ll*(prim[j]-1)%mod;
    41                 continue;
    42             }  
    43             phi[k]=phi[i]*1ll*prim[j]%mod;break;
    44         }
    45     }
    46     for(i=1;i<N;++i)    f[i]=1ll*phi[i]*i%mod,F[i]=(F[i-1]*1ll+f[i])%mod;
    47     printf("%d
    ",(calc(n)+mod)%mod);
    48 }
  • 相关阅读:
    python3中内置函数map 和 reduce函数的使用
    爬山算法和模拟退火算法
    Link-Cut Tree(LCT)
    启发式搜索——A*算法
    树上分块
    CodeChef TRIPS-Children Trips 树上分块
    CodeChef:Chef and Problems(分块)
    莫队算法
    Konig定理及证明
    块状链表
  • 原文地址:https://www.cnblogs.com/Troywar/p/8029537.html
Copyright © 2011-2022 走看看