Codeforces Round #779 (Div. 2)

A - Marin and Photoshoot

题意

给定01串,使得任意区间内,0的个数不超过1的个数

题解

每两个0之间至少要有2个1

Code

int n;
string s;

void solve(){
    cin >> n;
    cin >> s;
    int cnt = 0;
	for (int i = 1; i < n; i++){
		if (s[i] == '0' && s[i - 1] == '0')
            cnt += 2;
        if (i >= 2 && s[i] == '0' && s[i - 1] == '1' && s[i - 2] == '0')
            cnt += 1;
    }
	cout << cnt << endl;
}

B - Marin and Anti-coprime Permutation

题意

给定一组1n的排列,问能否使得该排列每一个数选取1n中的一个数乘自己(1~n每个只能用一次)使得最后的gcd>1

问有多少种方案

题解

这题比a要简单呜呜

我们如果想,乘法的本质就是加法,奇偶的加法只有全偶数和奇数相加就是奇数,那么我们将奇数赋予一个偶数的乘数,偶数对奇数相加那必然是偶数,剩下的奇数给偶数,这样全是奇数,就gcd也就是2了

那么有多少种方法呢?

当n为奇数的时候没有足够的偶数可以一一对应给奇数,那么方案数是0

当n为偶数的时候,有 n 2 \frac{n}{2} 2n个偶数, n 2 \frac{n}{2} 2n奇数,给奇数分配偶数有 A n 2 n 2 A_{\frac{n}{2}}^{\frac{n}{2}} A2n2n,给偶数分配奇数有 A n 2 n 2 A_{\frac{n}{2}}^{\frac{n}{2}} A2n2n,那么答案就是$ (A_{\frac{n}{2}}^{\frac{n}{2}} ) ^2$

Code

void solve(){
    cin >> n;
    if(n & 1)
        cout << 0 << endl;
    else{
        ll ans = 1;
        // ll tmp = 1;
        for (int i = 1; i <= n / 2; i++){
            ans = ans * i % mod;
        }
        cout << ans * ans % mod << endl;
    }
}

C - Shinju and the Lost Permutation

题意

我们定义一个数组b,对于一个排列p, b i = m a x ( p 1 , p 2 , … , p i ) b_i=max(p_1,p_2,\dots,p_i) bi=max(p1,p2,,pi),该数组的权值为数组b中元素的种类数。

现给定一数组c, c i c_i ci为排列p第 i − 1 i-1 i1次循环的权值

问根据这个数组c,我们能否确定其存在一个排列p与之对应

题解

按照上述的定义,首先我们能知道,c的长度为n,那么就会将排列所有的旋转出来的结果出现,所以说,n一定会出现在数组的首位置一次,且有且只有一次,所以c数组中如果出现了多个1,或者没出现1,那么一定是不合法的

其次,我们判断c数组的合法性的时候,c数组中的元素的顺序是无所谓的,但是相对顺序是不能变得,因为只是排列的初始状态不同,并且排列旋转后所有的状态都会出现,并不会影响其c中的元素,关键的是相对顺序

而又由于权值的计算方式和将元素前移的方式我们知道,当从1开始时,下一个出现的元素一定会比n小,所以权值增加1,同理后面操作也可能会增加1,但是也可能会出现下一个要过来的数比当前n之前的所有的数都要大,例如1 2 3 5 4,这样就会导致权值急剧下降,这是合法的

但是如果出现了下一位比当前位的差值超过了1,那么这是显然不可能的,因为每次都只会在前面多一个数,不可能会出现一次操作比n到的多了两个数

所以综上:

  • 1只能在c数组中必须且只能出现一次
  • 从c数组1所在的位置往后的数前后差值小于等于1

满足上述条件的就是YES

Code

// 官方题解代码,感觉学到了不少新的stl

void solve() {
    int n; cin >> n;
    vector<int> a(n);
    for (int &v: a) cin >> v;
    if (count(a.begin(), a.end(), 1) != 1) {
        cout << "NO\n";
        return;
    }
    int p = find(a.begin(), a.end(), 1) - a.begin();
    rotate(a.begin(), a.begin() + p, a.end());
    cout << a[0] << ' ';
    for (int i = 1; i < n; ++i) {
        cout << a[i] << ' ';
        if (a[i] - a[i - 1] > 1) {
            cout << "NO\n";
            return;
        }
    }
    cout << "YES\n";
}

D1 - 388535 (Easy Version)

题意

