1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
   | #include<bits/stdc++.h> using namespace std;
  const int N = 3000 + 10;
  void dfs(int x, int y, int n); void gao(int x, int y, int n, int m);
  struct node{ 	int x, y, w; };
  vector<node> ans;
  int dp[N];
  void gao(int x, int y, int n, int m){ 	if(!n || !m) return; 	if(n == m){ 		dfs(x, y, n); 		return; 	} 	if(n < m){ 		dfs(x, y, n); 		gao(x, y + n, n, m - n); 	}else{ 		dfs(x, y, m); 		gao(x + m, y, n - m, m); 	} }
  void dfs(int x, int y, int n){ 	if(n == 1) return; 	if(n * n <= 50){ 		ans.push_back({x, y, n}); 		return; 	} 	int a = dp[n], b = n - a; 	dfs(x, y, a); 	dfs(x + a, y + a, b); 	int dx, dy; 	for(dx = x + a, dy = y + a - b; dy >= y; dy -= b){ 		dfs(dx, dy, b); 	} 	dy += b; 	gao(dx, y, b, dy - y); 	for(dx = x + a - b, dy = y + a; dx >= x; dx -= b){ 		dfs(dx, dy, b); 	} 	dx += b; 	gao(x, dy, dx - x, b); 	ans.push_back({x, y, n}); }
  int find(int n, int m){ 	if(n > m) swap(n, m); 	if(n == 0) return 0; 	return m / n + find(n, m % n); }
  void solve(){ 	int n; 	cin >> n; 	dfs(1, 1, n); 	cout << ans.size() << '\n'; 	for(auto i : ans){ 		cout << i.x << ' ' << i.y << ' ' << i.w << '\n'; 	} }
  int main(){ 	int T = 1; 	for(int i = 2; i <= 1000; i++){ 		int res = 1e6, ans; 		for(int a = 1; a < i; a++){ 			int b = i - a; 			int cnt = 2 + 2 * find(a, b); 			if(cnt < res){ 				res = cnt; 				ans = a; 			} 		} 		dp[i] = ans; 	} 	ios::sync_with_stdio(0); 	while(T--){ 		solve(); 	} }
   |