Problem:
Problem I
  Skinny Polygon
You are asked to find a polygon that satisfies all the following conditions, given two integers, $x_{bb}$ and $y_{bb}$.
The number of vertices is either 3 or 4.
Edges of the polygon do not intersect nor overlap with other edges, i.e., they do not share any points with other edges except for their endpoints.
The $x$- and $y$-coordinates of each vertex are integers.
The $x$-coordinate of each vertex is between 0 and $x_{bb}$, inclusive. Similarly, the $y$-coordinate is between 0 and $y_{bb}$, inclusive.
At least one vertex has its $x$-coordinate 0.
At least one vertex has its $x$-coordinate $x_{bb}$.
At least one vertex has its $y$-coordinate 0.
At least one vertex has its $y$-coordinate $y_{bb}$.
The area of the polygon does not exceed 25000.
The polygon may be non-convex.
Input
The input consists of multiple test cases. The first line of the input contains an integer $n$, which is the number of the test cases ($1 \leq n \leq 10^5$). Each of the following $n$ lines contains a test case formatted as follows.
$x_{bb}$ $y_{bb}$
$x_{bb}$ and $y_{bb}$ ($2 \leq x_{bb} \leq 10^9, 2 \leq y_{bb} \leq 10^9$) are integers stated above.
Output
For each test case, output description of one polygon satisfying the conditions stated above, in the following format.
$v$
$x_1$ $y_1$
.
.
.
$x_v$ $y_v$
Here, $v$ is the number of vertices, and each pair of $x_i$ and $y_i$ gives the coordinates of the $i$-th vertex, $(x_i, y_i)$. The first vertex $(x_1, y_1)$ can be chosen arbitrarily, and the rest should be listed either in clockwise or in counterclockwise order.
When more than one polygon satisfies the conditions, any one of them is acceptable. You can prove that, with the input values ranging as stated above, there is at least one polygon satisfying the conditions.
Sample Input 1
2
5 6
1000000000 2
Sample Output 1
4
5 6
0 6
0 0
5 0
3
1000000000 0
0 2
999999999 0

Submitted solutions:

code_snippet: #include <stdio.h>
#include <bits/stdtr1c++.h>

#define clr(ar) memset(ar, 0, sizeof(ar))
#define read() freopen("lol.txt", "r", stdin)
#define dbg(x) cout << #x << " = " << x << endl
#define ran(a, b) ((((rand() << 15) ^ rand()) % ((b) - (a) + 1)) + (a))

using namespace std;

struct Point{
    long long x, y;
    Point(){}
    Point(long long x, long long y) : x(x), y(y){}
} ar[10];

void print(int n){
    printf("%d\n", n);
    for (int i = 0; i < n; i++) printf("%lld %lld\n", ar[i].x, ar[i].y);
}

long long area(Point poly[], int n){
    long long res = 0;
    for (int i = 0, j = n - 1; i < n; j = i++){
        res += ((poly[j].x + poly[i].x) * (poly[j].y - poly[i].y));
    }
    return abs(res);
}

long long extended_gcd(long long a, long long b, long long& x, long long& y){
    if (!b){
        y = 0, x = 1;
        return a;
    }

    long long g = extended_gcd(b, a % b, y, x);
    y -= ((a / b) * x);
    return g;
}

int main(){
    int t, i, j;
    long long a, x, y, z, g, xmax, ymax;

    scanf("%d", &t);
    while (t--){
        scanf("%lld %lld", &xmax, &ymax);
        g = extended_gcd(xmax, ymax, x, y);
        ar[0] = Point(0, 0), ar[1] = Point(abs(x), abs(y)), ar[2] = Point(xmax, ymax);

        a = area(ar, 3);
        if (a > 0 && a <= 50000 && ar[1].x <= xmax && ar[1].y <= ymax) print(3);
        else{
            ar[0] = Point(0, 0), ar[1] = Point(abs(y), abs(x)), ar[2] = Point(xmax, ymax);
            a = area(ar, 3);
            if (a > 0 && a <= 50000 && ar[1].x <= xmax && ar[1].y <= ymax) print(3);
            else{
                ar[0] = Point(0, 0), ar[1] = Point(xmax - 1, ymax), ar[2] = Point(xmax / g, ymax / g), ar[3] = Point(xmax, ymax - 1);
                a = area(ar, 4);
                assert(a > 0 && a <= 50000);
                print(4);
            }
        }
    }
    return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 3572
final_rank: 6

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

using ll = long long;
using ld = long double;

ll X, Y; 

bool isvalid(ll x, ll y) {
  return 0 <= x && x <= X && 0 <= y && y <= Y;
}

ll surface(array<ll, 2> v1, array<ll, 2> v2) {
  auto [x1, y1] = v1;
  auto [x2, y2] = v2;
  return abs(x1 * y2 - x2 * y1);
}

// ax + by = gcd(a, b) を満たす、(x, y) を求める。返り値は
// gcd(a, b) x, y の最小性は保証しない
long long ext_gcd(long long a, long long b, long long &x, long long &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }

  long long g = ext_gcd(b, a % b, y, x);
  y -= a / b * x;

  return g;
}

const ll S = 50000;

