每日羊题 (质数筛 + 数学 | 构造 + 位运算)

Problem - E - Codeforces

思路:

一眼质数筛,但是需要发现题目的一些小细节

题目说到 i j 只要满足 i % j == 0 或 j % i == 0 一个就能连边,所以我们直接输出 质数数量 是错的,考虑分成两部分

①.

对于此部分的所有数 x,我们都能和 2*x 连边,因此这些数不需要额外的边

②.

对于此部分的所有数 x,我们只能去往小于 x 的部分连边,因为最小的 2*x 不在 1 ~ n 范围内,那么这部分的质数显然就没有连边,即需要连边

所以答案就是 cnt[n] - cnt[n/2],其中 cnt[i] 代表 1 ~ i 中含有的质数数量

最后特判 n = 2 和 n = 3 这两个会多计算 2 的情况即可

代码:

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define yes cout << "YES\n"
#define no cout << "NO\n"
mt19937_64 rnd(chrono::steady_clock::now().time_since_epoch().count());

vector<int> p;
int nop[10000005];
int cnt[10000005];

void init()
{
    nop[1] = nop[0] = 1;
    for (int i = 2; i <= 10000000; i++)
    {
        if(!nop[i]) p.push_back(i);
        for(int j = 0;j < p.size() && p[j] * i <= 10000000;j++)
        {
            nop[i * p[j]] = 1;
            if(i % p[j] == 0) break;
        }
        cnt[i] = p.size();
    }
}

void solve()
{
    int n;
    cin >> n;
    if(n == 2)
    {
        cout << 0 << endl;
        return;
    }
    if(n == 3)
    {
        cout << 1 << endl;
        return;
    }
    cout << cnt[n] - cnt[n/2] << endl;
}

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

Problem - F - Codeforces

思路:

首先一眼就能看出我们选的数只能是 a 的子集,且 b 必须也是 a 的子集,否则无解

所以先特判特殊情况:如果 a == b == 0,那么就只能为 0,如果 b 不是 a 的子集,那么无解

接下来考虑正常情况

如果 a 的二进制位中只含有一个 1,那么此时我们只能填 0 和 a,同时我们肯定不能只填 0,所以必须两个都填,因此 b 必须等于 a 才有解,否则无解

如果 a 的二进制位中含有多余一个 1(假设为 k),那么此时我们能选 个数,同时注意到一个性质,此时这些数的异或和为 0

证明如下:对于任意一个 1,我们都有 种可能分配别的位置的数,所以这一位最后异或绝对是 0,而对于每一位我们都有这样的结论,因此最后每一位异或都是 0,所以结果为 0,得证

那么此时如果 b = 0,那么这 个数我们都能选,否则我们就删掉 b,即选剩下 个数,显然此时数量是最多的

代码:

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define yes cout << "YES\n"
#define no cout << "NO\n"
mt19937_64 rnd(chrono::steady_clock::now().time_since_epoch().count());

void solve()
{
    int a, b;
    cin >> a >> b;
    if ((a & b) != b)
    {
        cout << "-1\n";
        return;
    }
    if (a == 0 && b == 0)
    {
        cout << "1\n";
        cout << "0\n";
        return;
    }
    int cnt = 0;
    for (int i = 0; i < 31; i++)
    {
        if (a >> i & 1)
            cnt++;
    }
    if (cnt == 1)
    {
        if (b == a)
        {
            cout << "2\n";
            cout << 0 << " " << a << endl;
        }
        else
        {
            cout << "-1\n";
        }
        return;
    }
    vector<int> ans;
    for (int i = 0; i <= a; i++)
    {
        if ((a & i) == i)
        {
            ans.push_back(i);
        }
    }
    if (b == 0)
    {
        cout << ans.size() << endl;
        for (int i = 0; i < ans.size(); i++)
        {
            cout << ans[i] << " ";
        }
    }
    else
    {
        cout << ans.size() - 1 << endl;
        for (int i = 0; i < ans.size(); i++)
        {
            if (ans[i] == b)
                continue;
            cout << ans[i] << " ";
        }
    }
    cout << endl;
}

signed main()
{
    cin.tie(0)->sync_with_stdio(0);
    int t = 1;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}
相关推荐
txzrxz5 小时前
结构体排序,双指针,单调栈
数据结构·算法·双指针算法·单调栈·结构体排序
AndrewHZ5 小时前
【AI黑话日日新】什么是AI智能体?
人工智能·算法·语言模型·大模型·llm·ai智能体
清风~徐~来5 小时前
【视频点播系统】WebSocketpp 介绍及使用
开发语言
恒者走天下5 小时前
cpp c++辅导星球价格调整
c++
wWYy.5 小时前
算法:二叉树最大路径和
数据结构·算法
葱明撅腚5 小时前
利用Python挖掘城市数据
python·算法·gis·聚类
We་ct5 小时前
LeetCode 36. 有效的数独:Set实现哈希表最优解
前端·算法·leetcode·typescript·散列表
爱吃大芒果5 小时前
Flutter for OpenHarmony 实战:mango_shop 路由系统的配置与页面跳转逻辑
开发语言·javascript·flutter
学***54236 小时前
如何轻松避免网络负载过大
开发语言·网络·php
weixin_395448916 小时前
main.c_cursor_0129
前端·网络·算法