H. Boundary
Description
Bethany would like to tile her bathroom. The bathroom has width w centimeters and length l l l centimeters. If Bethany simply used the basic tiles of size 1 × 1 1×1 1×1 centimeters, she would use w ⋅ l w⋅l w⋅l of them.
However, she has something different in mind.
On the interior of the floor she wants to use the
1
×
1
1×1
1×1 tiles. She needs exactly
(
w
−
2
)
⋅
(
l
−
2
)
(w−2)⋅(l−2)
(w−2)⋅(l−2) of these.
On the floor boundary she wants to use tiles of size
1
×
a
1×a
1×a for some positive integer
a
a
a. The tiles can also be rotated by
90
90
90 degrees.
For which values of a can Bethany tile the bathroom floor as described? Note that a can also be
1
1
1.
Input
Each test contains multiple test cases. The first line contains an integer t ( 1 ≤ t ≤ 100 ) t (1≤t≤100) 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 ≤ 1 0 9 ) w, l (3≤w,l≤10^9) w,l(3≤w,l≤109) — the dimensions of the bathroom.
Output
For each test case, print an integer k ( 0 ≤ k ) k (0≤k) k(0≤k) — the number of valid values of a for the given test case — followed by k k k integers a 1 , a 2 , … , a k ( 1 ≤ a i ) a1,a2,…,ak (1≤ai) a1,a2,…,ak(1≤ai) — the valid values of a. The values a 1 , a 2 , … , a k a1,a2,…,ak 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 200000 200000 integers.
Example
input
3
3 5
12 12
314159265 358979323
output
3 1 2 3
3 1 2 11
2 1 2
思路
本体的大致意思就是用
1
∗
x
1*x
1∗x 的瓷砖将地面周围一圈铺满,求有多少个
x
x
x 满足条件。由于瓷砖只能铺在周围,则能够铺满的
x
x
x 一定能够被
2
∗
(
l
+
w
−
2
)
2*(l+w-2)
2∗(l+w−2)整除,枚举所有符合条件的
x
x
x ,在这基础上分为三种情况:
第一种情况为
w
%
x
=
0
w\%x=0
w%x=0 ,在这种情况下
w
w
w的一条边能被铺满,判断
(
l
−
1
)
%
x
=
t
(l-1)\%x=t
(l−1)%x=t,如果
t
=
0
t=0
t=0,说明侧边能铺满 ,下边只能满足
(
w
−
2
)
%
x
=
0
(w-2)\%x=0
(w−2)%x=0时
x
x
x 才成立
第二种情况为
w
%
x
=
1
w\%x=1
w%x=1 ,在这种情况下
w
w
w的一条边会留一个格子,则需要判断以下两种情况
即判断
(
(
l
−
1
)
%
x
+
l
%
x
)
((l-1)\%x + l\%x)
((l−1)%x+l%x)是否等于
1
1
1
第三种情况为
w
%
x
=
2
w\%x=2
w%x=2 ,同理判断,可以看代码自行理解
记得开long long!
代码
#include <bits/stdc++.h>
#define ll long long
#define mem(s, i) memset(s, i, sizeof(s))
#define pb push_back
#define pii pair<int, int>
#define INF 0x7fffffff
using namespace std;
const int N = 2e5 + 5;
const int MOD = 1e9;
void solve() {
ll w, l;
cin >> w >> l;
ll sum = 2 * w + 2 * l - 4;
vector<ll> res;
set<ll> q;
for (ll i = 1; i <= sqrt(sum); i++) {
if (sum % i == 0) {
q.insert(i);
q.insert(sum / i);
}
}
for (auto i : q) {
if (w % i == 0) {
if ((l - 1) % i == 0 && (w - 2) % i == 0) res.pb(i);
if ((l - 1) % i == 1) res.pb(i);
}
if (w % i == 1) {
if ((l - 1) % i + (l % i) == 1) {
res.pb(i);
}
}
if (w % i == 2) {
if (l % i == 0) {
if ((w - 2) % i == 0) {
res.pb(i);
}
} else if (l % i == 1) {
if (w % i == 0) {
res.pb(i);
}
}
}
}
cout << res.size() << " ";
for (auto i : res) {
cout << i << " ";
}
cout << endl;
}
int main() {
int t = 1;
cin >> t;
while (t--) {
solve();
}
return 0;
}