Round G - D (Simple Polygon)


#include <bits/stdc++.h>
using namespace std;

int main () {
    int T;
    scanf("%d", &T);
    
    for(int tc = 1; tc <= T; tc++) {
		int N, A;
		scanf("%d %d", &N, &A);
		if(A < N-2) {
			printf("Case #%d: IMPOSSIBLE\n", tc);
			continue;
		}
		printf("Case #%d: POSSIBLE\n", tc);
		if(N%4 == 0) {
			vector<pair<long long, long long>> u;
			vector<pair<long long, long long>> d;
			
			for(int i = 0; i < (N/2); i++) {
				if(i%2 == 0) {
					u.push_back(make_pair(i,1));
					d.push_back(make_pair(i,0));
				}
				else {
					u.push_back(make_pair(i,2));
					d.push_back(make_pair(i,1));
				}
			}
			u[N/2-1].second += A-N+2;
			for(int i = 0; i < (N/2); i++)
				printf("%lld %lld\n", d[i].first, d[i].second);
			for(int i = 0; i < (N/2); i++)
				printf("%lld %lld\n", u[(N/2)-1-i].first, u[(N/2)-1-i].second);
		}		
		else if(N%4 == 2) {
			vector<pair<long long, long long>> u;
			vector<pair<long long, long long>> d;
			
			for(int i = 0; i < (N/2); i++) {
				if(i%2 == 0) {
					u.push_back(make_pair(i,1000000000-1));
					d.push_back(make_pair(i,1000000000-2));
				}
				else {
					u.push_back(make_pair(i,1000000000));
					d.push_back(make_pair(i,1000000000-1));
				}
			}
			d[N/2-1].second -= A-N+2;
			for(int i = 0; i < (N/2); i++)
				printf("%lld %lld\n", d[i].first, d[i].second);
			for(int i = 0; i < (N/2); i++)
				printf("%lld %lld\n", u[(N/2)-1-i].first, u[(N/2)-1-i].second);
		}
		else {
			vector<pair<long long, long long>> u;
			vector<pair<long long, long long>> d;
			pair<long long, long long> l = make_pair(A+1-(N-1)/2,1);
			
			for(int i = 0; i < ((N-1)/2); i++) {
				if(i%2 == 0) {
					u.push_back(make_pair(i,1));
					d.push_back(make_pair(i,0));
				}
				else {
					u.push_back(make_pair(i,2));
					d.push_back(make_pair(i,1));
				}
			}
			for(int i = 0; i < ((N-1)/2); i++)
				printf("%lld %lld\n", d[i].first, d[i].second);
			printf("%lld %lld\n", l.first, l.second);
			for(int i = (N-3)/2; i >= 0; i--)
				printf("%lld %lld\n", u[i].first, u[i].second);
		}
    }
}