void solve() {
  cin >> X >> Y;
  bool isrev = false;
  if (X > Y) swap(X, Y), isrev = true;
  // X <= Y
  
  vector< array<ll, 2> > res;
  ll s = 1e17;
  
  {
    ll x, y;
    ll g = ext_gcd(Y, X, x, y); // Yx + Xy = g
    
    array<ll, 2> M = {X / g, Y / g};
    array<ll, 2> O = {0, 0}, R = {X, Y-1}, U = {X-1, Y};
    ll t = surface(R, M) + surface(M, U);
    if (t < s) s = t, res = {O, R, M, U};
    
    y *= (-1); // Yx - Xy = g
    x += X * 10000, y += Y * 10000;
    // cout << -1 << " " << x << " " << y << endl;
    ll mul = min(x / (X / g), y / (Y / g));
    x -= (X / g) * mul, y -= (Y / g) * mul;
    
    R = {X, Y}, M = {x, y};
    t = surface(R, M);
    if (t < s) s = t, res = {O, R, M};
  }
  
  assert(s <= S);
  
  cout << res.size() << '\n';
  for (auto [x, y] : res) {
    assert(isvalid(x, y));
    if (isrev) swap(x, y);
    cout << x << " " << y << '\n';
  }
  
  return;
}

int main() {
  ll T; cin >> T;
  while (T--) {
    solve();
  }
  
  return 0;
}
accuracy: 1
time_ms: 160
memory_kb: 3432
final_rank: 4

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

using ll = long long;
using ld = long double;

ll X, Y;
bool isrev = false;

bool isvalid(ll x, ll y) {
  return 0 <= x && x <= X && 0 <= y && y <= Y;
}

ll surface(array<ll, 2> v1, array<ll, 2> v2) {
  auto [x1, y1] = v1;
  auto [x2, y2] = v2;
  return abs(x1 * y2 - x2 * y1);
}

// ax + by = gcd(a, b) を満たす、(x, y) を求める。返り値は
// gcd(a, b) x, y の最小性は保証しない
long long ext_gcd(long long a, long long b, long long &x, long long &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }

  long long g = ext_gcd(b, a % b, y, x);
  y -= a / b * x;

  return g;
}

const ll S = 50000;

void solve() {
  cin >> X >> Y;
  if (X > Y) swap(X, Y), isrev = true;
  // X <= Y
  
  vector< array<ll, 2> > res;
  ll s = 1e17;
  
  {
    ll x, y;
    ll g = ext_gcd(Y, X, x, y); // Yx + Xy = g
    
    array<ll, 2> M = {X / g, Y / g};
    array<ll, 2> O = {0, 0}, R = {X, Y-1}, U = {X-1, Y};
    ll t = surface(R, M) + surface(M, U);
    if (t < s) s = t, res = {O, R, M, U};
    
    y *= (-1); // Yx - Xy = g
    x += X * 10000, y += Y * 10000;
    // cout << -1 << " " << x << " " << y << endl;
    ll mul = min(x / (X / g), y / (Y / g));
    x -= (X / g) * mul, y -= (Y / g) * mul;
    
    R = {X, Y}, M = {x, y};
    t = surface(R, M);
    if (t < s) s = t, res = {O, R, M};
  }
  
  assert(s <= S);
  
  cout << res.size() << endl;
  for (auto [x, y] : res) {
    if (isrev) swap(x, y);
    cout << x << " " << y << endl;
  }
  
  return;
}

int main() {
  ll T; cin >> T;
  while (T--) {
    solve();
  }
  
  return 0;
}
accuracy: 0.08333333333333333
time_ms: 320
memory_kb: 3436
final_rank: 19

code_snippet: #include <bits/stdc++.h>

#define rep(i,s,n) for(int i = int(s); i < int(n); i++)
#define rrep(i,s,n) for(int i = int(n) - 1; i >= int(s); i--)
#define all(v) (v).begin(), (v).end()

using ll = long long;
using ld = long double;
using ull = unsigned long long;

template<class T>
bool chmin(T &a, T b) {
    if(a <= b) return false;
    a = b;
    return true;
}

template<class T>
bool chmax(T &a, T b) {
    if(a >= b) return false;
    a = b;
    return true;
}

using namespace std;

tuple<ll,ll,ll,ll> Farey_lr(ll a, ll b){
    assert(a > 0 && b > 0);
    if (a == b) return {0, 1, 1, 0};
    ll q = (a-1) / b;
    auto [x1,y1,x2,y2] = Farey_lr(b, a-q*b);
    return{q*x2+y2, x2, q*x1+y1, x1};
}

tuple<ll,ll,ll> extgcd(ll a, ll b){
    auto [x1,y1,x2,y2] = Farey_lr(a, b);
    tie(x1, y1) = make_pair(y1, -x1);
    tie(x2, y2) = make_pair(-y2,x2);
    ll g = a*x1 + b * y1;
    pair<ll,ll> key1 = make_pair(abs(x1) + abs(y1), x1);
    pair<ll,ll> key2 = make_pair(abs(x2) + abs(y2), x2);
    return(key1 < key2 ? make_tuple(x1, y1, g): make_tuple(x2, y2, g));
}

