NOIP 模拟 $26\; \rm 降雷皇$

题解 \(by\;zj\varphi\)

用树状数组优化一下求最长上升子序列即可。

至于第二问,在求出答案后开 \(n\) 棵线段树,每颗维护当前最长上升子序列长度的方案数。

Code
#include<bits/stdc++.h>
#define ri register signed
#define p(i) ++i 
using namespace std;
namespace IO{
    char buf[1<<21],*p1=buf,*p2=buf,OPUT[100];
    #define gc() p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?(-1):*p1++
    template<typename T>inline void read(T &x) {
        ri f=1;x=0;register char ch=gc();
        while(!isdigit(ch)) {if (ch=='-') f=0;ch=gc();}
        while(isdigit(ch)) {x=(x<<1)+(x<<3)+(ch^48);ch=gc();}
        x=f?x:-x;
    } 
    template<typename T>inline void print(T x) {
        if (x<0) putchar('-'),x=-x;
        if (!x) return putchar('0'),(void)putchar('\n');     
        ri cnt(0);
        while(x) OPUT[p(cnt)]=x%10,x/=10;
        for (ri i(cnt);i;--i) putchar(OPUT[i]^48);
        return (void)putchar('\n');   
    }
}
using IO::read;using IO::print;
namespace nanfeng{
    #define FI FILE *IN
    #define FO FILE *OUT
    template<typename T>inline T cmax(T x,T y) {return x>y?x:y;}
    template<typename T>inline T cmin(T x,T y) {return x>y?y:x;}
    typedef long long ll;
    static const int N=1e7+7;
    int vis[N],phi[N],prim[N],nm[N],cnt,sn;    
    ll n,ans;
    inline void Getphi(int n) {
        for (ri i(2);i<=n;p(i)) {
            if (!vis[i]) phi[i]=i-1,vis[prim[p(cnt)]=i]=i;
            for (ri j(1);j<=cnt&&prim[j]*i<=n;p(j)) {
                int nw=i*prim[j];
                vis[nw]=prim[j];
                if (vis[i]==prim[j]) {
                    phi[nw]=phi[i]*prim[j];  
                    break;
                }
                else phi[nw]=phi[i]*(prim[j]-1);
            }  
        }
    }
    inline int main() {
        //FI=freopen("nanfeng.in","r",stdin);
        //FO=freopen("nf.out","w",stdout);
        read(n);
        sn=sqrt(n);
        Getphi(sn);
        for (ri i(2);i<=sn;p(i)) ans+=(ll)phi[i]*(n/i/i);
        print(ans);
        return 0;
    }
}
int main() {return nanfeng::main();}
上一篇:[算法] 高斯消元详解


下一篇:虚函数求面积