QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#344478 | #2476. Pizzo Collectors | PetroTarnavskyi# | RE | 0ms | 3820kb | C++20 | 1.9kb | 2024-03-04 17:12:11 | 2024-03-04 17:12:11 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define RFOR(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define SZ(a) int(a.size())
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair
#define F first
#define S second
typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef double db;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
string s;
cin >> s;
map<char, int> value;
int k;
cin >> k;
char mx = '0';
FOR(i, 0, k)
{
char x;
cin >> x;
int val;
cin >> val;
value[x] = val;
if(mx == '0' || val > value[mx])
mx = x;
}
int m = n;
int p = -1;
for(int pr = 2; pr * pr <= n; pr++)
{
if(n % pr)
continue;
p = pr;
n = 1;
break;
}
if(n != 1)
p = n;
assert(p != -1);
n = m;
int beta = 0;
int pw = 1;
while(pw < n)
{
beta++;
pw *= p;
}
vector<LL> dp(n);
RFOR(alpha, beta + 1, 0)
{
vector<LL> ndp(pw);
FOR(ost, 0, pw)
{
VI cnt(26, 0);
int pos = ost;
do{
if(s[pos] != '?')
cnt[s[pos] - 'A']++;
pos += pw;
if(pos >= n)
pos -= n;
}while(pos != ost);
LL cur = 0;
int num = 0;
FOR(i, 0, 26)
{
if(cnt[i] != 0)
{
cur = value[char('A' + i)];
num++;
}
}
if(num == 0)
cur = value[mx];
if(num >= 2)
cur = 0;
//ndp[ost] = cur * (pwU - 1) / (p - 1);
ndp[ost] = cur * (n / pw) * (beta + 1 - alpha);
//cout << pw << " " << ost << " " << ndp[ost] << "\n";
if(alpha != beta)
{
LL sum = 0;
FOR(i, 0, p)
{
sum += dp[pw * i + ost];
}
ndp[ost] = max(ndp[ost], sum);
}
}
dp = ndp;
pw /= p;
}
cout << dp[0] << "\n";
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3820kb
input:
8 ?A?B?A?C 3 A 1 B 1000 C 100000
output:
1301004
result:
ok single line: '1301004'
Test #2:
score: 0
Accepted
time: 0ms
memory: 3632kb
input:
4 ABCD 4 A 4 B 3 C 2 D 1
output:
10
result:
ok single line: '10'
Test #3:
score: 0
Accepted
time: 0ms
memory: 3528kb
input:
2 AB 2 A 1 B 2
output:
3
result:
ok single line: '3'
Test #4:
score: 0
Accepted
time: 0ms
memory: 3548kb
input:
7 ??????? 3 A 1 B 3 C 2
output:
42
result:
ok single line: '42'
Test #5:
score: -100
Runtime Error
input:
1 ? 26 A 1 B 2 C 3 D 4 E 5 F 6 G 7 H 8 I 9 J 10 K 11 L 12 M 13 N 14 O 15 P 16 Q 17 R 18 S 19 T 20 U 21 V 22 W 23 X 24 Y 25 Z 26