Hey!
First of all, always use the code sample button to insert your code, like this:
#include <iostream>
using namespace std;
int main() {
int t;
long int n, m, x, y, k, pow;
cin >> t;
while (t > 0) {
cin >> n;
cin >> m;
cin >> x;
cin >> y;
pow = 1;
k = 1;
while (k < n - 1) { k = k + x; }
while (pow < m - 1) { pow = pow + y; }
if (((k == n) && (pow == m)) || ((pow == m - 1) && (k == n - 1))) { cout << "Chefirnemo" << endl; }
else {
cout << "Pofik" << endl;
}
t--;
}
return 0;
}
Secondly, your code isn’t concise. Check this out:
#include <iostream>
using namespace std;
int main() {
int t;
long int n, m, x, y, k, pow;
cin >> t;
while (t--) {
cin >> n >> m >> x >> y;
pow = 1;
k = 1;
while (k < n - 1)
k = k + x;
while (pow < m - 1)
pow = pow + y;
if (((k == n) && (pow == m)) || ((pow == m - 1) && (k == n - 1)))
cout << "Chefirnemo" << endl;
else
cout << "Pofik" << endl;
}
return 0;
}
This is the very same code, but with extra, not required lines removed!
Thirdly, the thing is that you are not wrong with the idea, just that the implementation is wrong!
Yours is a brute force solution, one that basically loops until the required condition is reached.
Consider the input:
1
1000000000 1000000000 1 1
According to your solution, the first while loop will go all over from 1,2,3,… to 10^9 (10^9-1 operations)
Similarly, the second while loop will go all from 1,2,3,… to 10^9 (10^9-1 operations)
Total operations: 2*(10^9)-2
Considering that on CodeChef, 1 sec means 10^9 operations, including all the conditionals and other stuff, this would timeout, since the time limit allocated is just 0.5 sec.
Try to optimise your code, using the % (modulus) function, which checks if the number is divisible or not i.e. a%b==0 iff a is divisible by b.
Also, don’t forget to consider the cases where the numbers get negative/zero.
Hope this helps!
If you face any further issue, check out my solution (try it yourself first ):
Click to view
#include <bits/stdc++.h>
using namespace std;
typedef long long lld;
int main() {
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
lld T;
cin >> T;
while (T--) {
lld N, M, X, Y;
cin >> N >> M >> X >> Y;
N--;
M--;
bool flag = false;
if (N == 0 && M == 0)
flag = true;
else if (N == 1 && M == 1)
flag = true;
else if (N == 0 && M % Y == 0)
flag = true;
else if (N % X == 0 && M == 0)
flag = true;
else if (N % X == 0 && M % Y == 0)
flag = true;
N--;
M--;
if (N >= 0 && M >= 0) {
if (N == 0 && M % Y == 0)
flag = true;
else if (N % X == 0 && M == 0)
flag = true;
else if (N % X == 0 && M % Y == 0)
flag = true;
}
cout << (flag ? "Chefirnemo\n" : "Pofik\n");
}
return 0;
}