2026/11/16考试总结

· · 个人记录

题目相关信息(表格)

题目 难度 思路(没有/有一点/有 分数
T850018 T1 入门 有 10
T850019 T2 普及- 有 0
T850020 T3 普及- 有 80
T850023 T4 普及- 有 100
T850021 T5 普及- 有一点 0
T850024 T6 暂无评定 有一点 75
T850025 T7 普及 无 0

题目详细

T850018 T1

题意

这题是让我们用整数n和每次输入的字符串c和整数x,求出每次的答案并输出。

思路

只要先输入n,再输入n次c,接着将c与每一种位运算符对比,然后按照对应的位运算符进行运算与输出。

注意

该题只能用int类型!!!

原来的错误代码

#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,x,q;
string c;
signed main(){
    cin>>n>>q;
    while(q--){
        cin>>c;
        if(c=="<<"){
            cin>>x;
            cout<<(n<<x)<<endl;
            n=(n<<x);
        }else if(c==">>"){
            cin>>x;
            cout<<(n>>x)<<endl;
            n=(n>>x);
        }else if(c=="^"){
            cin>>x;
            cout<<(n^x)<<endl;
            n=(n^x);
        }else if(c=="|"){
            cin>>x;
            cout<<(n|x)<<endl;
            n=(n|x);
        }else{
            cin>>x;
            cout<<(n&x)<<endl;
            n=(n&x);
        }
    }
}

AC代码

#include<bits/stdc++.h>
//#define int long long 不能加
using namespace std;
int n,x,q;
string c;
signed main(){
    cin>>n>>q;
    while(q--){
        cin>>c;
        if(c=="<<"){
            cin>>x;
            cout<<(n<<x)<<endl;
            n=(n<<x);
        }else if(c==">>"){
            cin>>x;
            cout<<(n>>x)<<endl;
            n=(n>>x);
        }else if(c=="^"){
            cin>>x;
            cout<<(n^x)<<endl;
            n=(n^x);
        }else if(c=="|"){
            cin>>x;
            cout<<(n|x)<<endl;
            n=(n|x);
        }else{
            cin>>x;
            cout<<(n&x)<<endl;
            n=(n&x);
        }
    }
}

错因

写模板的时候开了 #define int long long

T850019 T2

题意

输入n个非负整数,将它们二进制下的1的个数之和输出,再判断它的奇偶性。

思路

将n个非负整数二奇偶性进制下的1的个数数出来并输出,最后%2,判断奇偶性。

原来的错误代码

#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
int n,c[105],sum;
int r(int x){
    string ans="";
    int s=0;
    while(x){
        ans=char(x%=2);
    }
    for(int i=0;i<=ans.size()-1;i++){
        if(ans[i]=='1'){
            s++;
        }
    }
    return s;
}
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>c[i];
        sum+=r(c[i]); 
    }
    cout<<sum<<" "<<sum%2;
    return 0;
}

AC代码

#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
int f(int x){
    int ans=0;
    while(x!=0){
        if(x%2==1){
            ans++;
        }
        x/=2;
    }
    return ans;
}
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n,sum=0;
    cin>>n;
    while(n--){
        int x;
        cin>>x;
        sum+=f(x);
    }
        cout<<sum<<" "<<sum%2;
    return 0;
}

错因

数1时的for可以换成while,并且可以不开数组。

T850020 T3

题意

求1~n之间的质数个数。

思路

只需用埃氏筛将1~n之间的质数数出来。

原来的错误代码

#include<bits/stdc++.h>
#define int long long
using namespace std;
int n;
int s;
int tong[100000005];
void f(){
    tong[0]=tong[1]=1;
    for(int i=2;i*i<=n;i++){
        if(tong[i]==0){
            for(int bs=2;i*bs<=n;bs++){
                tong[i*bs]=1;
            }
        }
    }
}
signed main(){
    cin>>n;
    f();
    for(int i=1;i<=n;i++){
        if(tong[i]==0){
            s++;
        }
    }
    cout<<s;
}

AC代码

#include<bits/stdc++.h>
#define int long long
using namespace std;
int n;
int s;
bool tong[100000005];
void f(){
    tong[0]=tong[1]=1;
    for(int i=2;i*i<=n;i++){
        if(tong[i]==0){
            for(int bs=2;i*bs<=n;bs++){
                tong[i*bs]=1;
            }
        }
    }
}
signed main(){
    cin>>n;
    f();
    for(int i=1;i<=n;i++){
        if(tong[i]==0){
            s++;
        }
    }
    cout<<s;
}

错因

tong的int类型占用内存比较大,要用bool,不然会MLE。

T850023 T4

题意

输入一个数n,如果有两个正整数x和y满足:x<=y、x+y=n、x和y都是质数,就输出x贺和y。