给定一个数组 a a a、一个 l l l和一个 r r r,数组a中包含了长度为 r − l + 1 r-l+1 rl+1 l ∼ r l\sim r lr的排列

将数组元素变为 a i = a i    x o r    x a_i=a_i \; xor \; x ai=aixorx

给定最后的数组a,问x是多少

ez版本中 l l l为0

题解

题目要求我们只需要对数组进行异或一次,那么本质上上,对于每一个数的同一位来说,如果^1,就说明对这位取反了,

那么我们只需要看看对于当前给定数组的每一位上统计是否有1的数量与原排列中0的数量相对应,有的话就说明x的这个位为1

Code

int T;
ll l, r;
int a[N];
int b[N];
int cnta[20][2];
int cntb[20][2];

void solve(){
    memset(cnta, 0, sizeof(cnta));
    memset(cntb, 0, sizeof(cntb));

    cin >> l >> r;
    for (int i = l; i <= r; i++){
        cin >> a[i];
    }

    for (int i = l; i <= r; i++){
        for (int j = 0; j < 20; j++){
            cnta[j][(a[i] >> j) & 1]++;
        }
    }

    for (int i = l; i <= r; i++){
        for (int j = 0; j < 20; j++){
            cntb[j][(i >> j) & 1]++;
        }
    }

    int ans = 0;

    for (int i = 0; i < 20; i++){
        if(cnta[i][0] == cntb[i][1])
            ans += 1ll << i;
    }
    cout << ans << endl;
    // for (int i = l; i <= r; i++){
    //     cout << (ans ^ a[i]) << ' ';
    // }
    // cout << endl
    //      << Endl;
}

D2 - 388535 (Hard Version)

题意

与d1一致,唯一的区别在于l不是一定等于0的

题解

ygg tql!https://zhuanlan.zhihu.com/p/488753758

leonard的板子tql

考察点:异或的性质与01trie求

要点:

  1. 异或的性质
    1. x^b=a; a^b=x
    2. $a\neq b \iff a \oplus x \neq b \oplus x $
    3. a ⊕ a = 0 , a ⊕ 0 = a a \oplus a=0, a\oplus 0=a aa=0,a0=a
  2. 01trie求最大、最小异或对、

Code

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <map>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <sstream>
#include <unordered_map>
#define ll long long
#define ull unsigned long long
#define re return
#define pb push_back
#define Endl "\n"
#define endl "\n"
#define x first
#define y second
#define all(x) (x).begin(),(x).end()

using namespace std;

using PII = pair<int, int>;

const int N = 1e6 + 10;
const int M = 1e5 + 10;
const int mod = 1000000007;
const int INF = 0x3f3f3f3f;

int dx[4] = {-1,0,1,0};
int dy[4] = {0,1,0,-1};

int T;
int l, r;
int a[N];
int tr[20 * (1 << 18)][2], idx;
// 每个数最多有31位,一个有N个数,一共需要31 * N个节点

void insert(int x) {
	int p = 0;
	for (int i = 20; i >= 0; i--) {
		int &s = tr[p][x >> i & 1];
		if (!s) s = ++idx;
		p = s;
	}
}
int query_mx(int x) {
	int p = 0, res = 0;
	for (int i = 20; i >= 0; i--) {
		int s = x >> i & 1;
		if (tr[p][!s]) {
			res += 1 << i;
			p = tr[p][!s];
		} else p = tr[p][s];
	}
	return res;
}
int query_mn(int x) {
	int p = 0, res = 0;
	for (int i = 20; i >= 0; i--) {
		int s = x >> i & 1;
		if (tr[p][s]) {
			p = tr[p][s];
		} else p = tr[p][!s], res += 1 << i;
	}
	return res;
}
void del(int x) {
	int p = 0;
	for (int i = 20; i >= 0; i--) {
		int s = tr[p][x >> i & 1];
		tr[p][x >> i & 1] = 0;
		p = s;
	}
}

void solve(){
    // init();
    cin >> l >> r;
    for (int i = l; i <= r; i++){
        cin >> a[i];
        insert(a[i]);
    }

    for (int i = l; i <= r; i++){
        int x = (a[i] ^ l);
        int maxx = (query_mx(x));
        int minn = (query_mn(x));
        //cout << x << "  " << maxx << "  " << minn << "\n";
        if(maxx == r && minn == l){
            cout << x << '\n';
            break;
        }
    }
    for (int i = l; i <= r; i++)
        del(a[i]);
    //idx = 0;
}

int main(){
    // ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    T = 1;
    cin >> T;
    while(T--){
        solve();
    }
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值