当前位置: 首页 > news >正文

【小羊肖恩】小羊杯 Round 2 C+K

题目链接:https://ac.nowcoder.com/acm/contest/100672#question

C.是毛毛虫吗?

思路:

其实很简单,假设我们要满足题目所给条件,那么这个毛毛虫最坏情况下肯定是一条如下图所示的无向图

右端省略号为对称图形 ,其中红线为毛毛虫的主体

那我们可以知道,只要对于其中任意一个节点增加一个,那么就无法构成毛毛虫

再总结一下,即只要一个节点有三个子节点,且这三个子节点都含有一个子节点(不为父节点)

那么就无法构成毛毛虫

代码:

#include <iostream>
#include <algorithm>
#include<cstring>
#include<cctype>
#include<string>
#include <set>
#include <vector>
#include <cmath>
#define ll long long
using namespace std;

void solve()
{
    int n;
    cin >> n;
    vector<vector<int> >a(n + 1);
    for (int i = 1; i < n; i++) {
        int x, y;
        cin >> x >> y;
        a[x].push_back(y);
        a[y].push_back(x);

    }
    for (int i = 1; i <= n; i++)
    {
        if (a[i].size() >= 3)
        {
            int sum = 0;
            for (int j = 0; j < a[i].size(); j++)
            {
                int t = a[i][j];
                if (a[t].size() > 1)sum++;
            }
            if (sum >= 3) {
                cout << "NO" << endl;
                return;
            }
        }
    }
    cout << "YES" << endl;
}

int main()
{
    cin.tie(0)->sync_with_stdio(false);
    int t = 1;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}

K.友善的数

思路:

首先,只有x或y有一个为1,那么必定无法找出

那么接下来我们考虑什么情况这个数k与x,y不互质

可以显然看出,我们最小的情况肯定是 Px*Py ,其中P为构成x/y的最小质因数

那么就分两种情况

①.gcd(x,y) == 1

此时x和y互质,那么此时只能选x和y的最小质因数

②.gcd(x,y) != 1

此时x和y有着公约数,那么我们可以考虑旋转公约数的最小质因子,但是不能保证其一定比x和y的最小质因数之积小,所以还需要取min

对于如何选取x和y的质因数,我们可以想到欧拉筛,在欧拉筛中我们保证每次筛选都是最小质因数的i倍,所以我们便可以定义一个数组用于储存每个数的最小质因数,同时预处理一下

代码:

#include <iostream>
#include <algorithm>
#include<cstring>
#include<cctype>
#include<string>
#include <set>
#include <vector>
#include <cmath>
#define ll long long
using namespace std;

ll gcd(ll a,ll b)
{
    return !b ? a : gcd(b, a % b);
}
const int N = 2e5+1;
bool isp[N + 1];
vector<int> p;
int minp[N + 1];

void els()
{
    memset(isp, true, sizeof isp);
    isp[0] = isp[1] = false;
    for (int i = 2; i <= N; i++)
    {
        if (isp[i]) p.push_back(i), minp[i] = i;
        for (int j = 0; j < p.size() && p[j] * i <=N; j++)
        {
            minp[p[j] * i] = p[j];
            isp[p[j] * i] = false;
            if (i % p[j] == 0) break;
        }
    }
}

void solve()
{
    ll x, y;
    cin >> x >> y;
    if (x == 1 || y == 1)
    {
        cout << -1 << endl;
        return;
    }
    if (gcd(x,y) == 1)
    {
        cout << (ll)(minp[x]) * (ll)(minp[y]) << endl;
    }
    else
    {
        cout << min((ll)minp[gcd(x,y)], (ll)minp[x] * (ll)minp[y]) << endl;
    }
}

int main()
{
    els();
    cin.tie(0)->sync_with_stdio(false);
    int t = 1;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}

相关文章:

  • 迷你世界脚本区域接口:Area
  • 如何防止Python网络爬虫爬取网站内容
  • 每日一题之因数计数
  • 【MySQL】索引(页目录、B+树)
  • 算法分析 —— 《栈》
  • [Lc滑动窗口_1] 长度最小的数组 | 无重复字符的最长子串 | 最大连续1的个数 III | 将 x 减到 0 的最小操作数
  • 蓝桥与力扣刷题(蓝桥 核桃的数量)
  • 研究生综合管理系统的开发与应用在农业大学中的实践
  • Redis详解(实战 + 面试)
  • C语言——文件操作
  • Hive-06之函数 聚合Cube、Rollup、窗口函数
  • 一、旋钮式滑动变阻器模块
  • 最新常见的图数据库对比,选型,架构,性能对比
  • LangPrompt提示词
  • 【弹性计算】弹性裸金属服务器和神龙虚拟化(三):弹性裸金属技术
  • 【RAG】Embeding 和 Rerank学习笔记
  • 【计算机网络入门】初学计算机网络(九)
  • jenv 使用指南
  • Java异常
  • Python Cookbook-2.24 在 Mac OSX平台上统计PDF文档的页数
  • 低代码/seo短视频
  • lamp 搭建wordpress/网站建设优化推广系统
  • 厦门网站推广找谁/宁波微信推广平台哪个好
  • 厦门找一家做网站的公司/百度链接提交
  • 专做PPP项目网站/信息流优化师发展前景
  • 网站开发与推广就业/爱站网长尾词挖掘工具