const ll mx = 50000;

void solve(){
    ll h, w; cin >> h >> w;
    if (h * w <= mx){
        cout << 3 << '\n';
        cout << 0 << ' ' << 0 << '\n';
        cout << 0 << ' ' << w << '\n';
        cout << h << ' ' << 0 << '\n';
        return ;
    }
    ll g = gcd(h,w);
    if (g > 50000){
        ll x = h/g;
        ll y = w/g;
        cout << 4 << '\n';
        cout << 0 << " " << 0 << endl;
        cout << h-1 << " " << w << endl;
        cout << x << " " << y << endl;
        cout << h << " " << w-1 << endl;
        return ;
    }
    auto [a, b, gg] = extgcd(h,w);
    assert(g == gg);
    assert(a * h + b * w == g);
    ll x = -b, y = a;
    ll hg = h/g, wg = w/g;
    while (x < 0 || y < 0){
        x += hg;
        y += wg;
    }
    while (x > h || y > w){
        x -= hg;
        y -= wg;
    }
    cout << 3 << endl;
    cout << 0 << " " << 0 << endl;
    cout << h << " " << w << endl;
    cout << x << " " << y << endl;
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);
    int t; cin >> t;
    while (t--){
        solve();
    }
}
accuracy: 1
time_ms: 340
memory_kb: 3456
final_rank: 7

code_snippet: #include <bits/stdc++.h>

#define rep(i,s,n) for(int i = int(s); i < int(n); i++)
#define rrep(i,s,n) for(int i = int(n) - 1; i >= int(s); i--)
#define all(v) (v).begin(), (v).end()

using ll = long long;
using ld = long double;
using ull = unsigned long long;

template<class T>
bool chmin(T &a, T b) {
    if(a <= b) return false;
    a = b;
    return true;
}

template<class T>
bool chmax(T &a, T b) {
    if(a >= b) return false;
    a = b;
    return true;
}

using namespace std;

tuple<ll,ll,ll,ll> Farey_lr(ll a, ll b){
    assert(a > 0 && b > 0);
    if (a == b) return {0, 1, 1, 0};
    ll q = (a-1) / b;
    auto [x1,y1,x2,y2] = Farey_lr(b, a-q*b);
    return{q*x2+y2, x2, q*x1+y1, x1};
}

tuple<ll,ll,ll> extgcd(ll a, ll b){
    auto [x1,y1,x2,y2] = Farey_lr(a, b);
    tie(x1, y1) = make_pair(y1, -x1);
    tie(x2, y2) = make_pair(-y2,x2);
    ll g = a*x1 + b * y1;
    pair<ll,ll> key1 = make_pair(abs(x1) + abs(y1), x1);
    pair<ll,ll> key2 = make_pair(abs(x2) + abs(y2), x2);
    return(key1 < key2 ? make_tuple(x1, y1, g): make_tuple(x2, y2, g));
}

void solve(){
    ll h, w; cin >> h >> w;
    ll g = gcd(h,w);
    if (g > 50000){
        ll x = h/g;
        ll y = w/g;
        cout << 4 << '\n';
        cout << 0 << " " << 0 << endl;
        cout << h-1 << " " << w << endl;
        cout << h << " " << w-1 << endl;
        cout << x << " " << y << endl;
        return ;
    }
    auto [a, b, gg] = extgcd(h,w);
    assert(g == gg);
    assert(a * h + b * w == g);
    ll x = -b, y = a;
    ll hg = h/g, wg = w/g;
    while (x < 0 || y < 0){
        x += hg;
        y += wg;
    }
    while (x > h || y > w){
        x -= hg;
        y -= wg;
    }
    cout << 3 << endl;
    cout << 0 << " " << 0 << endl;
    cout << h << " " << w << endl;
    cout << x << " " << y << endl;
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);
    int t; cin >> t;
    while (t--){
        solve();
    }
}
accuracy: 0.4166666666666667
time_ms: 340
memory_kb: 3448
final_rank: 12

code_snippet: #include <iostream>
#include <vector>
using namespace std;

long long gcd(long long a, long long b) {
	if (b == 0) return a;
	return gcd(b, a % b);
}

long long extgcd(long long a, long long b, long long& x, long long& y) {
	long long d = a;
	if (b != 0LL) {
		d = extgcd(b, a % b, y, x);
		y -= (a / b) * x;
	}
	else {
		x = 1; y = 0;
	}
	return d;
}

vector<pair<long long, long long>> solve(long long H, long long W) {
	// 3-Vertex Case
	if (gcd(H, W) <= 50000) {
		long long v1 = H / gcd(H, W);
		long long v2 = W / gcd(H, W);
		long long x = -1, y = -1;
		long long ret = extgcd(v2, v1, x, y);
		x *= -1LL;
		while (x < 0) { x += v1; y += v2; }
		while (x >= v1) { x -= v1; y -= v2; }
		vector<pair<long long, long long>> Return;
		Return.push_back(make_pair(0, 0));
		Return.push_back(make_pair(x, y));
		Return.push_back(make_pair(H, W));
		return Return;
	}

	// 4-Vertex Case
	else {
		long long rx = H / gcd(H, W);
		long long ry = W / gcd(H, W);
		vector<pair<long long, long long>> Return;
		Return.push_back(make_pair(0, 0));
		Return.push_back(make_pair(H - 1, W));
		Return.push_back(make_pair(rx, ry));
		Return.push_back(make_pair(H, W - 1));
		return Return;
	}
}

