Codeforces Round 853 Div2 A-D 题解

codeforces round 853

好久没有做题了呢,今天就来看看题目。

然后手贱点进去了个cn场,做了半天发现不对劲,赶紧撤退。再开一场吧。

A. Serval and Mocha's Array

这道题让所有长度大于2的前缀的gcd小于本身的长度,问是否可行

乍一听很吓唬人,但是我们可以想一下,首先如果都是素数,那么gcd肯定是1,然后1和其他的gcd又是1,所以一定是满足的。

那么2呢?我们可以发现,如果是2的倍数,那么gcd肯定是2,如果是奇数,那么肯定是1,又可以归到1上了

So,我们只需要找到gcd为小于2的两个数字让他们做前缀就好了

AC 代码
#include <bits/stdc++.h>

using namespace std;
constexpr int limit =  (4e5  + 5);//防止溢出
#define INF 0x3f3f3f3f
#define inf 0x3f3f3f3f3f3f3f
#define lowbit(i) i&(-i)//一步两步
#define EPS 1e-9
#define FASTIO  ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);
#define pi(a, b) pair<a,b>
#define rep(i, a, b) for(ll i = a; i <= b ; ++i)
#define per(i, a, b) for(ll i = b ; i >= a  ; --i)
#define MOD 998244353
#define traverse(u) for(int i = head[u]; ~i ; i = edge[i].next)
#define FOPEN freopen("C:\\Users\\tiany\\CLionProjects\\akioi\\data.txt", "rt", stdin)
#define FOUT freopen("C:\\Users\\tiany\\CLionProjects\\akioi\\dabiao.txt", "wt", stdout)
typedef long long ll;
typedef unsigned long long ull;
char buf[1 << 23], *p1 = buf, *p2 = buf, obuf[1 << 23], *O = obuf;
inline ll read() {
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
    ll sign = 1, x = 0;
    char s = getchar();
    while (s > '9' || s < '0') {
        if (s == '-')sign = -1;
        s = getchar();
    }
    while (s >= '0' && s <= '9') {
        x = (x << 3) + (x << 1) + s - '0';
        s = getchar();
    }
    return x * sign;
#undef getchar
}//快读
void print(ll x) {
    if (x / 10) print(x / 10);
    *O++ = x % 10 + '0';
}

void write(ll x, char c = 't') {
    if (x < 0)putchar('-'), x = -x;
    print(x);
    if (!isalpha(c))*O++ = c;
    fwrite(obuf, O - obuf, 1, stdout);
    O = obuf;
}
int n;
int a[limit];
void solve() {
    cin>>n;
    rep(i,1, n){
        cin>>a[i];
    }
    rep(i,1,n){
        rep(j, i + 1, n){
            if(gcd(a[i], a[j]) <= 2){
                cout<<"Yes"<<endl;
                return;
            }
        }
    }
    cout<<"No"<<endl;
}
int32_t main() {
#ifdef LOCAL
    FOPEN;
//    FOUT;
#endif
    FASTIO
    int kase;
    cin>>kase;
    while (kase--)
    invoke(solve);
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << "s";
    return 0;
}

B. Serval and Inversion Magic

这道题是给一个01串,问是否可以通过flip一个区间来得到一个回文串

这道题刚开始愣了半天,但是我仔细想了下发现要从结果反推原因,我们可以发现,如果有需要反转的区间(if any)存在,那么这个区间一定是连续的,不然就无法通过一次反转得到回文串了。

那么我们就可以贪心去找这个串,然后判断下是否连续就好了

AC 代码
#include <bits/stdc++.h>

using namespace std;
constexpr int limit =  (4e5  + 5);//防止溢出
#define INF 0x3f3f3f3f
#define inf 0x3f3f3f3f3f3f3f
#define lowbit(i) i&(-i)//一步两步
#define EPS 1e-9
#define FASTIO  ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);
#define pi(a, b) pair<a,b>
#define rep(i, a, b) for(ll i = a; i <= b ; ++i)
#define per(i, a, b) for(ll i = b ; i >= a  ; --i)
#define MOD 998244353
#define traverse(u) for(int i = head[u]; ~i ; i = edge[i].next)
#define FOPEN freopen("C:\\Users\\tiany\\CLionProjects\\akioi\\data.txt", "rt", stdin)
#define FOUT freopen("C:\\Users\\tiany\\CLionProjects\\akioi\\dabiao.txt", "wt", stdout)
typedef long long ll;
typedef unsigned long long ull;
char buf[1 << 23], *p1 = buf, *p2 = buf, obuf[1 << 23], *O = obuf;
inline ll read() {
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
    ll sign = 1, x = 0;
    char s = getchar();
    while (s > '9' || s < '0') {
        if (s == '-')sign = -1;
        s = getchar();
    }
    while (s >= '0' && s <= '9') {
        x = (x << 3) + (x << 1) + s - '0';
        s = getchar();
    }
    return x * sign;
#undef getchar
}//快读
void print(ll x) {
    if (x / 10) print(x / 10);
    *O++ = x % 10 + '0';
}

void write(ll x, char c = 't') {
    if (x < 0)putchar('-'), x = -x;
    print(x);
    if (!isalpha(c))*O++ = c;
    fwrite(obuf, O - obuf, 1, stdout);
    O = obuf;
}
int n;
int a[limit];
void solve() {
    cin>>n;
    string str;
    cin>>str;
    str = " " + str;
    int mid = (n + 1) / 2;
    int l = 1, r = n;
    int num = 0;
    int last = -1;
    rep(i, 1, n / 2){
        if(str[i] != str[n - i + 1]){
            if(num and i - last > 1){
                cout<<"NO"<<endl;
                return;
            }
            num++;
            last = i;
        }
    }
    cout<<"YES"<<endl;
}
int32_t main() {
#ifdef LOCAL
    FOPEN;
//    FOUT;
#endif
    FASTIO
    int kase;
    cin>>kase;
    while (kase--)
    invoke(solve);
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << "s";
    return 0;
}

C. Serval and Toxel's Arrays

这题很绕,大概意思是给一个串和修改一些位置上的元素,过程中这个数组始终是pairwise distinct的,最后所有的历史version数组pair up,每个pair的贡献是这两个数字拼接起来不重复元素的个数,问最后所有pair加起来的答案是多少

这个题,有点绕,确实很绕,我弟一开始没看懂,但是后面看明白了。

首先这种题我们要考虑贡献,我们需要统计元素的出现次数。然后观察样例,m次操作总共会产生m + 1个数组,我们把结果数组分为旱涝保收的前半部分和optional的后半部分,然后我们发现前半部分的每个元素贡献是出现次数的等差数列求和,后半部分是(出现次数) * (没出现次数)

很简单的一道题

AC 代码
#include <bits/stdc++.h>

using namespace std;
constexpr int limit =  (4e5  + 5);//防止溢出
#define INF 0x3f3f3f3f
#define inf 0x3f3f3f3f3f3f3f
#define lowbit(i) i&(-i)//一步两步
#define EPS 1e-9
#define FASTIO  ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);
#define pi(a, b) pair<a,b>
#define rep(i, a, b) for(ll i = a; i <= b ; ++i)
#define per(i, a, b) for(ll i = b ; i >= a  ; --i)
#define MOD 998244353
#define traverse(u) for(int i = head[u]; ~i ; i = edge[i].next)
#define FOPEN freopen("C:\\Users\\tiany\\CLionProjects\\akioi\\data.txt", "rt", stdin)
#define FOUT freopen("C:\\Users\\tiany\\CLionProjects\\akioi\\dabiao.txt", "wt", stdout)
typedef long long ll;
typedef unsigned long long ull;
char buf[1 << 23], *p1 = buf, *p2 = buf, obuf[1 << 23], *O = obuf;
inline ll read() {
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
    ll sign = 1, x = 0;
    char s = getchar();
    while (s > '9' || s < '0') {
        if (s == '-')sign = -1;
        s = getchar();
    }
    while (s >= '0' && s <= '9') {
        x = (x << 3) + (x << 1) + s - '0';
        s = getchar();
    }
    return x * sign;
#undef getchar
}//快读
void print(ll x) {
    if (x / 10) print(x / 10);
    *O++ = x % 10 + '0';
}

void write(ll x, char c = 't') {
    if (x < 0)putchar('-'), x = -x;
    print(x);
    if (!isalpha(c))*O++ = c;
    fwrite(obuf, O - obuf, 1, stdout);
    O = obuf;
}
int n,m;
int a[limit];
void solve() {
    cin>>n>>m;
    map<int, int>mp;//记录每个数字出现了多少次
    map<int, int>last;
    rep(i,1,n){
        cin>>a[i];
        mp[a[i]];
        last[a[i]];
    }
    rep(i, 1, m){
        int p, v;
        cin>>p>>v;
        if(a[p] == v)continue;
        int num = a[p];
        mp[num] += i - last[num];
        last.erase(num);
        last[v] = i;
        a[p] = v;
    }
    for(auto [k, v] : last){
//        cout<<k<<" "<<v<<endl;
        mp[k] += m - v + 1;
    }
    ll ans = 0;

    for(auto [k, v] : mp){
//        cout<<k<<" "<<v * (m + 1 - v)<<endl;
        ans += 1ll * v * (m + 1 - v) ;
        ans += 1ll * v * (v - 1) / 2;
    }
    cout<<ans<<endl;



}
int32_t main() {
#ifdef LOCAL
    FOPEN;
//    FOUT;
#endif
    FASTIO
    int kase;
    cin>>kase;
    while (kase--)
    invoke(solve);
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << "s";
    return 0;
}

D. Serval and Shift-Shift-Shift

这个题给两个01串a和b,每次操作可以选一个1-n的数字

然后

\(a \oplus (a >>k)\)
or
\(a \oplus (a <<k)\)

然后要求让a最多操作n次变成b,给出方案,如果不可行输出-1

这个题,题目给了暗示,就是操作n次,那么就提示我们给每个位置分配一次。

首先考虑一下什么情况是不可行的,1可以在xor里面变成0,但是0不可能变成1,所以如果a的某一位是0,b的某一位是1,那么就不可能变成b,所以我们可以先把这种情况排除掉。反过来也一样

然后因为我们不希望xor影响我们的前面配好的,所以我们就从左边开始,首先找到在b的msb左边的所有为1的bit,然后我们找到a的1,顶上去,把这些地方变成1,0不用管

之后我们去扫b的msb右边,然后我们可以用刚刚配好的1去把当前的0或者1变成1和0,从左到右,我们只shift right,因为我们不想干涉前面配好的,shift right前面的1都会和0异或变成1,0和0还是0,所以不影响,然后记录下这个shift的offset,就是答案

AC 代码
#include <bits/stdc++.h>

using namespace std;
constexpr int limit =  (4e5  + 5);//防止溢出
#define INF 0x3f3f3f3f
#define inf 0x3f3f3f3f3f3f3f
#define lowbit(i) i&(-i)//一步两步
#define EPS 1e-9
#define FASTIO  ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);
#define pi(a, b) pair<a,b>
#define rep(i, a, b) for(ll i = a; i <= b ; ++i)
#define per(i, a, b) for(ll i = b ; i >= a  ; --i)
#define MOD 998244353
#define traverse(u) for(int i = head[u]; ~i ; i = edge[i].next)
#define FOPEN freopen("C:\\Users\\tiany\\CLionProjects\\akioi\\data.txt", "rt", stdin)
#define FOUT freopen("C:\\Users\\tiany\\CLionProjects\\akioi\\dabiao.txt", "wt", stdout)
typedef long long ll;
typedef unsigned long long ull;
char buf[1 << 23], *p1 = buf, *p2 = buf, obuf[1 << 23], *O = obuf;
inline ll read() {
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
    ll sign = 1, x = 0;
    char s = getchar();
    while (s > '9' || s < '0') {
        if (s == '-')sign = -1;
        s = getchar();
    }
    while (s >= '0' && s <= '9') {
        x = (x << 3) + (x << 1) + s - '0';
        s = getchar();
    }
    return x * sign;
#undef getchar
}//快读
void print(ll x) {
    if (x / 10) print(x / 10);
    *O++ = x % 10 + '0';
}

void write(ll x, char c = 't') {
    if (x < 0)putchar('-'), x = -x;
    print(x);
    if (!isalpha(c))*O++ = c;
    fwrite(obuf, O - obuf, 1, stdout);
    O = obuf;
}
int n,m;
int a[limit];
void solve() {
    string pa,pb;
    cin>>n;
    cin>>pa>>pb;
    pa = " " + pa;
    pb = " " + pb;
    int cnt_a = count(pa.begin(),pa.end(),'1');
    int cnt_b = count(pb.begin(),pb.end(),'1');
    if(pa == pb){
        cout<<0<<endl;
        return;
    }
    if (!cnt_a or !cnt_b) {
        cout<<-1<<endl;
        return;
    }
    int fst = 0, scd = 0;
    rep(i,1,n){
        if(pa[i] == '1') {
            fst = i;
            break;
        }
    }
    vector<int>ans;
    rep(i,1,n){
        if(pb[i] == '1') {
            scd = i;
            break;
        }
    }
    auto bit_xor = [](char x, char y){
        return (x - '0') ^ (y - '0') + '0';
    };
    auto shift_left = [&](int x){
        rep(i, 1, n - x){
            pa[i] = bit_xor(pa[i], pa[i + x]);
        }
    };
    auto shift_right = [&](int x){
        per(i, 1, n - x){
            pa[i + x] = bit_xor(pa[i], pa[i + x]);
        }
    };
    per(i, 1, scd){
        if(pa[i] == pb[i])
            continue;
        per(j, 1, n){
            if(pa[j] == '1'){
                int offset = j - i;
                ans.push_back(offset);
                if (offset > 0) {
                    shift_left(offset);
                } else {
                    shift_right(-offset);
                }
                break;
            }
        }
    }
    rep(i, scd + 1, n){
        if(pa[i] == pb[i])
            continue;
        int offset = scd - i;
        ans.push_back(offset);
        shift_right(-offset);
    }
    cout<<ans.size()<<"\n";
    for(auto i : ans){
        cout<<i<<" ";
    }
    cout<<"\n";




}
int32_t main() {
#ifdef LOCAL
    FOPEN;
//    FOUT;
#endif
    FASTIO
    int kase;
    cin>>kase;
    while (kase--)
    invoke(solve);
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << "s";
    return 0;
}

救命,现在菜狗一个,啥都不行,这可得支愣起来,不然咋办啊!

posted @ 2023-07-09 18:31  tiany7  阅读(17)  评论(0编辑  收藏  举报