Answer the question
In order to leave comments, you need to log in
What is my mistake in the task?
Task
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
int tt;
cin >> tt;
while (tt--) {
int n;
char c;
string s;
cin >> n >> c >> s;
string b = s;
sort(b.begin(), b.end());
if (b[0] == b[b.size() - 1]) {
cout << "0" << '\n';
continue;
}
vector<bool>prime(n + 1, true);
prime[0] = prime[1] = false;
for (int i = 2; i * i <= n; i++) {
if (prime[i]) {
for (int j = i * i; j <= n; j += i)
prime[j] = false;
}
}
int ans = 0;
for (int i = n; i >= 0; i--) {
if (prime[i]) {
ans = i;
break;
}
}
if (s[ans - 1] == c) {
cout << "1" << '\n' << ans << '\n';
}
else {
cout << "2" << '\n';
if (ans < n)
cout << ans << " " << ans + 1 << '\n';
else
cout << ans - 1 << " " << ans << '\n';
}
}
return 0;
}
Answer the question
In order to leave comments, you need to log in
You have the right idea that two numbers in the answer is always enough. You can always give, for example, n, n-1 and these two operations will cover all positions from 1 to n.
But here is the case when one number is enough, you misunderstood. Why are you looking for the largest prime number? What's the point of looking at what's in position ans-1 is c?
Also, your code outputs 0 incorrectly sometimes.
Maybe you misunderstood the problem? You need to make sure that all positions in which characters other than c are not divisible by at least one number in your answer.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question