2026.11.16 河西-203 赛后总结

· · 个人记录

比赛分数:

题目 难度 分数 思路
T1.T850018 入门 100 有
T2.T850019 普及- 100 有
T3.T850020 普及- 100 有
T4.T850023 普及- 100 有
T5.T850021 普及- 0 有
T6.T850024 暂无评定 75 有
T7.T850025 普及- 47 有

T5.T850021

题意:

T组x y
求第x和第y个素数的异或值是否为1

CE(MLE)思路:

按照题目意思判断第x个质数和第y个质数的异或值是不是1

CE(MLE)原因:

洛谷开1e9的bool数组会直接CE

CE(MLE)代码:

#include<bits/stdc++.h>
const int N=1e9+5;
#define int long long
using namespace std;
bool tong[N];
int adx=0;
int a[N];
void f()
{
    tong[0]=tong[1]=1;
    for(int i=2;i<=1e9/i;i++)
    {
        if(tong[i]==1)
        {
            continue;
        }
        for(int bs=2;bs*i<=1e9;bs++)
        {
            tong[bs*i]=1;
        }
    }
    for(int i=1;i<=1e9;i++)
    {
        if(tong[i]==0)
        {
            a[++adx]=i;
        }
    }
}
signed main()
{
//  std::ios::sync_with_stdio(false);
//  cin.tie(0);
//  cout.tie(0);
    f();
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        int x,y;
        cin>>x>>y;
        if((a[x]^a[y])==1)
        {
            cout<<"YES\n";
        }
        else
        {
            cout<<"NO\n";
        }
    }
    return 0;
}

AC思路:

寻找那些质数的异或值为1,实际发现是2 3

AC代码:

#include<bits/stdc++.h>
#define int long long
using namespace std;
signed main()
{
//  std::ios::sync_with_stdio(false);
//  cin.tie(0);
//  cout.tie(0);
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        int x,y;
        cin>>x>>y;
        if((x==1 && y==2) || (x==2 && y==1))
        {
            cout<<"Yes\n";
        }
        else
        {
            cout<<"No\n";
        }
    }
    return 0;
}

T6.T850024

题意:

Q组l r
求出l~r中有多少个2017类似数
(x和(x+1)÷2都是素数)

TLE思路:

直接暴力埃式筛加l~r循环

TLE原因

q是10^5,l和r也是10^5,
埃式筛的时间复杂度不管也有10^10,洛谷最多一秒10^9

TLE代码:

#include<bits/stdc++.h>
const int N=1e5+5;
#define int long long
using namespace std;
bool tong[N];
void f()
{
    tong[1]=tong[0]=1;
    for(int i=2;i<=1e5/i;i++)
    {
        if(tong[i]==1)
        {
            continue;
        }
        for(int bs=2;bs*i<=1e5;bs++)
        {
            tong[bs*i]=1;
        }
    }
}
signed main()
{
    std::ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    f();
    int Q;
    cin>>Q;
    while(Q--)
    {
        int l,r,sum=0;
        cin>>l>>r;
        for(int i=l;i<=r;i++)
        {
            if(tong[i]==0 && tong[(i+1)/2]==0)
            {
                sum++;
            }
        }
        cout<<sum<<"\n";
    }
    return 0;
}

AC思路:

把所有2017的前缀和都预处理出来,
最后再求区间和(s[r]-s[l-1])

AC代码:

#include<bits/stdc++.h>
const int N=1e5+5;
#define int long long
using namespace std;
bool tong[N];
int s[N];
void f()
{
    tong[1]=tong[0]=1;
    for(int i=2;i<=1e5/i;i++)
    {
        if(tong[i]==1)
        {
            continue;
        }
        for(int bs=2;bs*i<=1e5;bs++)
        {
            tong[bs*i]=1;
        }
    }
}
signed main()
{
    std::ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    f();
    for(int i=1;i<=1e5;i++)
    {
        s[i]=s[i-1];
        if(tong[i]==0 && tong[(i+1)/2]==0)
        {
            s[i]++;
        }
    }
    int Q;
    cin>>Q;
    while(Q--)
    {
        int l,r,sum=0;
        cin>>l>>r;
        cout<<s[r]-s[l-1]<<"\n";
    }
    return 0;
}

T7.T850025

题意:

共有n个Ai,求出Ai有多少个是他的约数

TLE思路

暴力直接枚举是不是他的约数,如果i==j continue;

TLE原因:

n是10^5,n^n就是10^10,洛谷一秒只能10^9次

TLE代码:

#include<bits/stdc++.h>
const int N=1e5+5;
#define int long long
using namespace std;
int a[N];
signed main()
{
    std::ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    for(int i=1;i<=n;i++)
    {
        int sum=0;
        for(int j=1;j<=n;j++)
        {
            if(i==j)
            {
                continue;
            }
            if(a[i]%a[j]==0)
            {
                sum++;
            }
        }
        cout<<sum<<"\n";
    }
    return 0;
}

AC思路:

题目说要求所有数的约数,
和埃式筛的思路差不多,
先用桶(tong)存所有数字个数,
再遍历一遍因子,把他的倍数标记,然后再加上tong[倍数×i]
for(int i=1;i<=1e6;i++)
{
    int num=i;
    if(tong[num]!=0)
    {
        for(int bs=1;bs*num<=1e6;bs++)
        {
            ans[num*bs]+=tong[num];
        }
    }
}

AC代码:

#include<bits/stdc++.h>
const int N=1e6+5;
#define int long long
using namespace std;
int a[N],ans[N],tong[N];
signed main()
{
    std::ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        tong[a[i]]++;
    }
    for(int i=1;i<=1e6;i++)
    {
        int num=i;
        if(tong[num]!=0)
        {
            for(int bs=1;bs*num<=1e6;bs++)
            {
                ans[num*bs]+=tong[num];
            }
        }
    }
    for(int i=1;i<=n;i++)
    {
        int num=a[i];
        cout<<ans[num]-1<<"\n";
    }
    return 0;
}

THE END