跳到正文

剪贴板

剪贴板 pso4jtlg

原文
剪贴板信息

剪贴板信息

收录
更新
C++
#include <bits/stdc++.h>
#define int long long
#define un unsigned
using namespace std;
const int N=2000010,INF=0x3f3f3f3f,p=13331;
int n,m,ans;char a[N];
un int h[N],op[N],cur;multiset<un int>s;
static char buf[1000000],*paa=buf,*pd=buf;
#define getchar() paa==pd&&(pd=(paa=buf)+fread(buf,1,1000000,stdin),paa==pd)?EOF:*paa++
inline int read(void){
	int u(0),t(1);char fc(getchar());
	while(!isdigit(fc)){if(fc=='-') t=-1;fc=getchar();}
	while(isdigit(fc)) u=(u<<1)+(u<<3)+(fc^48),fc=getchar();
	return u*t;
}
inline void print(int u)
{
	if(u<0) putchar('-'),u=-u;
	if(u>9) print(u/10);
	putchar(u%10+'0');
}
un int gethsh(int l,int r){return h[r]-h[l-1]*op[r-l+1];}
bool check(int x){for(int i=1;i<=m;i+=x) if(gethsh(i,i+x-1)!=cur)return 0;return 1;}
namespace KMP
{
	inline bool chk(char c) { return !(c>='a'&&c<='z'); }
	inline bool ck(char c) { return c!='\n'&&c!='\r'&&c!=-1; }
	inline void rd(char s[],int&n)
	{
		s[++n]=getchar();
		while(chk(s[n])) s[n]=getchar();
		while(ck(s[n])) s[++n]=getchar();
		n--;
	}
}
using namespace KMP;
signed main()
{
	n=read();op[0]=1;for(int i=op[0]=1;i<=N-10;i++) op[i]=op[i-1]*p;
	for(int j=1;j<=n;j++)
	{
		m=0;read();rd(a,m);
		for(int i=1;i<=m;i++) h[i]=h[i-1]*p+a[i];
		for(int i=1;i<=m;i++)
		{
			if(m%i)continue;cur=gethsh(1,i);
			if(check(i)){s.insert(cur);break;}
		}
	}
	while(!s.empty())
	{
		int x=*s.lower_bound(0),xx=s.count(x);
		ans+=xx*xx;s.erase(x);
	}
	print(ans);return 0;
}