int main() {
	long long T, H, W;
	cin >> T;
	for (int i = 1; i <= T; i++) {
		cin >> H >> W;
		vector<pair<long long, long long>> Answer = solve(H, W);
		cout << Answer.size() << endl;
		for (int j = 0; j < Answer.size(); j++) cout << Answer[j].first << " " << Answer[j].second << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 420
memory_kb: 3456
final_rank: 8

code_snippet: #include <iostream>
#include <vector>
using namespace std;

long long gcd(long long a, long long b) {
	if (b == 0) return a;
	return gcd(b, a % b);
}

long long extgcd(long long a, long long b, long long& x, long long& y) {
	long long d = a;
	if (b != 0LL) {
		d = extgcd(b, a % b, y, x);
		y -= (a / b) * x;
	}
	else {
		x = 1; y = 0;
	}
	return d;
}

vector<pair<long long, long long>> solve(long long H, long long W) {
	// 3-Vertex Case
	if (gcd(H, W) <= 50000) {
		long long v1 = H / gcd(H, W);
		long long v2 = W / gcd(H, W);
		long long x = -1, y = -1;
		long long ret = extgcd(v2, v1, x, y);
		x *= -1LL;
		if (x < 0) { x += v1; y += v2; }
		vector<pair<long long, long long>> Return;
		Return.push_back(make_pair(0, 0));
		Return.push_back(make_pair(x * gcd(H, W), y * gcd(H, W)));
		Return.push_back(make_pair(H, W));
		return Return;
	}

	// 4-Vertex Case
	else {
		long long rx = H / gcd(H, W);
		long long ry = W / gcd(H, W);
		vector<pair<long long, long long>> Return;
		Return.push_back(make_pair(0, 0));
		Return.push_back(make_pair(H - 1, W));
		Return.push_back(make_pair(rx, ry));
		Return.push_back(make_pair(H, W - 1));
		return Return;
	}
}

int main() {
	long long T, H, W;
	cin >> T;
	for (int i = 1; i <= T; i++) {
		cin >> H >> W;
		vector<pair<long long, long long>> Answer = solve(H, W);
		cout << Answer.size() << endl;
		for (int j = 0; j < Answer.size(); j++) cout << Answer[j].first << " " << Answer[j].second << endl;
	}
	return 0;
}
accuracy: 0.08333333333333333
time_ms: 340
memory_kb: 3328
final_rank: 18

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<b;i++)
using ll = long long;
template<class T> bool chmin(T &a,const T b){if(a>b){a=b;return 1;}return 0;}
template<class T> bool chmax(T &a,const T b){if(a<b){a=b;return 1;}return 0;}
const int INF = (1<<30)-1;
const int mod=998244353;
using ld = long double;
const ld P=acosl(-1)/(ld)(180);
#define all(p) p.begin(),p.end()

ll extgcd(ll a,ll b,ll &x,ll &y){
	if(a>b){
		return extgcd(b,a,y,x);
	}
	if(a==0){
		x=0,y=1;
		return b;
	}
	ll s=b/a,t=b%a;
	ll ans=extgcd(a,t,x,y);
	x-=s*y;
	return ans;
}

int main(){
	int t;
	cin>>t;
	while(t){
		ll a,b,x,y;
		cin>>a>>b;
		ll D=extgcd(a,b,x,y);
		if(D<=49000){
			cout<<"3\n0 0\n";
			cout<<a<<" "<<b<<"\n";
			cout<<abs(y)<<" "<<abs(x)<<"\n";
		}
		else{
			cout<<"4\n0 0\n";
			cout<<a<<" "<<b-1<<"\n";
			cout<<a/D<<" "<<b/D<<"\n";
			cout<<a-1<<" "<<b<<"\n";
		}
		t--;
	}
}
accuracy: 1
time_ms: 140
memory_kb: 3436
final_rank: 3

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod=1000000007,MAX=400005,INF=1<<30;

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    int Q;cin>>Q;
    while(Q--){
        bool fl=false;
        ll H,W;cin>>W>>H;
        if(H<W){
            fl=true;
            swap(H,W);
        }
        if(W<=50000){
            if(fl){
                cout<<3<<"\n";
                cout<<0<<" "<<0<<"\n";
                cout<<1<<" "<<0<<"\n";
                cout<<H<<" "<<W<<"\n";
            }else{
                cout<<3<<"\n";
                cout<<0<<" "<<0<<"\n";
                cout<<0<<" "<<1<<"\n";
                cout<<W<<" "<<H<<"\n";
            }
        }else{
            ll a;
            if((H-1)%W==0){
                a=H-2;
            }else{
                a=H-1;
            }
            assert(a%W);
            pair<double,ll> mi=mp(1e10,0LL);
            for(ll k=1;k<=3000;k++){
                //assert((a*k)%W);
                ll s=((a*k)/W)+1;
                ll b=H*k/s-1;
                //if((H*k)%s==0) b--;
                chmin(mi,mp(s-(double)(a*k)/W,k));
            }
            //cout<<mi.fi<<" "<<mi.se<<endl;
            ll k=mi.se;
            ll s=((a*k)/W)+1;
            ll b=H*k/s-1;
            //if((H*k)%s==0) b--;
            
            if(fl){
                cout<<4<<"\n";
                cout<<0<<" "<<0<<"\n";
                cout<<a<<" "<<W<<"\n";
                cout<<s<<" "<<k<<"\n";
                cout<<H<<" "<<b<<"\n";
            }else{
                cout<<4<<"\n";
                cout<<0<<" "<<0<<"\n";
                cout<<W<<" "<<a<<"\n";
                cout<<k<<" "<<s<<"\n";
                cout<<b<<" "<<H<<"\n";
            }
        }
    }
}
accuracy: 0.08333333333333333
time_ms: 190
memory_kb: 3456
final_rank: 17

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod=1000000007,MAX=400005,INF=1<<30;

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    int Q;cin>>Q;
    while(Q--){
        bool fl=false;
        ll H,W;cin>>W>>H;
        if(H<W){
            fl=true;
            swap(H,W);
        }
        if(W<=50000){
            if(fl){
                cout<<3<<"\n";
                cout<<0<<" "<<0<<"\n";
                cout<<1<<" "<<0<<"\n";
                cout<<H<<" "<<W<<"\n";
            }else{
                cout<<3<<"\n";
                cout<<0<<" "<<0<<"\n";
                cout<<0<<" "<<1<<"\n";
                cout<<W<<" "<<H<<"\n";
            }
        }else{
            ll a;
            if((H-1)%W==0){
                a=H-2;
            }else{
                a=H-1;
            }
            assert(a%W);
            pair<double,ll> mi=mp(1e10,0LL);
            for(ll k=1;k<=10000;k++){
                //assert((a*k)%W);
                ll s=((a*k)/W)+1;
                ll b=H*k/s-1;
                //if((H*k)%s==0) b--;
                chmin(mi,mp(s-(double)(a*k)/W,k));
            }
            //cout<<mi.fi<<" "<<mi.se<<endl;
            ll k=mi.se;
            ll s=((a*k)/W)+1;
            ll b=H*k/s-1;
            //if((H*k)%s==0) b--;
            
            if(fl){
                cout<<4<<"\n";
                cout<<0<<" "<<0<<"\n";
                cout<<a<<" "<<W<<"\n";
                cout<<s<<" "<<k<<"\n";
                cout<<H<<" "<<b<<"\n";
            }else{
                cout<<4<<"\n";
                cout<<0<<" "<<0<<"\n";
                cout<<W<<" "<<a<<"\n";
                cout<<k<<" "<<s<<"\n";
                cout<<b<<" "<<H<<"\n";
            }
        }
    }
}
accuracy: 0.08333333333333333
time_ms: 540
memory_kb: 3268
final_rank: 20

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod=998244353,MAX=300005,INF=1<<30;

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    int Q;cin>>Q;
    while(Q--){
        ll x,y;cin>>x>>y;
        if(x<=50000){
            cout<<3<<"\n";
            cout<<0<<" "<<0<<"\n";
            cout<<0<<" "<<1<<"\n";
            cout<<x<<" "<<y<<"\n";
            continue;
        }
        if(y<=50000){
            cout<<3<<"\n";
            cout<<0<<" "<<0<<"\n";
            cout<<1<<" "<<0<<"\n";
            cout<<x<<" "<<y<<"\n";
            continue;
        }
        bool fl=false;
        if(x>y){
            fl=true;
            swap(x,y);
        }
        vector<pair<ll,ll>> ans;
        ll ax=x,ay=y-1,bx=x-1,by=y;
        for(ll i=1;;i++){
            ll j=(i*ay)/ax+1;
            if(i*by>j*bx){
                ans.push_back(mp(0,0));
                ans.push_back(mp(ax,ay));
                ans.push_back(mp(i,j));
                ans.push_back(mp(bx,by));
                break;
            }
        }
        cout<<si(ans)<<"\n";
        for(auto a:ans){
            if(fl) cout<<a.se<<" "<<a.fi<<"\n";
            else cout<<a.fi<<" "<<a.se<<"\n";
        }
    }
}
accuracy: 0.08333333333333333
time_ms: 50
memory_kb: 3304
final_rank: 15

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod=998244353,MAX=300005,INF=1<<30;

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    int Q;cin>>Q;
    while(Q--){
        ll x,y;cin>>x>>y;
        if(x<=50000){
            cout<<3<<"\n";
            cout<<0<<" "<<0<<"\n";
            cout<<0<<" "<<1<<"\n";
            cout<<x<<" "<<y<<"\n";
            continue;
        }
        if(y<=50000){
            cout<<3<<"\n";
            cout<<0<<" "<<0<<"\n";
            cout<<1<<" "<<0<<"\n";
            cout<<x<<" "<<y<<"\n";
            continue;
        }
        bool fl=false;
        if(x>y){
            fl=true;
            swap(x,y);
        }
        vector<pair<ll,ll>> ans;
        ll ax=x,ay=y-1,bx=x-1,by=y;
        for(ll i=1;;i++){
            ll j=(i*ay+ax-1)/ax;
            if(i*by>j*bx){
                ans.push_back(mp(0,0));
                ans.push_back(mp(ax,ay));
                ans.push_back(mp(i,j));
                ans.push_back(mp(bx,by));
                break;
            }
        }
        cout<<si(ans)<<"\n";
        for(auto a:ans){
            if(fl) cout<<a.se<<" "<<a.fi<<"\n";
            else cout<<a.fi<<" "<<a.se<<"\n";
        }
    }
}
accuracy: 0.08333333333333333
time_ms: 40
memory_kb: 3452
final_rank: 16