思路

枚举x,就能用n-x算出y,再判断x是否<=y,用埃氏筛判断x与y是否为质数,然后输出x、y。

AC代码

#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
int n,s,tong[10000005];
void f(){
    tong[0]=tong[1]=1;
    for(int i=2;i*i<=1e7;i++){
        if(tong[i]==0){
            for(int bs=2;i*bs<=1e7;bs++){
                tong[i*bs]=1;
            }
        }
    }
}
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    f();
    cin>>n;
    for(int i=1;i<=n;i++){
        if(tong[i]==0&&tong[n-i]==0&&i<=n-i){
            cout<<i<<" "<<n-i<<endl;
            s++;
        }
    }
    if(s==0) cout<<-1<<" "<<-1<<endl;
    return 0;
}

T850021 T5

题意

进行t组询问,每次询问给出两个数:x、y,问第x个质数和第y个质数的异或值是否是1?

思路

老师讲解后,我知道了只有第1个质数和第2个质数的异或值为1,因此,只要判断x==1&&y==2||x==2&&y==1,满足输出Yes,否则输出No。

原来的错误代码

#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
int t,x,y,tong[100000005],e[100000005],s;
void f(){
    tong[0]=tong[1]=1;
    for(int i=2;i*i<=1e8;i++){
        if(tong[i]==0){
            for(int bs=2;i*bs<=1e8;bs++){
                tong[i*bs]=1;
            }
        }
    }
    int xb=1;
    for(int i=1;i<=1e8;i++){
        if(tong[i]==0){
            e[xb]=i;
            xb++; 
        }
    }
}
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    f();
    cin>>t;
    while(t--){
        cin>>x>>y;
        if((e[x]^e[y])==1){
            cout<<"Yes"<<endl;
        }else{
            cout<<"No"<<endl;
        }
        //cout<<e[x]<<" "<<e[y]<<endl;
    }
    return 0;
}

AC代码

#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
int t,x,y;

signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin>>t;
    while(t--){
        cin>>x>>y;
        if(x==1&&y==2||x==2&&y==1) cout<<"Yes"<<endl;
        else cout<<"No"<<endl;
    }
}

错因

没有找到规律,以为是把每次输入对应的质数都进行异或。

T850024 T6

题意

满足n和(n+1)÷2 都是质数的奇数n,称为2017 类似数,输入q次l,r,求出l~r之间是2017类似数的个数。

思路

for循环从l到r枚举n,如果n和(n+1)÷2都是质数,就用前缀和计数。

原来的错误代码

#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
int q,l,r,tong[100005],s;
void f(){
    tong[0]=tong[1]=1;
    for(int i=2;i*i<=1e5;i++){
        if(tong[i]==0){
            for(int bs=2;i*bs<=1e5;bs++){
                tong[i*bs]=1;
            }
        }
    }
}
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    f();
    cin>>q;
    while(q--){
        cin>>l>>r;
        for(int i=l;i<=r;i++){
            if(tong[i]==0&&tong[(i+1)/2]==0&&i%2==1){
                s++;
            }
        }
        cout<<s<<endl;
        s=0;
    }
    return 0;
}

AC代码

#include<bits/stdc++.h>
#define int long long
#define endl '\n '
using namespace std ;
const int N = 1e5 + 5 ;
bool tong[N];
int s[N];
void f() {
    tong [0] = tong [1] = 1;
    for (int i = 2; i * i <= 1e5; i++) {
        if (tong [i] == 0) {
            for (int bs = 2; i * bs <= 1e5; bs++) {
                tong [i * bs] = 1;
            }
        }
    }
}
signed main() {
    ios ::sync_with_stdio(0);
    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 t ;
    cin >> t ;
    while (t--) {
        int l, r ;
        cin >> l >> r ;
        cout << s [r] - s [l - 1] << endl ;
    }
    return 0 ;
}

错因

没有想到用前缀和计数,这样可以省时间。

T850025 T7

题意

有n个整数,求其他的数中有多少个是a[i]的约数。

思路

将其他数与每一个数相除,能整除就记录,最后-1,不能算上它自己。

原来的错误代码

没写

AC代码

#include<bits/stdc++.h>
#define int long long
#define endl '\n '
using namespace std ;
const i nt N = 1e6 + 5 ;
int a [N], ans [N], tong [N];
signed main() {
    ios ::sync_with_stdio(0);
    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++) { //遍历数字
//拿到一个数字i
        int num = i ;
        if (tong[num] != 0) { //存在Num这个数字
//将num的倍数全部增加num的个数个因子(可以算其他奶牛,不能算自己)
            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 << endl ;
    }
    return 0 ;
}