9.26

· · 个人记录

P8598 [蓝桥杯 2013 省 AB] 错误票据

思路

用一个map,每输入一个ID,如果那个ID已经标记了,那么那个ID是重号,否则标记ID。并且mn和mx打擂台记录最小和最大值。

然后遍历map (mn ~ mx) 没标记的ID就是断号。

代码

#include<bits/stdc++.h>
#define ll long long
using namespace std;
map<int, bool> mp;
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int n, an, am, mn = 1e9, mx = -1;
    cin >> n;
    for(int i = 1; i <= n; i++){
        int x;
        while(cin >> x){
            mn = min(mn, x);
            mx = max(mx, x);
            if(mp[x]){
                am = x;
            }else{
                mp[x] = 1;
            }
        }
    }
    for(int i = mn; i <= mx; i++){
        if(!mp[i]){
            an = i;
            cout << an << ' ' << am;
            return 0;
        }
    }
    return 0;
}

P12156 [蓝桥杯 2025 省 Java B] 电池分组

思路

推导

两组包含了所有的数,所以其异或和为

(a_1\oplus a_2\oplus a_3\oplus...\oplus a_{k-1}\oplus a_k)\oplus(a_{k+1}\oplus a_{k+2}\oplus a_{k+3}\oplus...\oplus a_{n-1}\oplus a_n)

也就是

a_1\oplus a_2\oplus a_3 \oplus ...\oplus a_{n-1} \oplus a_n

因为异或操作是二进制下两个数字的一位两两相同为0,否则为1,由此可以推断出,x\oplus x=0,所以想让两组相同,那么两组的异或和异或的结果要为0,又根据上面推断出来的,可得出

a_1\oplus a_2\oplus a_3 \oplus ...\oplus a_{n-1} \oplus a_n = 0

就可以分出异或和相同的两组。

实现

只需要算出a数组的异或和是否为0。

如果为0,则可以分出异或和相同的两组,输出 YES,否则输出 NO。

代码

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int a[1001], n;
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int t;
    cin >> t;
    while(t--){
        cin >> n;
        int yh;
        for(int i = 1; i <= n; i++){
            cin >> a[i];
            if(i == 1){
                yh = a[i];
            }else{
                yh = yh ^ a[i];
            }
        }
        if(yh == 0){
            cout << "YES\n";
        }else{
            cout << "NO\n";
        }
    }
    return 0;
}

P8738 [蓝桥杯 2020 国 C] 天干地支

思路

只需要算出当前年份离2020年有多远,再让天干和地支向前/后移动就好了。

代码

#include<bits/stdc++.h>
#define ll long long
using namespace std;
string tg[] = {"jia", "yi", "bing", "ding", "wu", "ji", "geng", "xin", "ren", "gui"}, dz[] = {"zi", "chou", "yin", "mao", "chen", "si", "wu", "wei", "shen", "you", "xu", "hai"};
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int n;
    cin >> n;
    int cha = (n - 2020) % 60 + 60;
    cha %= 60;
    cout << tg[(6 + cha) % 10] + dz[cha % 12];
    return 0;
}

B3856 [语言月赛 202309] 椰奶国

思路

没什么思路,复杂的模拟。

代码

#include<bits/stdc++.h>
#define int long long
#define ll long long
using namespace std;
int h[100011], ff[100011];
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    for(int i = 0; i < 100011; i++){
        ff[i] = i * 10 + 1; 
    }
    h[0] = 1;
    for(int i = 1; i < 100011; i++){
        h[i] = h[i - 1] + ff[i];
    }
    int t;
    cin >> t;
    while(t--){
        int n, a, b, c, e, f, g;
        cin >> n >> a >> b >> c >> e >> f >> g;
        ll day = 0;
        for(int i = 0; i < n; i++){
            day += h[i];
        }
        ll s1 = 0;
        for(int i = 0; i < a; i++){
            s1 += h[i];
        }
        for(int i = 0; i < b; i++){
            s1 += ff[i];
        }
        s1 += c + 1;
        ll s2 = 0;
        for(int i = 0; i < e; i++){
            s2 += h[i];
        }
        for(int i = 0; i < f; i++){
            s2 += ff[i];
        }
        s2 += g + 1;
        if(s2 < s1){
            cout << day - s1 + s2 << "\n";
        }else{
            cout << s2 - s1 << "\n";
        }
    }
    return 0;
}

P7036 [NWRRC 2016] Folding

思路

如果原来的长和宽大于目标的长和宽,就可以达成目标,否则不行,输出-1。

每次尽可能地折一半,统计长和宽折的次数。

代码

#include<bits/stdc++.h>
#define ll long long
using namespace std;

int calc(int W, int w, int H, int h){
    if(W < w || H < h){
        return INT_MAX;
    }
    int cnt = 0;
    while(W > w){
        cnt++;
        W = (W + 1) / 2;
    }
    int cnt1 = 0;
    while(H > h){
        cnt1++;
        H = (H + 1) / 2;
    }
    return cnt + cnt1;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int W, H, w, h;
    cin >> W >> H >> w >> h;
    int a1 = calc(W, w, H, h);
    int a2 = calc(W, h, H, w);
    if(min(a1, a2) == INT_MAX){
        cout << -1;
    }else{
        cout << min(a1, a2);
    }
    return 0;
}

P9632 [ICPC 2020 Nanjing R] K Co-prime Permutation

思路

前置知识

相邻两个数\gcd为1,任何数与1的\gcd为1,\gcd(n, n)为n。

实现

我们可以让前k个的\gcd(a_i,i)等于1。

于是,我们可以让前k个数向后循环移动一位,后面的数不变。

代码

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int n, k;
    cin >> n >> k;
    if(k == 0){
        cout << -1;
        return 0;
    }
    for(int i = 1; i <= k; i++){
        cout << i % k + 1 << " ";
    }
    for(int i = k + 1; i <= n; i++){
        cout << i << " ";
    }
    return 0;
}