2022-04-26每日刷题打卡

本文介绍了两个编程题目。第一题涉及排列问题,要求判断是否能按特定条件排列人数,通过分析可能的非法排列形态来确定答案。第二题关注瓷砖铺设,需找出所有能用于边界铺设的瓷砖长度,通过计算最大公约数和分解来得到答案。这两题都需要深入理解题目要求并运用适当的数据结构和算法解决。
摘要由CSDN通过智能技术生成

2022-04-26每日刷题打卡

代码源——每日一题

排队 - 题目 - Daimayuan Online Judge

请判断有没有一种方法可以将编号从 1 到 N 的 N 个人排成一排,并且满足给定的 M 个要求。

对于每个要求会给出两个整数 Ai 和 Bi,表示编号 Ai 和 Bi 的人是相邻的。

保证每个要求都不同,比如已经给出了 1,5,就不会再给出 1,5 或 5,1。

输入格式

第一行两个整数 N 和 M,表示 N 个人和 M 个要求。

输出格式

如果有一种能把这些人拍成一排并满足所有条件的方法,就输出 Yes,否则,输出 No。

样例输入1

4 2
1 3
2 3

样例输出1

Yes

样例输入2

4 3
1 4
2 4
3 4

样例输出2

No

样例输入3

3 3
1 2
1 3
2 3

样例输出3

No

数据规模

对于全部数据保证 2≤N≤105,0≤M≤105,1≤Ai<Bi≤N。

对于这里的排队要求,一共有三种可能违法的情况:(鼠标画图很难的)
在这里插入图片描述

1是整体是一个圈,此时每个点都至少被两个边连着。

2是有形成圈的情况,此时至少有一个点被三条边连着。

3是可能分成两个不相接的两部分(题目并没有说每个点都有要求),一个是正常的队列,一个是环。

我们先根据题目要求把所有边都连上,然后看形成的图的形状。如果有一个点被三条边连着,满足第二个条件,输出no;如果没有点是只被一条边连着,满足第一个条件,输出no。

第三个条件就比较麻烦了,我们可以先找到所有只被一条边连着的点,然后以这些点为起点遍历图,如果最后有点在提问中出现过,但没有在这次遍历中被遍历到,说明那些点自成一个环了(由于不和我们的边连着,所以遍历不到),输出no。

其余情况都输出yes。

#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 = 100500;

int main()
{
    int n, m;
    cin >> n >> m;
    bool flag = true;
    unordered_map<int, vector<int>>mymap;
    unordered_map<int, int>cnt;
    while (m--)
    {
        int a, b;
        cin >> a >> b;
        cnt[a] = 1;
        cnt[b] = 1;
        if (mymap[a].size() == 2)
        {
            cout << "No";
            return 0;
        }
        mymap[a].push_back(b);
        if (mymap[b].size() == 2)
        {
            cout << "No";
            return 0;
        }
        mymap[b].push_back(a);
    }
    vector<int>st;
    for (auto i : mymap)
    {
        if (i.second.size() > 2)
        {
            cout << "NO";
            return 0;
        }
        if (i.second.size() == 1)
        {
            st.push_back(i.first);
        }
    }
    int len = st.size();
    if (len == 0&&mymap.size()!=0)
    {
        cout << "No";
        return 0;
    }
    for (int i = 0; i < len; i++)
    {
        cnt[st[i]] = 0;
        int x = st[i], y = mymap[st[i]][0];
        cnt[y] = 0;
        while (1)
        {
            if (mymap[y].size() != 1)break;
            else if (mymap[y][0] != x)
            {
                cnt[mymap[y][0]] = 0;
                x = y;
                y = mymap[y][0];
            }
            else
            {
                cnt[mymap[y][1]] = 0;
                x = y;
                y = mymap[y][1];
            }
        }
    }
    for (auto i : cnt)
    {
        if (i.second == 1)
        {
            cout << "No";
            return 0;
        }
    }

    cout << "Yes";
    return 0;
}

CodeForces

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;
}
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值