SWERC 2021-2022部分题解

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 wl 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) (w2)(l2) 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(1t100) — 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(3w,l109) — the dimensions of the bathroom.

Output

For each test case, print an integer k ( 0 ≤ k ) k (0≤k) k(0k) — 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(1ai) — 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 1x 的瓷砖将地面周围一圈铺满,求有多少个 x x x 满足条件。由于瓷砖只能铺在周围,则能够铺满的 x x x 一定能够被 2 ∗ ( l + w − 2 ) 2*(l+w-2) 2(l+w2)整除,枚举所有符合条件的 x x x ,在这基础上分为三种情况:
第一种情况为 w % x = 0 w\%x=0 w%x=0 ,在这种情况下 w w w的一条边能被铺满,判断 ( l − 1 ) % x = t (l-1)\%x=t (l1)%x=t,如果 t = 0 t=0 t=0,说明侧边能铺满 ,下边只能满足 ( w − 2 ) % x = 0 (w-2)\%x=0 (w2)%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) ((l1)%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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值