1780G - Delicious Dessert - CodeForces Solution


binary search math number theory string suffix structures

Please click on ads to support us..

C++ Code:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
using namespace std;
struct SuffixArray
{
    typedef long long ll;
	#define N 1000005
	char s[N];
	int n,m,par[N],sz[N],cur[N];
	int *x,*y,X[N],Y[N],c[N],sa[N],height[N],Rank[N];
    ll ans;
    vector<int>add[N];
    inline int find(int x){
        return par[x]==x?x:par[x]=find(par[x]);
    }
	inline void clear()
	{
	    memset(X,0,sizeof(X));memset(Y,0,sizeof(Y));memset(c,0,sizeof(c));
	    memset(sa,0,sizeof(sa));memset(height,0,sizeof(height));memset(Rank,0,sizeof(Rank));
	}
	inline void init()
	{
        ans=0;
		scanf("%d%s",&n,s);
        for(int i=0;i<=n;++i){
            par[i]=i;
            sz[i]=1;
        }
        cur[1]=n+1;
	}
	inline void get_sa(int _m=30)//_m为字符集大小 
	{
		m=_m;
	    x=X,y=Y;
	    for (int i=0;i<m;++i) c[i]=0;
	    for (int i=0;i<n;++i) x[i]=s[i]-'a',++c[x[i]];
	    for (int i=1;i<m;++i) c[i]+=c[i-1];
	    for (int i=n-1;i>=0;--i) sa[--c[x[i]]]=i;
	
	    for (int k=1;k<=n;k<<=1)
	    {
	        int p=0;
	        for (int i=n-k;i<n;++i) y[p++]=i;
	        for (int i=0;i<n;++i) if (sa[i]>=k) y[p++]=sa[i]-k;
	
	        for (int i=0;i<m;++i) c[i]=0;
	        for (int i=0;i<n;++i) ++c[x[y[i]]];
	        for (int i=1;i<m;++i) c[i]+=c[i-1];
	        for (int i=n-1;i>=0;--i) sa[--c[x[y[i]]]]=y[i];
	
	        swap(x,y);
	        p=1;x[sa[0]]=0;
	        for (int i=1;i<n;++i)
	            x[sa[i]]=y[sa[i-1]]==y[sa[i]]&&((sa[i-1]+k<n?y[sa[i-1]+k]:-1)==(sa[i]+k<n?y[sa[i]+k]:-1))?p-1:p++;
	        if (p>n) break;
	        m=p;
	    }
	}
	inline void get_height()
	{
	    for (int i=0;i<n;++i) Rank[sa[i]]=i;
	    int k=0;height[0]=0;
	    for (int i=0;i<n;++i)
	    {
	        if (!Rank[i]) continue;
	        if (k) --k;
	        int j=sa[Rank[i]-1];
	        while (i+k<n&&j+k<n&&s[i+k]==s[j+k]) ++k;//必须是原数组s 不能是现数组X/x
	        height[Rank[i]]=k;
	    }
	}
	//Rank[i]:下标位置在i的后缀的排名 
	//sa[i]:后缀排名第i的下标位置  
	//Rank和sa互为反函数 范围均在[0,n-1] 
	//height[i]:排名第i和排名第i-1的LCP长度 
	void PR()
	{
		string p(s);
		for(int i=0;i<n;++i)
		printf("Rank[%d]:%d\n",i,Rank[i]);
		for(int i=0;i<n;++i)
		{
		 	printf("sa[%d]:%d ",i,sa[i]);
		 	cout<<p.substr(sa[i])<<endl;
		}
		for(int i=0;i<n;++i)
		printf("height[%d]:%d\n",i,height[i]);
	}
    inline void un(int x,int y){
        x=find(x),y=find(y);
        if(x==y)return;
        cur[sz[x]]--;
        cur[sz[y]]--;
        par[y]=x;
        sz[x]+=sz[y];
        cur[sz[x]]++;
    }
    void solve()
    {
        init();
        get_sa();
        get_height();
        //PR();
        for(int i=1;i<=n;++i){
            if(height[i]>=1){
                add[height[i]].push_back(i);
            }
        }
        for(int i=n;i>1;--i){
            for(auto &x:add[i]){
                un(x-1,x);
            }
            //cout<<"i:"<<i<<endl;
            //for(int j=1;j<=n;++j){
            //    cout<<"cur["<<j<<"]:"<<cur[j]<<endl;
            //}
            for(int j=i;j<=n;j+=i){
                ans+=1ll*j*cur[j];
            }
        }
        cout<<ans+n<<endl;
    }
}sa;
int main()
{
    sa.solve();
    return 0; 
} 
//1 3 1 >=1 说明a出现了4次
//3 >=3 aba出现了1次 ab出现了1次


Comments

Submit
0 Comments
More Questions

1626A - Equidistant Letters
977D - Divide by three multiply by two
1654B - Prefix Removals
1654A - Maximum Cake Tastiness
1649A - Game
139A - Petr and Book
1612A - Distance
520A - Pangram
124A - The number of positions
1041A - Heist
901A - Hashing Trees
1283A - Minutes Before the New Year
1654D - Potion Brewing Class
1107B - Digital root
25A - IQ test
785A - Anton and Polyhedrons
1542B - Plus and Multiply
306A - Candies
1651C - Fault-tolerant Network
870A - Search for Pretty Integers
1174A - Ehab Fails to Be Thanos
1169A - Circle Metro
780C - Andryusha and Colored Balloons
1153A - Serval and Bus
1487C - Minimum Ties
1136A - Nastya Is Reading a Book
1353B - Two Arrays And Swaps
1490E - Accidental Victory
1335A - Candies and Two Sisters
96B - Lucky Numbers (easy)