codeforces round 948 div2(a,b,c)

题目链接

A

#include<bits/stdc++.h>

using namespace std;

#define int long long
#define PII pair<int,int>

void solve()
{
    int n,m;cin>>n>>m;
    if(n&1){
        if((m&1)&&m>=1&&m<=n)cout<<"YES"<<'\n';
        else cout<<"NO"<<'\n';
    }else{
        if((m%2==0)&&m>=0&&m<=n)cout<<"YES"<<'\n';
        else cout<<"NO"<<'\n';
    }
}

signed main()
{
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    int t;cin>>t;
    while(t--){
        solve();
    }
    return 0;
}

B

题目概述

给定正整数 x x x,构造满足以下条件的数列

思路

如果没有"相邻两个元素不同时为非零数",就变成了写出 x x x的二进制表示

而二进制表示与最终的答案相差在哪?在于二进制表示有连续的 1 1 1上,我们想要用什么代替连续的 1 1 1

经过写几个例子发现,几个连续的二进制 1 1 1可以转换为首位的 − 1 -1 −1和末位置 + 1 +1 +1的 1 1 1,如图

ACcode

#include<bits/stdc++.h>

using namespace std;

#define int long long
#define PII pair<int,int>

void solve()
{
    int x;cin>>x;
    vector<int>a(35,0);
    for(int i=0;i<32;i++){ 
        a[i]=x>>i&1;//位移运算存储二进制表示
    }
    for(int i=0;i+1<32;i++){
        if(a[i]==1&&a[i+1]==1){
            int j=i;
            while(a[j]==1&&a[j+1]==1)j++;
            a[i]=-1;
            a[j+1]=1;
            for(int k=i+1;k<=j;k++)a[k]=0;
            i=j;
        }
    }
    cout<<32<<'\n';
    for(int i=0;i<32;i++)cout<<a[i]<<' ';
    cout<<'\n';
}

signed main()
{
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    int t;cin>>t;
    while(t--){
        solve();
    }
    return 0;
}

C

题目大意

给定 n n n个正整数的数组 a a a,定义特殊子序列为该子序列的最小公倍数不在 a a a内,问 a a a的最长特殊子序列的长度

思路

如果最大元素 m a ma ma不能整除其他元素,则为 n n n

否则,最长的 l c m lcm lcm一定是 m a ma ma的约数,遍历约数筛最大即可

ACcode

#include<bits/stdc++.h>

using namespace std;

#define int long long
#define PII pair<int,int>

void solve()
{
    int n;cin>>n;
    vector<int>a(n+1);
    int ma=-1;
    for(int i=1;i<=n;i++)cin>>a[i],ma=max(ma,a[i]);
    for(int i=1;i<=n;i++){
        if(ma%a[i]){
            cout<<n<<'\n';
            return;
        }
    }

    //最长lcm必为ma约数
    int ans=0;
    auto check=[&](int x){
        for(int i=1;i<=n;i++)if(a[i]==x)return;
        int t,cnt;
        cnt=0;
        t=1;
        for(int i=1;i<=n;i++){
            if(x%a[i]==0){
                cnt++;
                t=lcm(t,a[i]);
            }
        }
        if(t==x)ans=max(ans,cnt);
    };
    for(int i=1;i*i<=ma;i++){
        if(ma%i==0){
            check(i);
            check(ma/i);
        }
    }
    cout<<ans<<'\n';
}

signed main()
{
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    int t;cin>>t;
    while(t--){
        solve();
    }
    return 0;
}
相关推荐
柠檬少少开发2 分钟前
图像拼接算法及实现(一)
人工智能·算法·计算机视觉
DreamByte7 分钟前
Python Tkinter小程序
开发语言·python·小程序
覆水难收呀15 分钟前
三、(JS)JS中常见的表单事件
开发语言·前端·javascript
阿华的代码王国19 分钟前
【JavaEE】多线程编程引入——认识Thread类
java·开发语言·数据结构·mysql·java-ee
繁依Fanyi25 分钟前
828 华为云征文|华为 Flexus 云服务器部署 RustDesk Server,打造自己的远程桌面服务器
运维·服务器·开发语言·人工智能·pytorch·华为·华为云
weixin_4866811441 分钟前
C++系列-STL容器中统计算法count, count_if
开发语言·c++·算法
基德爆肝c语言41 分钟前
C++入门
开发语言·c++
怀九日1 小时前
C++(学习)2024.9.18
开发语言·c++·学习·面向对象·引用·
一道秘制的小菜1 小时前
C++第七节课 运算符重载
服务器·开发语言·c++·学习·算法
易辰君1 小时前
Python编程 - 协程
开发语言·python