제출 #1004358

#제출 시각아이디문제언어결과실행 시간메모리
1004358De3b0oCopy and Paste 3 (JOI22_copypaste3)C++14
1 / 100
11 ms24312 KiB
#include<bits/stdc++.h>
#define ll long long
#define F first
#define S second
#define in insert
#define pb push_back
#define ppb pop_back()
#define d3 ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define cans cout << ans << "\n";
#define yes cout << "Yes" << "\n";
#define no cout << "No" << "\n";
#define pll pair<ll,ll>
#define lin cout << "\n";
#define sqr 340
#define mod 1000000007
#define mid ((l+r)/2)
#define lc (2*x)
#define rc (2*x+1)

using namespace std;

ll n;
string s;
ll A , B , C;
bitset<1600000090> e;
ll dp[209][209];
vector<ll> idx[1009][1009];

ll fp(ll x , ll y)
{
    if(y==0)
        return 1;
    ll z = fp(x,y/2);
    if(y&1)
        return z*z%mod*x%mod;
    else
        return z*z%mod;
}

ll gt(ll i , ll j , ll l1 , ll r1)
{
    ll l = 0 , r = idx[i][j].size()-1;
    ll sum = 0;
    while(l<=r)
    {
        if(idx[i][j][mid]>r1)
            r=mid-1;
        else
        {
            sum=mid+1;
            l=mid+1;
        }
    }
    sum*=(j-i+1);
    return sum;
}

ll solve(ll l , ll r)
{
    if(dp[l][r])
        return dp[l][r];
    ll ans = (r-l+1)*A;
    for(ll i = l ; r>i ; i++)
    {
        for(ll j = i ; r>j ; j++)
        {
            ll ans1 = solve(i,j) + B + (i-l)*A + C;
            ll y = gt(i,j,j+1,r);
            ans1+=y/(j-i+1)*C;
            ans1+=(r-j-y)*A;
            ans=min(ans,ans1);
        }
    }
    dp[l][r]=ans;
    return ans;
}

int main()
{
    mt19937 rnd(time(0));
    cin >> n;
    cin >> s;
    cin >> A >> B >> C;
    ll p = rnd();
    for(int l = 1 ; n>=l ; l++)
    {
        map<ll,pll> mp;
        ll hsh = 0;
        ll pr = 1;
        for(int i = n-l ; i>n-2*l ; i--)
        {
            ll y = int(s[i]);
            hsh+=y*pr;
            hsh%=mod;
            pr*=p;
            pr%=mod;
        }
        mp[hsh]={n-2*l+1,n-l};
        pr=fp(p,l-1);
        for(int j = n-l-1 ; j>=0 ; j--)
        {
            int i = j-l+1;
            ll y = int(s[j+1]);
            hsh-=y;
            hsh%=mod;
            hsh+=mod;
            hsh%=mod;
            hsh*=fp(p,mod-2)%mod;
            y=int(s[i]);
            hsh+=y*pr;
            hsh%=mod;
            if(mp[hsh].F!=0)
            {
                idx[i][j]=idx[mp[hsh].F][mp[hsh].S];
                if(mp[hsh].F>j)
                    idx[i][j].pb(mp[hsh].S);
            }
            mp[hsh]={i,j};
        }
    }
    for(int i = 0 ; n>i ; i++)
        for(int j = i ; n>j ; j++)
            reverse(idx[i][j].begin(),idx[i][j].end());
    ll ans = solve(0,n-1);
    cans
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...