ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 잉카달력
    백준알고리즘 2019. 7. 21. 19:41
    #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;
    }

    조금 건너뛰어가면서 해본결과

     

     

     

     

     

    '백준알고리즘' 카테고리의 다른 글

    로또  (0) 2019.07.23
    일곱난쟁이  (0) 2019.07.23
    리모컨  (0) 2019.07.21
    퇴사  (0) 2019.07.21
    테트로미노  (0) 2019.07.21

    댓글

Designed by Tistory.