11.16

· · 个人记录

T1

:::info[题意] 给定一个十进制整数 n,接下来会进行 q 次操作,每次操作会给一个字符串 c 与一个操作数 x,需要根据字符串 c 来对 n 与 x 进行对应的位运算,结果 重新存储 到 n 中。 ::: :::info[思路] 输入n和q,进行q次询问,然后每次输入一个字符串+一个x,表示位运算,接着每次输出位运算的结果即可。 :::

:::error[WA代码]

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

::: :::success[AC代码]

#include <bits/stdc++.h>
using namespace std; 
//#define int long long  不能开long long
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n, q;
    cin >> n >> q;
    while (q--){
        string c;
        int x;
        cin >> c >> x;
        if (c == "&"){//如果是&,那么进行按位与
            cout << (n &= x) << endl;
        }
        if (c == "|"){//如果是|,那么进行按位或
            cout << (n |= x) << endl;
        }
        if (c == "^"){//如果是^,那么进行按位异或
            cout << (n ^= x) << endl;
        }
        if (c == "<<"){//如果是<<,那么进行按位左移
            cout << (n <<= x) << endl;
        }
        if (c == ">>"){//如果是>>,那么进行按位右移
            cout << (n >>= x) << endl;
        }
    }
    return 0;
} 

:::

T3

:::info[题意] 输入N,求1~N之间的质数数量。 ::: :::info[思路] 输入一个N,进行埃氏筛,注意数据范围是1e8,所以为了避免MLE,选择用bool或char类型。 :::

:::error[WA代码]

#include <bits/stdc++.h>
using namespace std; 
#define int long long
const int N = 1e8+5;
int tong[N];
void f(){
    tong[0] = tong[1] = 1;
    for (int i = 2; i * i <= 1e8; i++){
        if (tong[i] == 0){
            for (int j = 2; j * i <= 1e8; j++){
                tong[i * j] = 1;
            }
        }   
    }
}
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int sum = 0;
    f();
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++){
        if (tong[i] == 0){
            sum++;
        }
    }
    cout << sum;
    return 0;
} 

::: :::success[AC代码]

#include <bits/stdc++.h>
using namespace std; 
#define int long long
const int N = 1e8+5;
bool tong[N];//为了避免MLE,选择用bool类型
void f(){
    tong[0] = tong[1] = 1;
    for (int i = 2; i * i <= 1e8; i++){
        if (tong[i] == 0){
            for (int j = 2; j * i <= 1e8; j++){
                tong[i * j] = 1;
            }
        }   
    }
}
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int sum = 0;
    f();//记得调用
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++){
        if (tong[i] == 0){//统计
            sum++;
        }
    }
    cout << sum;
    return 0;
} 

:::

T6

:::info[题意]

满足“N 和 (N+1)÷2 都是素数”的奇数 N,称为2017 类似数。

现在给出q个查询,请你求出满足l^i <= x <= r^i且 x 是2017 类似数的奇数 x 的个数。
:::info[思路]
>这里有左右端点,所以一定考前缀和。
输入q,表示数据组数,然后每次输入li和ri,直接输出前缀和数组之间的数值。

:::error[WA代码] 没写 ::: :::success[AC代码]

#include <bits/stdc++.h> 
using namespace std;
#define int long long
const int N = 1e5+5;
int tong[N], t[N], l;
int a[N], idx, s[N];
void f(){//基本埃氏筛
    tong[0] = tong[1] = 1;
    for (int i = 2; i * i <= 1e5; i++){
        if (tong[i] == 0){
            for (int j = 2; j * i <= 1e5; j++){
                tong[i * j] = 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]++;//如果是2017类似数,那么加一
        }
    }
    int t;
    cin>>t;
    while(t--)
    {
        int l, r;
        cin >> l >> r;
        cout << s[r] - s[l-1] << endl;//直接调用数组
    }
    return 0;
} 

:::

T7

:::info[题意] 即共有n个整数a^1 a^2 …… a^n,对于每一个数a^i,求其他的数中有多少个是它的约数。 ::: :::info[思路] 输入n和n个数,判断哪些是它的因数,每句会超时,所以个数也要用桶处理 :::

:::error[WA代码] 没写 ::: :::success[AC代码]

#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int 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++)//利用埃氏筛的性质
    {
        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 << endl;//直接输出这个能打的个数-自己
    }
    return 0;
}

:::