QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#273868 | #7876. Cyclic Substrings | ucup-team992# | AC ✓ | 1778ms | 722844kb | C++17 | 4.5kb | 2023-12-03 04:54:03 | 2023-12-03 04:54:04 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define ll long long
typedef int uci;
#define int long long
#define F first
#define S second
#define ar array
string s;
const int MOD = 1e9+7;
int fhash[9000001];
int bhash[9000001];
int inv[9000001];
int pows[9000001];
seed_seq seq{
(uint64_t) chrono::duration_cast<chrono::nanoseconds>(chrono::high_resolution_clock::now().time_since_epoch()).count(),
(uint64_t) __builtin_ia32_rdtsc(),
(uint64_t) (uintptr_t) make_unique<char>().get()
};
mt19937 rng(seq);
int p;
int exp(int n, int pe){
int out{1};
while(pe){
if(pe&1){
out *= n;
out %= MOD;
}
pe >>= 1;
n *= n;
n %= MOD;
}
return out;
}
int ms;
void init(){
int run{};
int pow = 1;
inv[0] = 1;
inv[1] = exp(p, MOD-2);
pows[0] = 1;
pows[1] = p;
for(int i{}; i < s.size(); ++i){
if(i >= 2){
inv[i] = (inv[i-1]*inv[1])%MOD;
pows[i] = (pows[i-1]*p)%MOD;
}
run += (pows[i]*(s[i]-'0'+1))%MOD;
run %= MOD;
fhash[i] = run;
}
run = 0;
for(int i = s.size()-1; i >= 0; --i){
run += (pows[ms-1-i]*(s[i]-'0'+1))%MOD;
run %= MOD;
bhash[i] = run;
}
}
bool eq(int i, int j){
int fh = ((fhash[j] - (i == 0 ? 0 : fhash[i-1]))*inv[i])%MOD;
fh %= MOD;
if(fh < 0)
fh += MOD;
int bh = ((bhash[i] - (j == ms-1 ? 0 : bhash[j+1]))*inv[ms-1-j])%MOD;
bh %= MOD;
if(bh < 0)
bh += MOD;
return fh == bh;
}
int gethash(int i, int j){
int fh = ((fhash[j] - (i == 0 ? 0 : fhash[i-1]))*inv[i])%MOD;
fh %= MOD;
if(fh < 0)
fh += MOD;
return fh;
}
template <class K, class V>
using ht = gp_hash_table<
K, V, hash<K>, equal_to<K>, direct_mask_range_hashing<>, linear_probe_fn<>,
hash_standard_resize_policy<hash_exponential_size_policy<>,
hash_load_check_resize_trigger<>, true>>;
int pcounts[9000001];
int things[9000001];
int isp[9000001];
vector<ar<int, 3>> plengths[3000001];
struct node{
node *ne[10];
int ind;
node(){
for(int i{}; i < 10; ++i)
ne[i] = nullptr;
ind = -1;
}
};
int add(int i, int pl, node *root){
if(i == 0){
if(root->ind != -1)
return root->ind;
root->ind = pl;
return root->ind;
}else{
if(!root->ne[i%10]){
root->ne[i%10] = new node();
}
return add(i/10, pl, root->ne[i%10]);
}
}
void solve(){
p = uniform_int_distribution<int>(11, MOD-2)(rng);
int n;
cin >> n;
cin >> s;
string t = s;
s += t;
s += t;
ms = s.size();
init();
int ind{};
for(int i = n; i < 2*n; ++i){
int l = 1, r = (n-1)/2;
int longest = 0;
while(l <= r){
int mid = l + (r-l)/2;
if(eq(i-mid, i+mid)){
longest = mid;
l = mid+1;
}else{
r = mid-1;
}
}
int hh = gethash(i-longest, i+longest);
int le = 1+2*longest;
plengths[le].push_back({hh, 1, i});
l = 1, r = n/2;
longest = 0;
while(l <= r){
int mid = l + (r-l)/2;
if(eq(i-mid+1, i+mid)){
longest = mid;
l = mid+1;
}else
r = mid-1;
}
if(longest != 0){
hh = gethash(i-longest+1, i+longest);
le = 2*longest;
plengths[le].push_back({hh, 1, i});
}
}
int out{};
int mm = 998244353;
for(int i = n; i >= 1; --i){
sort(plengths[i].begin(), plengths[i].end());
for(int j{}; j < plengths[i].size(); ){
int k = j;
int amt{};
while(k < plengths[i].size() && plengths[i][k][0] == plengths[i][j][0]){
amt += plengths[i][k][1];
++k;
}
out += ((amt)*(amt)%mm * i)%mm;
out %= mm;
if(i > 2){
int rep = plengths[i][j][2];
int hh;
if(i%2 == 0){
hh = gethash(rep-(i-2)/2+1, rep+(i-2)/2);
}else{
hh = gethash(rep-(i-2)/2, rep+(i-2)/2);
}
plengths[i-2].push_back({hh, amt, rep});
}
j = k;
}
}
cout << out << '\n';
}
uci main(){
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
solve();
}
这程序好像有点Bug,我给组数据试试?
詳細信息
Test #1:
score: 100
Accepted
time: 7ms
memory: 83476kb
input:
5 01010
output:
39
result:
ok 1 number(s): "39"
Test #2:
score: 0
Accepted
time: 7ms
memory: 83968kb
input:
8 66776677
output:
192
result:
ok 1 number(s): "192"
Test #3:
score: 0
Accepted
time: 8ms
memory: 82884kb
input:
1 1
output:
1
result:
ok 1 number(s): "1"
Test #4:
score: 0
Accepted
time: 11ms
memory: 83340kb
input:
2 22
output:
12
result:
ok 1 number(s): "12"
Test #5:
score: 0
Accepted
time: 8ms
memory: 83320kb
input:
2 21
output:
2
result:
ok 1 number(s): "2"
Test #6:
score: 0
Accepted
time: 4ms
memory: 83720kb
input:
3 233
output:
10
result:
ok 1 number(s): "10"
Test #7:
score: 0
Accepted
time: 10ms
memory: 83784kb
input:
3 666
output:
54
result:
ok 1 number(s): "54"
Test #8:
score: 0
Accepted
time: 453ms
memory: 257692kb
input:
1000000 3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333...
output:
496166704
result:
ok 1 number(s): "496166704"
Test #9:
score: 0
Accepted
time: 1422ms
memory: 607472kb
input:
3000000 2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222...
output:
890701718
result:
ok 1 number(s): "890701718"
Test #10:
score: 0
Accepted
time: 1491ms
memory: 685092kb
input:
3000000 9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999...
output:
224009870
result:
ok 1 number(s): "224009870"
Test #11:
score: 0
Accepted
time: 1778ms
memory: 534580kb
input:
3000000 8989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989...
output:
51985943
result:
ok 1 number(s): "51985943"
Test #12:
score: 0
Accepted
time: 1737ms
memory: 559020kb
input:
3000000 1911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911...
output:
355676465
result:
ok 1 number(s): "355676465"
Test #13:
score: 0
Accepted
time: 1412ms
memory: 673836kb
input:
3000000 7777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777...
output:
788510374
result:
ok 1 number(s): "788510374"
Test #14:
score: 0
Accepted
time: 1462ms
memory: 722844kb
input:
3000000 5555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555...
output:
691884476
result:
ok 1 number(s): "691884476"
Test #15:
score: 0
Accepted
time: 1171ms
memory: 559096kb
input:
3000000 0990990909909909099090990990909909909099090990990909909099099090990990909909099099090990990909909099099090990909909909099099090990909909909099090990990909909909099090990990909909909099090990990909909099099090990990909909099099090990990909909099099090990909909909099099090990909909909099090990...
output:
701050848
result:
ok 1 number(s): "701050848"
Test #16:
score: 0
Accepted
time: 1216ms
memory: 490932kb
input:
3000000 2772772727727727277272772772727727727277272772772727727277277272772772727727277277272772772727727277277272772727727727277277272772727727727277272772772727727727277272772772727727727277272772772727727277277272772772727727277277272772772727727277277272772727727727277277272772727727727277272772...
output:
486861605
result:
ok 1 number(s): "486861605"
Test #17:
score: 0
Accepted
time: 1406ms
memory: 580744kb
input:
3000000 4554554545545545455454554554545545545455454554554545545455455454554554545545455455454554554545545455455454554545545545455455454554545545545455454554554545545545455454554554545545545455454554554545545455455454554554545545455455454554554545545455455454554545545545455455454554545545545455454554...
output:
450625621
result:
ok 1 number(s): "450625621"
Test #18:
score: 0
Accepted
time: 1541ms
memory: 574620kb
input:
3000000 1181811811818118181181181811818118118181181181811818118118181181181811818118118181181811811818118118181181811811818118181181181811811818118181181181811811818118181181181811818118118181181181811818118118181181181811818118118181181811811818118118181181811811818118181181181811811818118181181181...
output:
649551870
result:
ok 1 number(s): "649551870"
Extra Test:
score: 0
Extra Test Passed