CodeForces:H、Boundary

222 阅读2分钟

一起养成写作习惯!这是我参与「掘金日新计划 · 4 月更文挑战」的第25天,点击查看活动详情

题目描述

Problem - H - H. Boundary

Bethany would like to tile her bathroom. The bathroom has width w centimeters and length l centimeters. If Bethany simply used the basic tiles of size 1×1 centimeters, she would use w⋅l of them.

However, she has something different in mind.

On the interior of the floor she wants to use the 1×1 tiles. She needs exactly (w−2)⋅(l−2) of these. On the floor boundary she wants to use tiles of size 1×a for some positive integer a. The tiles can also be rotated by 90 degrees. For which values of a can Bethany tile the bathroom floor as described? Note that a can also be 1.

Input

Each test contains multiple test cases. The first line contains an integer t (1≤t≤100) — the number of test cases. The descriptions of the t test cases follow.

Each test case consist of a single line, which contains two integers w, l (3≤w,l≤109) — the dimensions of the bathroom.

Output

For each test case, print an integer k (0≤k) — the number of valid values of a for the given test case — followed by k integers a1,a2,…,ak (1≤ai) — the valid values of a. The values a1,a2,…,ak have to be sorted from smallest to largest.

It is guaranteed that under the problem constraints, the output contains at most 200000 integers.

input

3
3 5
12 12
314159265 358979323

output

3 1 2 3
3 1 2 11
2 1 2

Note

In the first test case, the bathroom is 33 centimeters wide and 55 centimeters long. There are three values of aa such that Bethany can tile the floor as described in the statement, namely a=1a=1, a=2a=2 and a=3a=3. The three tilings are represented in the following pictures.

07ab4f102ebaf2a3ad3562155fe883c082ef2d70.png (737×342) (codeforces.com)

问题解析

这题是说,给你一个宽w,长l的地板,让你用一种长度为1* a的瓷砖填满最外圈(蓝色部分),求有多少种长度为a的瓷砖可以做到。

我们可以先求:想铺满最外圈,最长可以用哪些瓷砖,然后再求那些瓷砖可以组成这些长度,比如要一种长度为8的瓷砖可以拼出最外圈,那么4和2也可以。这一步求小瓷砖的过程其实就是求最大公约数。然后一共有这5种方法铺满最外圈:

那我们就求出每一种方法所用到的瓷砖的最大公约数即可,然后就像我们前面说的,如果8可以,那么4 2也可以,所以我们还要对最大公约数分解,去重后排序输出。

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<math.h>
#include<set>
#include<numeric>
#include<string>
#include<string.h>
#include<iterator>
#include<map>
#include<unordered_map>
#include<stack>
#include<list>
#include<queue>
#include<iomanip>

#define endl '\n';
typedef long long ll;
typedef pair<ll, ll>PII;
const int N = 1e6 + 50;
map<int, int>mymap;
int gcd(int a,  int b)
{
	return b == 0 ? a : gcd(b, a % b);
}
void get_gcd(int x, int y)
{
	int ans = gcd(x , y);
	for (int i = 1; i <= ans / i; i++)
	{
		if (ans % i == 0)
		{
			mymap[i] = 1;
			mymap[ans / i] = 1;
		}
	}
}
int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		mymap.clear();
		int x,  y;
		cin >> x >> y;
		vector<int>res;
		
		mymap[1] = 1;
		mymap[2] = 1;
		get_gcd(x - 2, y);
		get_gcd(x, y - 2);
		get_gcd(y, gcd(x - 1, y - 2));
		get_gcd(x, gcd(y - 1, x - 2));
		get_gcd(x - 1, y - 1);
		for (auto i : mymap)
		{
			if (i.second == 1)res.push_back(i.first);
		}
		cout << res.size() << " ";
		sort(res.begin(), res.end());
		for (int i = 0; i < res.size(); i++)
		{
			cout << res[i] << " ";
		}
		cout << endl;
	}
	return 0;
}