code_snippet: #include <iostream>
#include <algorithm>
#include <numeric>
#include <cassert>

template <typename T>
T extgcd(T a, T b, T& x, T& y) {
    T s = a, t = b,
      xs = 1, ys = 0,
      xt = 0, yt = 1;

    while (t != 0) {
        T div = s / t;

        s -= t * div;
        xs -= xt * div;
        ys -= yt * div;

        std::swap(s, t);
        std::swap(xs, xt);
        std::swap(ys, yt);
    }

    x = xs;
    y = ys;
    return s;
}

using namespace std;
using lint = long long;

void solve() {
    lint h, w;
    cin >> w >> h;

    auto g = gcd(h, w);
    if (g <= 50000) {
        auto hg = h / g, wg = w / g;
        lint x, y;

        extgcd(hg, wg, x, y);
        y = -y;

        while (x < 0) {
            x += wg;
            y += hg;
        }
        while (x > wg) {
            x -= wg;
            y -= hg;
        }
        while (y < 0) {
            x += wg;
            y += hg;
        }
        while (y > hg) {
            x -= wg;
            y -= hg;
        }
        assert(0 <= x && x <= wg &&
               0 <= y && y <= hg);
        assert(hg * x - wg * y == 1);

        cout << "3\n0 0\n"
             << w << " " << h << "\n"
             << x << " " << y << "\n";
    } else {
        cout << "4\n0 0\n"
             << w - 1 << " " << h << "\n"
             << w / g << " " << h / g << "\n"
             << w << " " << h - 1 << "\n";
    }
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    int n;
    cin >> n;
    while (n--) solve();

    return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 3448
final_rank: 2

code_snippet: #include <iostream>
#include <algorithm>
#include <numeric>
#include <cassert>

template <typename T>
T extgcd(T a, T b, T& x, T& y) {
    T s = a, t = b,
      xs = 1, ys = 0,
      xt = 0, yt = 1;

    while (t != 0) {
        T div = s / t;

        s -= t * div;
        xs -= xt * div;
        ys -= yt * div;

        std::swap(s, t);
        std::swap(xs, xt);
        std::swap(ys, yt);
    }

    x = xs;
    y = ys;
    return s;
}

using namespace std;
using lint = long long;

void solve() {
    lint h, w;
    cin >> w >> h;

    auto g = gcd(h, w);
    if (g <= 50000) {
        auto hg = h / g, wg = w / g;
        lint x, y;

        extgcd(hg, wg, x, y);
        y = -y;

        while (x < 0) {
            x += wg;
            y += hg;
        }
        while (x > wg) {
            x -= wg;
            y -= hg;
        }
        while (y < 0) {
            x += wg;
            y += hg;
        }
        while (y > hg) {
            x -= wg;
            y -= hg;
        }
        assert(0 <= x && x <= wg &&
               0 <= y && y <= hg);
        assert(hg * x - wg * y == 1);

        cout << "3\n0 0\n"
             << w << " " << h << "\n"
             << x * g << " " << y * g << "\n";
    } else {
        cout << "4\n0 0\n"
             << w - 1 << " " << h << "\n"
             << w / g << " " << h / g << "\n"
             << w << " " << h - 1 << "\n";
    }
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    int n;
    cin >> n;
    while (n--) solve();

    return 0;
}
accuracy: 0.08333333333333333
time_ms: 30
memory_kb: 3256
final_rank: 14

code_snippet: #include <cmath>
#include <iostream>
using namespace std;
long long extgcd(long long a, long long b, long long &x, long long &y) {
	long long g = a;
	x = 1, y = 0;
	if (b != 0) g = extgcd(b, a % b, y, x), y -= (a / b) * x;
	return g;
}
int main() {
	int q;
	cin >> q;
	for (int i = 0; i < q; ++i) {
		long long px, py, qx, qy;
		cin >> px >> py;
		long long g = extgcd(px, py, qy, qx);
		if (g <= 50000) {
			cout << 3 << endl;
			cout << 0 << ' ' << 0 << endl;
			cout << px << ' ' << py << endl;
			cout << abs(qx) << ' ' << abs(qy) << endl;
		}
		else {
			cout << 4 << endl;
			cout << 1 << ' ' << 0 << endl;
			cout << px << ' ' << py << endl;
			cout << 0 << ' ' << 1 << endl;
			cout << px / g * (g - 1) << ' ' << py / g * (g - 1) << endl;
		}
	}
	return 0;
}
accuracy: 1
time_ms: 530
memory_kb: 3088
final_rank: 5

code_snippet: #include <cmath>
#include <iostream>
using namespace std;
long long extgcd(long long a, long long b, long long &x, long long &y) {
	long long g = a;
	x = 1, y = 0;
	if (b != 0) g = extgcd(b, a % b, y, x), y -= (a / b) * x;
	return g;
}
int main() {
	int q;
	cin >> q;
	for (int i = 0; i < q; ++i) {
		long long px, py, qx, qy;
		cin >> px >> py;
		long long g = extgcd(px, py, qy, qx);
		if (g < 50000) {
			cout << 3 << endl;
			cout << 0 << ' ' << 0 << endl;
			cout << px << ' ' << py << endl;
			cout << abs(qx) << ' ' << abs(qy) << endl;
		}
		else {
			cout << 4 << endl;
			cout << 1 << ' ' << 0 << endl;
			cout << px << ' ' << py << endl;
			cout << 0 << ' ' << 1 << endl;
			cout << px / g * (g - 1) << ' ' << qx / g * (g - 1) << endl;
		}
	}
	return 0;
}
accuracy: 0.4166666666666667
time_ms: 440
memory_kb: 3088
final_rank: 11

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;



int gcd(int x,int y){
	if(x < y){

		swap(x,y);
	}

	if(y == 0){
		return x;
	}else{
		return gcd(y,x%y);
	}
}


int extend_Euclid(int a,int b,int& ans_x,int& ans_y){
	if(b == 0){ //再帰の一番最後にたどり着いた場合
		ans_x = 1;
		ans_y = 0;
		return a; //最小公倍数
	}else{
		int ret = extend_Euclid(b,(a%b),ans_y,ans_x);
		ans_y -= (a/b)*ans_x;
		return ret;
	}
}

//https://icpc.iisf.or.jp/past-icpc/comments/2016-slides.pdf

void func(){

	int x_bb,y_bb;

	scanf("%d %d",&x_bb,&y_bb);

	int x,y,num;

	num = extend_Euclid(x_bb,y_bb,x,y);

	if(num <= 50000){

		printf("3\n");
		printf("0 0\n");

		printf("%d %d\n",x_bb,y_bb);

		printf("%d %d\n",abs(y),abs(x));

	}else{

		printf("4\n");
		printf("0 0\n");

		printf("%d %d\n",x_bb-1,y_bb);
		printf("%d %d\n",x_bb/num,y_bb/num);
		printf("%d %d\n",x_bb,y_bb-1);
	}
}

int main(){

	int num_case;

	scanf("%d",&num_case);

	for(int loop = 0; loop < num_case; loop++){

		func();
	}

	return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 3220
final_rank: 1

code_snippet: #define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <algorithm>
#include <vector>
#include <map>
#include <functional>
#include <iostream>
#include <set>
#include <cstring>
#include <cmath>
#include <unordered_map>
#include <assert.h>

using namespace std;
typedef long long ll;

#define SIZE 500010
#define INF 1000000000

ll gcd(ll a, ll b) {
	if (a == 0) return b;
	return gcd(b%a, a);
}

ll power(int x, int n, int mod) {
	ll ret = 1, t = x;

	while (n) {
		if (n % 2) ret = (ret * t) % mod;
		n /= 2;
		t = (t * t) % mod;
	}

	return ret;
}


ll extgcd(ll a, ll b, int* x, int* y, int mod)
{
	ll u, v, q, tmp;

	*x = 1; *y = 0; u = 0; v = 1;
	while (b > 0) {
		q = a / b;
		tmp = u%mod; u = (*x - (q * u)%mod) % mod; *x = tmp;
		tmp = v%mod; v = (*y - (q * v)%mod) % mod; *y = tmp;
		tmp = b; b = a - q * b; a = tmp;
	}
	//if(d!=nullptr)*d = a;
	return a;
}
ll gete_phi(int mod) {
	//オイラー値の導出
	ll temp = mod;
	ll m = mod;
	std::unordered_map<int, ll> dp;
	if (dp.count(mod)) { return dp[mod]; }
	for (ll i = 2; i * i <= m; ++i)
	{
		if (m % i == 0)
		{
			temp = temp / i * (i - 1);
			for (; m % i == 0; m /= i);
		}
	}
	if (m != 1)temp = temp / m * (m - 1);
	return dp[mod] = temp;
}

int main() {
	int V;

	scanf("%d", &V);

	for (int i = 0; i < V; i++) {
		ll x, y;

		scanf("%lld%lld", &x, &y);

		bool isswap = x < y; // x >= y
		if (isswap) swap(x, y);

		ll g = gcd(x, y);

		ll x2 = x / g;
		ll y2 = y / g;

		ll ansx, ansy;

		//cerr << x << ", " << y << endl;
		//cerr << "g : " << g << endl;

		if (x2 == 1 && y2 == 1) {
			ansx = 1;
			ansy = 0;
		}
		else {
			int k,buf;//power(y2, gete_phi(x2) - 1, x2);
			extgcd(x2, y2, &buf, &k, x2);
			if (k < 0) { k += x2; }
			//cerr << " > " << k * y2 % x2 << endl;
			assert((k * y2 % x2) == 1);
			ansx = k;
			ansy = (ansx * y) / x;
		}

		if (isswap) swap(x, y);
		if (isswap) swap(ansx, ansy);

		printf("3\n"
			"0 0\n"
			"%lld %lld\n", x, y);
		printf("%lld %lld\n", ansx, ansy);

		// printf("%.10lf %.10lf\n", (double)ansy, (double)ansx * y / x);

		//printf("%.10lf\n", (double)ansx * y / x);

		//assert(abs(ansy - (double)ansx * y / x) < 1 + 1e-8);
		//double area = x * abs(ansy - (double)ansx * y / x) / 2;
		//printf("area: %.10lf\n", area);
	}
}
accuracy: 0.4166666666666667
time_ms: 100
memory_kb: 3220
final_rank: 10

code_snippet: #include <cstdio>
#include <algorithm>
#include <vector>
#include <map>
#include <functional>
#include <iostream>
#include <set>
#include <cstring>
#include <cmath>

using namespace std;
typedef long long ll;

#define SIZE 500010
#define INF 1000000000

ll gcd(ll a, ll b){
  if(a == 0) return b;
  return gcd(b%a, a);
}

ll power(int x, int n, int mod){
  ll ret = 1, t = x;

  while(n){
    if(n % 2) ret = (ret * t) % mod;
    n /= 2;
    t = (t * t) % mod;
  }

  return ret;
}

pair<pair<ll,ll>,ll> extGCD(ll p, ll q) {
  if (q == 0) return {{p, 1}, 0};
  auto r = extGCD(q, p%q);
  return {{r.first.first, r.second}, r.first.second - p/q*r.second};
}

int main(){
  int V;

  scanf("%d", &V);

  for(int i=0;i<V;i++){
    ll x, y;

    scanf("%lld%lld", &x, &y);

    bool isswap = x < y; // x >= y
    if(isswap) swap(x, y);
    
    ll g = gcd(x, y);

    ll x2 = x / g;
    ll y2 = y / g;

    ll ansx, ansy;

    //cerr << x << ", " << y << endl;
    //cerr << "g : " << g << endl;
    
    if(x2 == 1 && y2 == 1){
      ansx = 1;
      ansy = 0;
    }else{
      auto p = extGCD(y2, x2);
      //cerr << y2 << " " << x2 << endl;
      //cerr << "> " << p.first.first << " " << p.first.second << " " << p.second << endl;
      ll k = (p.first.second + x2) % x2;; //power(y2, gete_phi(x2)-1, x2);
      //cerr << k << endl;
      //cerr << " > " << k * y2 % x2 << endl;
      ansx = k;
      ansy = (ansx * y) / x;
    }

    if(isswap) swap(x, y);
    if(isswap) swap(ansx, ansy);
    
    printf("3\n"
           "0 0\n"
           "%lld %lld\n", x, y);
    printf("%lld %lld\n", ansx, ansy);

    // printf("%.10lf %.10lf\n", (double)ansy, (double)ansx * y / x);

    //printf("%.10lf\n", (double)ansx * y / x);

    //assert(abs(ansy - (double)ansx * y / x) < 1 + 1e-8);
    //double area = x * abs(ansy - (double)ansx * y / x) / 2;
    //printf("area: %.10lf\n", area);
  }
}
accuracy: 0.4166666666666667
time_ms: 80
memory_kb: 3224
final_rank: 9

code_snippet: #include <cstdio>
#include <algorithm>
#include <vector>
#include <map>
#include <functional>
#include <iostream>
#include <set>
#include <cstring>
#include <cmath>

using namespace std;
typedef long long ll;

#define SIZE 500010
#define INF 1000000000

ll gcd(ll a, ll b){
  if(a == 0) return b;
  return gcd(b%a, a);
}

ll power(int x, int n, int mod){
  ll ret = 1, t = x;

  while(n){
    if(n%2) ret = (ret * t) % mod;
    n /= 2;
    t = (t * t)%mod;
  }

  return ret;
}

int main(){
  int V;

  scanf("%d", &V);

  for(int i=0;i<V;i++){
    ll x, y;

    scanf("%lld%lld", &x, &y);

    ll g = gcd(x, y);

    ll x2 = x / g;
    ll y2 = y / g;

    ll ansx, ansy;
    
    if(x2 == 1 && y2 == 1){
      ansx = 1;
      ansy = 0;
    }else if(y2 == 1){
      ll k = power(y2, x2-2, x2);
      ansx = x - k;
      ansy =  (ansx * y + x - 1) / x;
    }else{
      ll k = power(x2, y2-2, y2);

      ansy = y - k;
      ansx = (ansy * x + y - 1) / y;
    }
    
    printf("3\n"
           "0 0\n"
           "%lld %lld\n", x, y);
    printf("%lld %lld\n", ansx, ansy);

    //double area = x * abs(ansy - (double)ansx * y / x) / 2;
    //printf("%.10lf\n", area);
  }
}
accuracy: 0.08333333333333333
time_ms: 60
memory_kb: 3204
final_rank: 13