QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#685893 | #526. Magic | Mher777 | 90 | 154ms | 172492kb | C++20 | 5.6kb | 2024-10-28 21:50:59 | 2024-10-28 21:51:01 |
Judging History
answer
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <iomanip>
#include <array>
#include <string>
#include <algorithm>
#include <cmath>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <bitset>
#include <list>
#include <iterator>
#include <numeric>
#include <complex>
#include <utility>
#include <random>
#include <cassert>
#include <fstream>
using namespace std;
mt19937 rnd(time(nullptr));
/* -------------------- Typedefs -------------------- */
typedef int itn;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef float fl;
typedef long double ld;
/* -------------------- Usings -------------------- */
using vi = vector<int>;
using vll = vector<ll>;
using mii = map<int, int>;
using mll = map<ll, ll>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
/* -------------------- Defines -------------------- */
#define ff first
#define ss second
#define pub push_back
#define pob pop_back
#define puf push_front
#define pof pop_front
#define mpr make_pair
#define yes cout<<"Yes\n"
#define no cout<<"No\n"
#define all(x) (x).begin(), (x).end()
#define USACO freopen("feast.in", "r", stdin); freopen("feast.out", "w", stdout);
/* -------------------- Constants -------------------- */
const int dx[8] = { -1, 0, 1, 0, -1, -1, 1, 1 };
const int dy[8] = { 0, -1, 0, 1, -1, 1, -1, 1 };
const int MAX = int(1e9 + 5);
const ll MAXL = ll(1e18) + 5ll;
const ll MOD = ll(1000000007);
const ll MOD2 = ll(998244353);
/* -------------------- Functions -------------------- */
void fastio() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
}
void precision(int x) {
cout.setf(ios::fixed | ios::showpoint);
cout.precision(x);
}
ll gcd(ll a, ll b) {
if (a == 0 || b == 0) return(max(a, b));
while (b) {
a %= b;
swap(a, b);
}
return a;
}
ll lcm(ll a, ll b) {
return a / gcd(a, b) * b;
}
ll range_sum(ll a, ll b) {
if (a > b) return 0ll;
ll dif = a - 1, cnt = b - a + 1;
ll ans = ((b - a + 1) * (b - a + 2)) / 2;
ans += ((b - a + 1) * dif);
return ans;
}
string dec_to_bin(ll a) {
string s = "";
for (ll i = a; i > 0; ) {
ll k = i % 2;
i /= 2;
char c = k + 48;
s += c;
}
if (a == 0) {
s = "0";
}
reverse(all(s));
return s;
}
ll bin_to_dec(string s) {
ll num = 0;
for (int i = 0; i < s.size(); i++) {
num *= 2ll;
num += (s[i] - '0');
}
return num;
}
ll factorial_by_mod(ll n, ll mod) {
ll ans = 1;
ll num;
for (ll i = 1; i <= n; ++i) {
num = i % mod;
ans *= num;
ans %= mod;
}
return ans;
}
bool isPrime(ll a) {
if (a == 1) return false;
for (ll i = 2; i * i <= a; i++) {
if (a % i == 0) return false;
}
return true;
}
ll binpow(ll a, ll b) {
if (!a) return 0;
ll ans = 1;
while (b) {
if (b & 1) {
ans *= a;
}
b >>= 1;
a *= a;
}
return ans;
}
ll binpow_by_mod(ll a, ll b, ll mod) {
if (!a) return 0;
ll ans = 1;
while (b) {
if (b & 1) {
ans *= a;
ans %= mod;
}
b >>= 1;
a *= a;
a %= mod;
}
return ans;
}
/* -------------------- Solution -------------------- */
const int N = 100005, M = 55;
ll dp[N], p_pow[N], nxt[M][N];
vi a[M][M];
int m;
void slv() {
int n;
string s;
cin >> n >> s;
map<char, int> mp;
vector<char> v;
for (int i = 0; i < n; ++i) {
if (mp[s[i]]) continue;
mp[s[i]] = ++m;
v.pub(s[i]);
}
for (int i = 0; i < m; ++i) {
int ind1 = mp[v[i]];
for (int j = 0; j < m; ++j) {
if (i == j) continue;
int ind2 = mp[v[j]];
a[ind1][ind2].assign(n + 1, 0);
for (int ind = 1; ind <= n; ++ind) {
a[ind1][ind2][ind] = a[ind1][ind2][ind - 1];
if (s[ind - 1] == v[i]) {
++a[ind1][ind2][ind];
}
else if (s[ind - 1] == v[j]) {
--a[ind1][ind2][ind];
}
}
}
}
ll p = 31;
p_pow[0] = 1ll;
for (int i = 1; i <= m; ++i) {
p_pow[i] = (p_pow[i - 1] * p) % MOD;
}
for (int i = 1; i <= m; ++i) {
vll v(n + 1, 0);
int cur = 0;
for (int ind = 0; ind <= n; ++ind) {
cur = 0;
for (int j = 1; j <= m; ++j) {
if (i == j) continue;
v[ind] += a[i][j][ind] * p_pow[cur];
if (v[ind] > MOD) v[ind] -= MOD;
++cur;
}
}
unordered_map<ll, int> last;
for (int ind = n; ind >= 0; --ind) {
nxt[i][ind] = last[v[ind]];
last[v[ind]] = ind;
}
}
ll ans = 0;
for (int i = n; i >= 1; --i) {
int ind = mp[s[i - 1]];
int r = nxt[ind][i - 1];
if (!r) continue;
dp[i] = dp[r + 1] + 1ll;
if (dp[i] > MOD) dp[i] -= MOD;
ans += dp[i];
if (ans > MOD) ans -= MOD;
}
cout << ans;
}
void cs() {
int tstc = 1;
//cin >> tstc;
while (tstc--) {
slv();
}
}
void precalc() {
return;
}
int main() {
fastio();
precalc();
//precision(0);
cs();
return 0;
}
詳細信息
Pretests
Final Tests
Test #1:
score: 5
Accepted
time: 0ms
memory: 3708kb
input:
100 SsSsSsSsSsSSssSSsSSssSsSsssssSsSssssSsSSSSsSSSSsSsSssSSSSsSSsssSssSssssSsssSssSsSssssssSSssSSsSSSsSS
output:
440
result:
ok single line: '440'
Test #2:
score: 5
Accepted
time: 0ms
memory: 45716kb
input:
100 XUHzGCBgfsEvaJiOlmjuNwYTcpkPynZtVASQqhMDoeFRKbdLIrgOGBUlECmJHzisvfXaQecMypSjPqRtTDowkZnFNYVAuhUrXosy
output:
3
result:
ok single line: '3'
Test #3:
score: 5
Accepted
time: 1ms
memory: 7956kb
input:
200 xtIxtIxtIxtIxxttxIxxxtIItxIIIxxItItxtIItxIxxtIItxIxIIxItttItxxtItxItxItxItxxIIIxtxtIxxtIIttIItIxIItIxtIxtIxtIxtIxttttxIttxtxIxxtIIxtxIIxIIxxIIttIIIxtxIttxItxxxxItxItxItxItxItxItxtxxtIxttIxIIIItxtIxtIx
output:
402
result:
ok single line: '402'
Test #4:
score: 5
Accepted
time: 4ms
memory: 32096kb
input:
500 mhUMlYtJLIiOkfsBSTodgrDQZWbCnvlJLMhmYItUivdnMgDmUCblrfhTsWoQkOSZBYtDrvbniIhUMmdfLsJgTlCvJLSQlnLZMiDIfQYZSgkTJsbWoOodrBOWCkBtnnmYlDLhTmMSbilsJIUvtUQhdgrvfCLYvkWCYhodtfBgsmJObiDroMkZZQtOSTIIWUBiQbLrDhsgWklCtfoCvMlSdUnmJdhLnTvYYWfUZDbSJgIkZOJminfrZsiBMbODgMoQsQTIlSToLlLmDYtvSLiBMdbBgOmUUJTQdfkvlCWn...
output:
19
result:
ok single line: '19'
Test #5:
score: 5
Accepted
time: 0ms
memory: 10224kb
input:
1000 yzdBsSvHpkyzdBsSvHpkyzdBsSvHpkyzdBsSvHpkyzdBsSvHpkyzdBsSvHpkyzdBsSvHpkyzdBsSvHpkyzdBsSvHpkyzdBsSvHpkpkkkHsHSBsdsvkyyzzSBykpHzskdpvSyBHszkdpvSyBHszkdpvSyBHszkdpvSyBHszkdpvSyBHszkdpvSyBHszpsBpyzdsyHkpyvzzzszkpyyBzySHzkSdvyBspHzkSdvyBspHzkSdvyBspHzkSdvyBspHzkSdvyBspHvpkkSsszddykHzkpzBpBkdHSvszypBk...
output:
3103
result:
ok single line: '3103'
Test #6:
score: 5
Accepted
time: 21ms
memory: 63940kb
input:
2000 MyWnObAVvNzcYqECPmTtJXpwjeIkLixhHKlRguQBsDrdSGaUFZWbnMyAOvVunpeHyhNYczPmCFjLKsEwROxMUWSJtrlgIkqQdTiBGDXaZrjClWIEVxwdibQLntNMKPmvqOYFUzuTepsghJkHASRcyDaLVdiWjMCbrEQNXBnxZGKwlItBHhAFcRpuzzEvGbyNZYvgqVTsTmeqAPkmXDacSYUCJPOZRJinjuxSHsUhraXgBDMFplQtWIwkdeGLyOKVFnbPzchHNYepuyCvAmCLDkZcZkidejaqvumJbVL...
output:
56
result:
ok single line: '56'
Test #7:
score: 5
Accepted
time: 0ms
memory: 6048kb
input:
2000 WRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRRRWWWRRWWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRWRW...
output:
207783
result:
ok single line: '207783'
Test #8:
score: 5
Accepted
time: 0ms
memory: 7904kb
input:
5000 BABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABAB...
output:
666340
result:
ok single line: '666340'
Test #9:
score: 5
Accepted
time: 2ms
memory: 8192kb
input:
20000 umumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumumum...
output:
19154645
result:
ok single line: '19154645'
Test #10:
score: 5
Accepted
time: 0ms
memory: 8708kb
input:
50000 mpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmpmp...
output:
86508692
result:
ok single line: '86508692'
Test #11:
score: 5
Accepted
time: 5ms
memory: 8756kb
input:
100000 ElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElE...
output:
513536001
result:
ok single line: '513536001'
Test #12:
score: 5
Accepted
time: 6ms
memory: 10224kb
input:
100000 ElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElE...
output:
499749995
result:
ok single line: '499749995'
Test #13:
score: 5
Accepted
time: 0ms
memory: 9980kb
input:
100000 ElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElElE...
output:
499749995
result:
ok single line: '499749995'
Test #14:
score: 5
Accepted
time: 154ms
memory: 140948kb
input:
10000 IrUwTbhZexXAplmaVfqMOBigvjGHoCQJsYNFDLcnRdyESkPutzbiHhVwAsvZOgqMITmClxDQJpojfFeBaNGUYXrIcRXxSPkETAewrpZnbdyzLutUhvHsVxcCMFlNBoigJQOYDjGImafqXRjdeJrtChDgEdlyUhkvuaENsfLPQSoSAiZBOGZrPRcwYLqkzzTUImMnwHyutnVTbFbpvYymCpaaetgENRdXJgXuoxvNUDDxOVcjELfQGJfihHlqFCsljkIdshMeABrIHiTOgoQsRYVOxZqLoDpFTfSAYm...
output:
100
result:
ok single line: '100'
Test #15:
score: 5
Accepted
time: 0ms
memory: 12816kb
input:
50000 RxNHRxNHRxNHRxNHRxNHRxNHRxNHRxNHRxNHRxNHRxNHRxNHRxNHRxNHRxNHRxNHRxNHRxNHRxNHRxNHRxNHRxNHRxNHRxNHRxNHRxNHRxNHRxNHRxNHRxNHRxNHRxNHRxNHRxNHRxNHRxNHRxNHRxNHRxNHRxNHRxNHRxNHRxNHRxNHRxNHRxNHRxNHRxNHRxNHRxNHRxNHRxNHRxNHRxNHRxNHRxNHRxNHRxNHRxNHRxNHRxNHRxNHRxNHRxNHRxNHRxNHRxNHRxNHRxNHRxNHRxNHRxNHRxNHRx...
output:
25510755
result:
ok single line: '25510755'
Test #16:
score: 5
Accepted
time: 27ms
memory: 50736kb
input:
100000 dFExBWcrYPdFExBWcrYPdFExBWcrYPdFExBWcrYPdFExBWcrYPdFExBWcrYPdFExBWcrYPdFExBWcrYPdFExBWcrYPdFExBWcrYPdFExBWcrYPdFExBWcrYPdFExBWcrYPdFExBWcrYPdFExBWcrYPdFExBWcrYPdFExBWcrYPdFExBWcrYPdFExBWcrYPdFExBWcrYPdFExBWcrYPdFExBWcrYPdFExBWcrYPdFExBWcrYPdFExBWcrYPdFExBWcrYPdFExBWcrYPdFExBWcrYPdFExBWcrYPdFE...
output:
39836830
result:
ok single line: '39836830'
Test #17:
score: 0
Time Limit Exceeded
input:
100000 mXYwpGNxavDJQIysKAeZibMOrkfWdlhcFuHnLTESCjUPqztoBgVRpYNXmwGxQDsJIeAyKavLpWTdjURhuEYbtngiHlfZCqBozFOrkVNMScPgKrEQDltaHdmvUFkXuZeILoWOszMiASTBnbjPchxyqfGwJCRXDapvwxQyYImJVGNYhMbNkdZjPgpuUmBKtfienWHwrOSACFEcqzoTXVLslRisvxbKQaDIeZAGyOJMmCTWzhDRwpLPBVdqJsGXEtUuljxrkFfSYHNcnogQKIayvdbWMlAZerOfkiUuE...
output:
result:
Test #18:
score: 0
Time Limit Exceeded
input:
100000 QeuiySpTEKbUdvBrjgIznkLhXRVoDwJAYGPONWfMFqlZHmCacstxwBzfWNMukAUDvrTEiXjbnYQGpPoVIKgeLRdhyJOSNczmZMwftxlHBCasFWqUdLAlpbcuTokyxturpIKEeiYbqSTXaORQZeDyimvQUsdhnCGJKvSPjEVHgFrBODGgWXNNhcMYoLPjIVJkAwRfznmmROpuTdyKYMETUtlbSdqvIBCCJDusqnUzZwhZbtecxQkapeLriHWlAoxafXsGyFQiMBXzOHVtJVNScnkflNgHLBPhGAuqo...
output:
result:
Test #19:
score: 5
Accepted
time: 92ms
memory: 101724kb
input:
100000 gruAhpZWdVkONqQgruAhpZWdVkONqQgruAhpZWdVkONqQgruAhpZWdVkONqQgruAhpZWdVkONqQgruAhpZWdVkONqQgruAhpZWdVkONqQgruAhpZWdVkONqQgruAhpZWdVkONqQgruAhpZWdVkONqQgruAhpZWdVkONqQgruAhpZWdVkONqQgruAhpZWdVkONqQgruAhpZWdVkONqQgruAhpZWdVkONqQgruAhpZWdVkONqQgruAhpZWdVkONqQgruAhpZWdVkONqQgruAhpZWdVkONqQgruAhpZW...
output:
27619254
result:
ok single line: '27619254'
Test #20:
score: 5
Accepted
time: 131ms
memory: 172492kb
input:
100000 MvNUleIZJnpgutjQDXzoMvNUleIZJnpgutjQDXzoMvNUleIZJnpgutjQDXzoMvNUleIZJnpgutjQDXzoMvNUleIZJnpgutjQDXzoMvNUleIZJnpgutjQDXzoMvNUleIZJnpgutjQDXzoMvNUleIZJnpgutjQDXzoMvNUleIZJnpgutjQDXzoMvNUleIZJnpgutjQDXzoMvNUleIZJnpgutjQDXzoMvNUleIZJnpgutjQDXzoMvNUleIZJnpgutjQDXzoMvNUleIZJnpgutjQDXzoMvNUleIZJnpgu...
output:
21636849
result:
ok single line: '21636849'