Submission #112116

# Submission time Handle Problem Language Result Execution time Memory
112116 2019-05-17T12:43:18 Z aleksami Palindromic Partitions (CEOI17_palindromic) C++14
100 / 100
168 ms 34696 KB
#include <bits/stdc++.h>

using namespace std;
#define MAXN 1000005
typedef long long ll;
const ll p = 131;
const ll MOD = 1e9+7;
ll addmod(ll x,ll y)
{
  x+=y;
  x+=MOD;
  x%=MOD;
  return x;
}
ll mulmod(ll x,ll y)
{
  x*=y;
  x%=MOD;
  return x;
}

ll pw[MAXN];
ll invpw[MAXN];

ll fastpow(ll x,int s)
{
  if(s==0)return 1LL;
  if(s%2==0)
  {
    ll y = fastpow(x,s/2);
    return mulmod(y,y);
  }
  return mulmod(x,fastpow(x,s-1));
}

void init()
{
  pw[0]=1LL;
  for(int i = 1; i < MAXN; i++)pw[i]=mulmod(pw[i-1],p);
  invpw[0]=1LL;
  ll invp = fastpow(p,MOD-2);
  for(int i = 1; i < MAXN; i++)invpw[i]=mulmod(invpw[i-1],invp);
}
ll sum[MAXN];
string s;
int n;

ll geth(ll x,int s)
{
  return mulmod(x,pw[s]);
}
ll get(int l,int r)
{
  ll ret = sum[r];
  if(l>0)ret=addmod(ret,-sum[l-1]);
  ret=mulmod(ret,invpw[l]);
  return ret;
}

int solve(int left,int right)
{
  for(int d = 1; left+d-1 < right-d+1; d++)
  {
    if(get(left,left+d-1)==get(right-d+1,right))return 2+solve(left+d,right-d);
  }
  if(left <= right)return 1;
  return 0;
}

void solve()
{
  cin >> s;
  n = s.length();
  for(int i = 0; i < n; i++)
  {
    sum[i]=geth(s[i],i);
    if(i>0)sum[i]=addmod(sum[i],sum[i-1]);
  }
  cout << solve(0,n-1) << "\n";
}

int main()
{
  ios_base::sync_with_stdio(false);
  init();
  int t;
  cin >> t;
  while(t--)
  {
    solve();
  }
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 22 ms 16000 KB Output is correct
2 Correct 23 ms 16016 KB Output is correct
3 Correct 24 ms 16000 KB Output is correct
4 Correct 25 ms 16100 KB Output is correct
5 Correct 25 ms 15992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 16000 KB Output is correct
2 Correct 23 ms 16016 KB Output is correct
3 Correct 24 ms 16000 KB Output is correct
4 Correct 25 ms 16100 KB Output is correct
5 Correct 25 ms 15992 KB Output is correct
6 Correct 23 ms 16000 KB Output is correct
7 Correct 24 ms 15992 KB Output is correct
8 Correct 27 ms 15992 KB Output is correct
9 Correct 23 ms 15992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 16000 KB Output is correct
2 Correct 23 ms 16016 KB Output is correct
3 Correct 24 ms 16000 KB Output is correct
4 Correct 25 ms 16100 KB Output is correct
5 Correct 25 ms 15992 KB Output is correct
6 Correct 23 ms 16000 KB Output is correct
7 Correct 24 ms 15992 KB Output is correct
8 Correct 27 ms 15992 KB Output is correct
9 Correct 23 ms 15992 KB Output is correct
10 Correct 25 ms 16248 KB Output is correct
11 Correct 24 ms 16120 KB Output is correct
12 Correct 25 ms 16256 KB Output is correct
13 Correct 24 ms 16092 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 16000 KB Output is correct
2 Correct 23 ms 16016 KB Output is correct
3 Correct 24 ms 16000 KB Output is correct
4 Correct 25 ms 16100 KB Output is correct
5 Correct 25 ms 15992 KB Output is correct
6 Correct 23 ms 16000 KB Output is correct
7 Correct 24 ms 15992 KB Output is correct
8 Correct 27 ms 15992 KB Output is correct
9 Correct 23 ms 15992 KB Output is correct
10 Correct 25 ms 16248 KB Output is correct
11 Correct 24 ms 16120 KB Output is correct
12 Correct 25 ms 16256 KB Output is correct
13 Correct 24 ms 16092 KB Output is correct
14 Correct 162 ms 34696 KB Output is correct
15 Correct 103 ms 30096 KB Output is correct
16 Correct 168 ms 34220 KB Output is correct
17 Correct 92 ms 25876 KB Output is correct