-
#include<cstdio> #include<iostream> #include<utility> using namespace std; typedef pair<int, int> PAIR; int n; PAIR ending; PAIR now; int main(){ scanf("%d", &n); for(int i = 0; i < n; i++){ int x, y; cin>>x>>y; ending.first = x; ending.second = y; int xx,yy; cin>>xx>>yy; now.first = xx; now.second = yy; PAIR comb = make_pair(1,1); int answer = 1; if(now.first == 1 && now.second == 1) answer = 1; else { while (true) { if (comb.first == ending.first && comb.second == ending.second) { answer = -1; break; } comb.second += 1; comb.first += 1; answer += 1; if (comb.second > ending.second) comb.second = 1; if (comb.first > ending.first) comb.first = 1; if (comb.first == now.first && comb.second == now.second) break; } } cout<<answer<<endl; } return 0; }
pair써서 막 여러개해봤는데, 결과는 경우의수가 40000 * 40000 인지라, 너무 커서 시간초과
#include<iostream> #include<utility> using namespace std; typedef pair<int, int> PAIR; int t; int main() { cin >> t; for (int al = 0; al < t; al++) { int m, n, x, y; cin >> m >> n >> x >> y; x--; y--; bool t = false; for (int i = x; i < n * m; i += m) { if (i % n == y) { cout << i + 1 << endl; t = true; break; } } if (t == false) { cout << -1 << endl; } } return 0; }
조금 건너뛰어가면서 해본결과