Răspuns :
Răspuns:
Explicație:
#include <fstream>
using namespace std;
ifstream f("bucati.in");
ofstream g("bucati.out");
int n;
short c, size;
bool prime(int b){
int d = 2;
while(d <= b / 2){
if(b % d == 0)
return 0;
d++;
}
if(b > 1)
return 1;
else
return 0;
}
int main(){
f >> c >> n;
f.close();
short cif[12];
for(int i = 0; n; i++){
cif[i] = n % 10;
n /= 10;
size = i;
}
size++;
for(int i = 0; i < size / 2; i++)
swap(cif[i], cif[size - 1 - i]);
if(c == 1){
int cCost, aux, maxCost = 0;
for(int i = 0; i < size; i++){
int j = i;
aux = 0;
for(int k = 0; k < size - 2; k++){
aux = aux * 10 + cif[j];
j++;
if(j == size)
j = 0;
}
cCost = aux + cif[j] + cif[((j + 1) == size ? 0 : (j + 1))];
maxCost = max(maxCost, cCost);
}
g << maxCost;
}else{
int s = 0, minCost = 1000000001;
for(int i = 0; i < size; i++)
s += cif[i];
for(int i = 0; i < size; i++){
s += cif[i] * 9;
if(prime(s))
minCost = min(minCost, s);
s -= cif[i] * 9;
}
for(int i = 0; i < size; i++){
s += cif[i] * 99 + cif[((i + 1) == size ? 0 : (i + 1))] * 9;
if(prime(s))
minCost = min(minCost, s);
s -= cif[i] * 99 + cif[((i + 1) == size ? 0 : (i + 1))] * 9;
}
g << minCost;
}
g.close();
}