QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#20976#2476. Pizzo CollectorsltunjicWA 3ms3684kbC++112.1kb2022-02-23 02:21:212022-05-03 12:10:21

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-05-03 12:10:21]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:3684kb
  • [2022-02-23 02:21:21]
  • 提交

answer

#include <bits/stdc++.h> 

#define X first
#define Y second
#define ll long long
#define pii pair<int, int>
#define pb push_back
#define vec vector
#define siz size()
#define pri(i, poc, n, pov) for(int i = (int) poc; i < (int) n; i += (int) pov)
#define od(i, poc, n, pov) for(int i = (int) poc; i > (int) n; i -= (int) pov)

using namespace std;

const ll INF = 1e18;                              
const int LOG = 20;
const int OFF = (1 << LOG);
const int MOD = 1e9 + 7;
const int lx[8] = {1, -1, 0, 0, -1, 1, 1, -1};
const int ly[8] = {0, 0, 1, -1, -1, 1, -1, 1};
const int N = 1e5 + 10;

int abs(int x){ 
	if(x < 0)
		return -x;
	return x;
}

ll n, p;
ll val[30], mx; 
ll prec[OFF];
string s;

int findp(int x){ 
	pri(i, 2, x + 1, 2){ 
		int y = x;
		while((y % i) == 0){ 
			y /= i;
		}
		if(y != 1)
			continue;
		return i;
	}
	return 1;
}

ll rek(int x, int d){ 
	if(d > n || d == 0) return 0;
	
	ll ret = 0;
	ll ret2 = 0;
	
	bool cat = true;
	bool qm = (s[x] == '?');
	bool have = qm;
	char imam = '-';

	if(!qm)
		imam = s[x];

	ll y = x;
	while(((y + d) % n) != x){
		y = (y + d) % n;

		if(qm && imam == '-' && s[y] != '?'){ 
			imam = s[y]; 
		}

		if((s[y] != imam) && s[y] != '?'){
			cat = false;
			break;
		}
		if(s[y] != '?'){
			qm = false;
		} else { 
			have = true;
		}
	}

	y = x;
	for(int i = 0; i < p; i++){ 
		ret2 += rek(y, d * p);
		y = (y + d) % n;
	}

	ret = val[imam - 'A'];
	if(qm)
		ret = mx;
	ret *= prec[d]; 

	if(cat)
		return max(ret, ret2);
	return ret2;
}

int main(){ 
	ios_base::sync_with_stdio(false); 
	cin.tie(0);
	cout.tie(0);
	setprecision(9);

	int spec;
	cin >> n >> s >> spec;

	pri(i, 0, spec, 1){ 
		char c;
		ll w;
		cin >> c >> w;
		val[c - 'A'] = w;
		mx = max(mx, w);
	}

	p = findp(n);
	
	if(p == 1){
		if(s[0] == '?')
			cout << mx << "\n";
		else 
			cout << val[s[0] - 'A'] << "\n";
		return 0;
	}

	prec[n] = 1; 
	for(int i = n / p; i > 1; i /= p){ 
		prec[i] = n / i + prec[i * p] * p; 
	}
	prec[1] = n + prec[p] * p;	

	cout << rek(0, 1) << "\n";
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 3604kb

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: 2ms
memory: 3604kb

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: 3ms
memory: 3684kb

input:

2
AB
2
A 1
B 2

output:

3

result:

ok single line: '3'

Test #4:

score: -100
Wrong Answer
time: 1ms
memory: 3608kb

input:

7
???????
3
A 1
B 3
C 2

output:

3

result:

wrong answer 1st lines differ - expected: '42', found: '3'