C . Serval and The Formula【Codeforces Round 1011 (Div. 2)】

C.Serval and The Formula

在这里插入图片描述

思路:

这场打的纯唐,导致掉大分了,主要就是卡这个题了。
一直在想怎么位运算,但是忽略了最基础的观察和构造方法,div2前三题的思维题一般都是:Attention is all you need.

注意到 x + y = ( x ⊕ y ) + 2 ( x & y ) x + y = (x \oplus y) + 2(x \& y) x+y=(xy)+2(x&y) ,说明想要满足题目要求的形式,就得让 x + k x+k x+k y + k y+k y+k的二进制在同一位上不能同时为1
可以证明x==y时是无解的。
又注意到可以把 m a x ( x , y ) max({x,y}) max(x,y) 变成 1000000 这样的形式,那么答案就很简单了:
k = 2 n − m a x ( x , y ) k=2^n-max(x,y) k=2nmax(x,y),n足够大就行。

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n'
#define int long long
#define pb push_back
#define pii pair<int, int>
#define FU(i, a, b) for (int i = (a); i <= (b); ++i)
#define FD(i, a, b) for (int i = (a); i >= (b); --i)
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
int zb(int n) { return log2f(n & -n); }
void solve() {
    int x, y;
    cin >> x >> y;
    if(x==y ){cout<<"-1\n";return;}
    if (y > x)
        swap(x, y);
    int k = (1<<(__lg(x)+1))-x;
    cout << k<<endl;
}

signed main() {
    cin.tie(0)->ios::sync_with_stdio(0);
    int T = 1;
    cin >> T;
    while (T--) {
        solve();
    }
    return 0;
}
B. Serval and Final MEX time limit per test1 second memory limit per test256 megabytes You are given an array a consisting of n≥4 non-negative integers. You need to perform the following operation on a until its length becomes 1 : Select two indices l and r (1≤l<r≤|a| ), and replace the subarray [al,al+1,…,ar] with a single integer mex([al,al+1,…,ar]) , where mex(b) denotes the minimum excluded (MEX)∗ of the integers in b . In other words, let x=mex([al,al+1,…,ar]) , the array a will become [a1,a2,…,al−1,x,ar+1,ar+2,…,a|a|] . Note that the length of a decreases by (r−l) after this operation. Serval wants the final element in a to be 0 . Help him! More formally, you have to find a sequence of operations, such that after performing these operations in order, the length of a becomes 1 , and the final element in a is 0 . It can be shown that at least one valid operation sequence exists under the constraints of the problem, and the length of any valid operation sequence does not exceed n . Note that you do not need to minimize the number of operations.The minimum excluded (MEX) of a collection of integers b1,b2,…,bk is defined as the smallest non-negative integer x which does not occur in the collection b . Input Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤1000 ). The description of the test cases follows. The first line of each test case contains a single integer n (4≤n≤5000 )the length of the array a . The second line contains n integers a1,a2,…,an (0≤ai≤n )the elements of the array a . It is guaranteed that the sum of n over all test cases does not exceed 5000 . Output For each test case, output a single integer k (0≤k≤n ) in the first line of output — the length of the operation sequence. Then, output k lines, the i -th line containing two integers li and ri (1≤li<ri≤|a| )the two indices you choose in the i -th operation, where |a| denotes the length of the array be
03-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值