QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#356657#6567. Repetitive String Inventionec1117TL 13ms64808kbC++144.0kb2024-03-18 07:09:512024-03-18 07:09:52

Judging History

你现在查看的是最新测评结果

  • [2024-03-18 07:09:52]
  • 评测
  • 测评结果:TL
  • 用时:13ms
  • 内存:64808kb
  • [2024-03-18 07:09:51]
  • 提交

answer

#include <bits/stdc++.h>
#include <random>
using namespace std;
 
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pi;
typedef vector<int> vi; 
typedef vector<pi> vpi;

#define mp make_pair
#define f first
#define s second
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rsz resize
#define bk back()
#define pb push_back

#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define For(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define trav(a,x) for (auto& a: x)

const int MOD = 1e9+7;
const int MX = 807;
const int MX2 = 407;
const ld PI = acos((ld)-1);
mt19937 rng; // or mt19937_64
template<class T> bool ckmin(T& a, const T& b) { 
	return b < a ? a = b, 1 : 0; }

void DBG() { cerr << "]" << endl; }
template<class H, class... T> void DBG(H h, T... t) {
	cerr << h; if (sizeof...(t)) cerr << ", ";
	DBG(t...); }
#ifdef LOCAL // compile with -DLOCAL
#define dbg(...) cerr << "LINE(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
#else
#define dbg(...) 0
#endif

/**
 * Description: Multiply two 64-bit integers mod another if 128-bit is not available.
	* modMul is equivalent to \texttt{(ul)(\_\_int128(a)*b\%mod)}. 
	* Works for $0\le a,b<mod<2^{63}.$
 * Source: KACTL
 * Verification: see "Faster Factoring"
 */

typedef unsigned long long ul;
ul modMul(ul a, ul b, const ul mod) {
	ll ret = a*b-mod*(ul)((ld)a*b/mod);
	return ret+((ret<0)-(ret>=(ll)mod))*mod; }
ul modPow(ul a, ul b, const ul mod) {
	if (b == 0) return 1;
	ul res = modPow(a,b/2,mod); res = modMul(res,res,mod);
	return b&1 ? modMul(res,a,mod) : res;
}

map<pi,int> M;
int pr = 1007, pw[MX];
map<int,int> toPush[MX][MX], toPush2[MX][MX], have[MX], have2[MX];

map<int,int> sameLen[MX];
//have is when later is longer end
//have2 is for shorter end

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0); 
	string S;cin>>S;
	int n = sz(S);
	pw[0]=1;
	ll ans = 0;
	FOR(i,1,n+1)
		pw[i] = pw[i-1]*pr%MOD;

	For(i,n) {
		ll cur = 0;
		FOR(j,i,n) {
			cur = (cur*pr+ (S[j]-'a')+1)%MOD;
			M[{i,j}] = cur;
		}
	}

	
	ll ans2 = 0;
	For(i,n) {
		FOR(j,i,n) { // i to j
			ans2 += sameLen[j-i+1][M[{i,j}]];
		}
		For(j,i+1) {// j to i
			int len = i-j+1;
			sameLen[len][M[{j,i}]]++;
		}

	}

	For(i,n) {
		For(j,MX) { //j is len
			trav(x,toPush2[i][j]) {
				have2[j][x.f] += x.s;
			}
			trav(x,toPush[i][j]) {
				have[j][x.f] += x.s;
			}
		}

		ll cur = 0;
		FOR(j,i,n) { //i st, j end
			int len = j-i+1;
			cur = (cur*pr+ (S[j]-'a')+1)%MOD;
			
			for(int k=j-1;k>=0 && 2*(k-i+1)>len; k--) { // i to k
				dbg(i,k,j);
				int newLen = k-i+1;
				int needLen = 2*newLen - len;

				// ll val = v[k-i];
				ll val = M[{i,k}];
				ll fin = (val*pw[newLen]+val)%MOD;
				ll want = (fin-(cur*pw[needLen])%MOD+MOD)%MOD;
				dbg("D",val,fin,want,needLen);
				toPush2[j+1][needLen][want]++;

			}
			dbg("A",j+1,len,cur);
			toPush[j+1][len][cur]++;
		}
		
		cur = 0;
		FOR(j,i,n) { // second part
			int len = j-i+1;
			cur = (cur*pr+ (S[j]-'a')+1)%MOD;

			dbg("C",len,cur, have2[len][cur]);
			ans += have2[len][cur];

			for(int k=i+1;k<j && 2*(j-k+1)>len;k++) { // k to j
				int newLen = j-k+1;
				int needLen = 2*newLen - len;

				ll val = M[{k,j}];
				ll fin = (val*pw[newLen]+val)%MOD;
				ll want = ((fin-cur)%MOD+MOD)*modPow(pw[len],MOD-2,MOD)%MOD;
				dbg("B",val, fin, needLen,want, have[needLen][want]);
				ans += have[needLen][want];
			}

		}
	}
	dbg(ans2);
	cout << ans+ans2 << '\n';

}

/*
FOR(i,1,n+1) {
	int cur = 0;
	For(j,n) {
		cur = (cur*pr + (S[j]-'a'))%MOD;
		if(j>=i-1) {
			M[i][cur]++;
		}
	}
}

FOR(i,1,n/2+1) {
	//count strings of len 2i
	int len = 2*i;
	FOR(j,1,len) {
		int k = 2*i-j;
		

	}
}

*/


/* stuff you should look for
	* int overflow, array bounds
	* special cases (n=1?)
	* do smth instead of nothing and stay organized
	* WRITE STUFF DOWN
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 13ms
memory: 64808kb

input:

aaaa

output:

9

result:

ok single line: '9'

Test #2:

score: 0
Accepted
time: 8ms
memory: 64776kb

input:

axabxbcxcdxd

output:

22

result:

ok single line: '22'

Test #3:

score: -100
Time Limit Exceeded

input:

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...

output:


result: