#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int n(0),m(0);
cin >> n >> m;
int matrix[n][m];
char arr[n][m];
arr[0][0] = '0';
for(int i = 0;i<n;++i){
for(int j = 0;j<m;++j){
cin >> matrix[i][j];
if(!i && !j)continue;
if(!i){
matrix[i][j] += matrix[i][j-1];
arr[i][j] = 'R';
}
if(!j){
matrix[i][j] += matrix[i-1][j];
arr[i][j] = 'D';
if(i && j){
matrix[i][j] += max(matrix[i-1][j],matrix[i][j-1]);
if(max(matrix[i-1][j],matrix[i][j-1]) == matrix[i-1][j])arr[i][j] = 'D';
else arr[i][j] = 'R';
cout << matrix[n-1][m-1];
cout << "\n";
string s;
for(int i = n-1;;){
for(int j = m-1;;){
if(arr[i][j] == '0'){
reverse(s.begin(),s.end());
s.erase(0,1);
cout << s << endl;
return 0;
s.push_back(arr[i][j]);
s.push_back(' ');
if(arr[i][j] == 'R'){
j -= 1;
continue;
if(arr[i][j] == 'D'){
i -= 1;
cout << endl;
Объяснение:
5591(20), 5623(16), 5639(23), 5641(16), 5647(22), - 4
5651(17), 5653(19), 5657(23), 5659(25), 5669(26), - 1
5683(22), 5689(28), 5693(23), 5701(13), 5711(14), - 3
5717(20), 5737(22), 5741(17), 5743(19), 5749(25), - 2
5779(28), 5783(23), 5791(22), 5801(14), 5807(20), - 4
5813(17), 5821(16), 5827(22), 5839(25), 5843(20), - 3
5849(26), 5851(19), 5857(25), 5861(20), 5867(26), - 3
5869(28), 5879(29), 5881(22), 5897(29), 5903(17), - 2
5923(19), 5927(23), 5939(26), 5953(22), 5981(23), - 2
5987(29), 6007(13), 6011(09), 6029(17), 6037(16), - 1
6043(13), 6047(17), 6053(14), 6067(19), 6073(16), - 2
6079(22), 6089(23), 6091(16), 6101(08), 6113(11), - 3
6121(10), 6131(11), 6133(13), 6143(14), 6151(13), - 2
6163(17), 6173(17), 6197(23), 6199(25), 6203(11), - 0
6211(10), 6217(16), 6221(11), 6229(19), 6247(19), - 2
6257(20), 6263(17), 6269(23), 6271(16), 6277(22), - 3
6287(23), 6299(26), 6301(10), 6311(11), 6317(17), - 2
6323(14), 6329(20), 6337(19), 6343(16), 6353(17), - 3
6359(23), 6361(16), 6367(22), 6373(19), 6379(25), - 2
6389(26), 6397(25), 6421(13), 6427(19), 6449(23), - 1
6451(16), 6469(25), 6473(20), 6481(19), 6491(20), - 3
6521(14), 6529(22), 6547(22), 6551(17), 6553(19), - 3
6563(20), 6569(26), 6571(19), 6577(25), 6581(20), - 3
6599(29), 6607(19), 6619(22), 6637(22), 6653(20). - 3
Всего 120 простых чисел, из них 57 имеют четную сумму цифр.
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int n(0),m(0);
cin >> n >> m;
int matrix[n][m];
char arr[n][m];
arr[0][0] = '0';
for(int i = 0;i<n;++i){
for(int j = 0;j<m;++j){
cin >> matrix[i][j];
if(!i && !j)continue;
if(!i){
matrix[i][j] += matrix[i][j-1];
arr[i][j] = 'R';
}
if(!j){
matrix[i][j] += matrix[i-1][j];
arr[i][j] = 'D';
}
if(i && j){
matrix[i][j] += max(matrix[i-1][j],matrix[i][j-1]);
if(max(matrix[i-1][j],matrix[i][j-1]) == matrix[i-1][j])arr[i][j] = 'D';
else arr[i][j] = 'R';
}
}
}
cout << matrix[n-1][m-1];
cout << "\n";
string s;
for(int i = n-1;;){
for(int j = m-1;;){
if(arr[i][j] == '0'){
reverse(s.begin(),s.end());
s.erase(0,1);
cout << s << endl;
return 0;
}
s.push_back(arr[i][j]);
s.push_back(' ');
if(arr[i][j] == 'R'){
j -= 1;
continue;
}
if(arr[i][j] == 'D'){
i -= 1;
continue;
}
}
}
cout << endl;
return 0;
}
Объяснение: