Submission #540624

#TimeUsernameProblemLanguageResultExecution timeMemory
540624Killer2501Trener (COCI20_trener)C++14
110 / 110
66 ms23152 KiB
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define ull unsigned long long
#define pb push_back
#define pll pair<ll, ll>
#define pii pair<int, int>
#define fi first
#define se second
using namespace std;
const int N = 2e3+5;
const int M = 1e5+5;
const int mod = 1e9+7;
const ll base = 327;
const ll inf = 1e15+7;
int c[N], d[N], ans, tong, dp[N], h[N];
int n, m, k;
int a[52][N][52], b[N];
vector<int> adj[N], vi, gr[N];
void add(int& x, int y)
{
    x += y;
    if(x >= mod)x -= mod;
}
struct BIT
{
    int n;
    vector<ll> fe;
    BIT(){}
    void init(int _n)
    {
        n = _n;
        fe.assign(n+1, 0);
    }
    void add(int id, ll x)
    {
        for(; id <= n; id += id & -id)fe[id] += x;
    }
    void add(int l, int r, ll x)
    {
        add(l, x);
        add(r+1, -x);
    }
    ll get(int id)
    {
        ll res = 0;
        for(; id; id -= id & -id)res += fe[id];
        return res;
    }
}L, R;
struct query
{
    int x, y, l, r;
}q[N];
vector<string> s[N];
int get(int i, int j, int l, int r)
{
    return (a[i][j][r]-1ll*a[i][j][l-1]*c[r-l+1]%mod+mod)%mod;
}
void sol()
{
    srand((int)(time(0)));
    cin >> n >> m;
    for(int i = 0; i < 26; i ++)b[i] = rand() % mod;
    c[0] = 1;
    for(int i = 1; i <= n; i ++)c[i] = 1ll*c[i-1]*base%mod;
    for(int i = 1; i <= n; i ++)
    {
        s[i].resize(m);
        for(int j = 0; j < m; j ++)
        {
            cin >> s[i][j];
            for(int p = 1; p <= i; p ++)
                a[i][j][p] = (1ll*a[i][j][p-1]*base+b[s[i][j][p-1]-'a'])%mod;
        }
    }
    map<int, int> mp;
    for(int j = 0; j < m; j ++)++mp[a[1][j][1]];
    for(int i = 2; i <= n; i ++)
    {
        for(int j = 0; j < m; j ++)
        {
            dp[j] = 0;
            k = a[i][j][i-1];
            if(mp.count(k))add(dp[j], mp[k]);
            k = get(i, j, 2, i);
            if(k == a[i][j][i-1])continue;
            if(mp.count(k))add(dp[j], mp[k]);
        }
        mp.clear();
        for(int j = 0; j < m; j ++)
        {
            k = a[i][j][i];
            mp[k] += dp[j];
            if(mp[k] >= mod)mp[k] -= mod;
        }
        //cout << mp.size() << '\n';
    }
    for(int j = 0; j < m; j ++)add(ans, dp[j]);
    cout << ans;

}
int main()
{
    cin.tie(0);
    cout.tie(0);
    ios_base::sync_with_stdio(0);
    #define task "test"
    if(fopen(task".inp", "r"))
	{
		freopen(task".inp", "r", stdin);
		freopen(task".out", "w", stdout);
	}
    int test = 1;
    //cin >> test;
    while(test -- > 0)sol();
    return 0;
}


Compilation message (stderr)

trener.cpp: In function 'int main()':
trener.cpp:111:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  111 |   freopen(task".inp", "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
trener.cpp:112:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  112 |   freopen(task".out", "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...