Algorithm
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <iostream>
#include <fstream>
#include <sstream>
bool firstWins(long n, long m, long k){
long min = (m < n) ? m : n;
if(min % (k + 1) == 0){return true;}
else if(k == 1 || (min / (k + 1)) % 2 == 0){return (m % 2) != (n % 2);}
else{return (m % 2) == (n % 2);}
}
int main(){
std::ifstream instream; instream.open("input.txt");
FILE * outputFile = fopen("output.txt","w");
long t, k; instream >> t >> k;
while(t--){
long n, m; instream >> n >> m;
fputs(firstWins(n, m, k) ? "+\n" : "-\n", outputFile);
}
instream.close();
fclose(outputFile);
return 0;
}
Copy The Code &
Try With Live Editor
Input
10 2
1 1
1 2
2 1
2 2
1 3
2 3
3 1
3 2
3 3
4 3
1 1
1 2
2 1
2 2
1 3
2 3
3 1
3 2
3 3
4 3
Output
-
+
+
-
-
+
-
+
+
+
+
+
-
-
+
-
+
+
+
Demonstration
Codeforces Solution-D. New Game with a Chess Piece-Solution in C, C++, Java, Python