Codeforces Round #563 (Div. 2) ABCD 题解

A. Ehab Fails to Be Thanos

题意:问你能否对a数组任意排序,使得前n段和不等于后n段和。

思路:水题,直接从小到大排序。这个情况都相等就一定无解。

view code
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 1e5+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0',  ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };

ll n;
ll a[maxn];

int main()
{
    n = read();
    ll sum1 = 0, sum2 = 0;
    rep(i,1,n*2) a[i] = read();
    sort(a+1,a+1+n+n);
    rep(i,1,n) sum1 += a[i];
    rep(i,n+1,n*2) sum2 += a[i];
    if(sum1==sum2) cout<<-1<<endl;
    else
    {
        rep(i,1,n*2) cout<<a[i]<<' '; cout<<endl;
    }
    return 0;
}


B. Ehab Is an Odd Person

题意:若a[i]+a[j]是奇数,就可以调换这两者。这个操作可以做任意多次。问你能构成的最小字典序序列。

思路:因为a[i]+a[j]要是奇数,必须是奇数+偶数的形式才行。而且能发现,其实只要奇数和偶数在a中都有,就可以拿出一个来和任意位置swap,就一定能达到字典序最小。反之就只能输出原序列。

view code
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 1e5+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0',  ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };

ll a[maxn];
bool vis[maxn];

int main()
{
    ll n = read();
    int cur = 2;
    ll ma = 1;
    while(cur<=n)
    {
        if(vis[cur])
        {
            cur++;
            continue;
        }
        for(ll i=1; i*cur<=n; i++)
        {
            if(!vis[i*cur]) a[i*cur] = ma, vis[i*cur] = 1;
        }
        cur++;
        ma++;
    }
    rep(i,2,n) cout<<a[i]<<' '; cout<<endl;
    return 0;
}


C. Ehab and a Special Coloring Problem

题意:让你构造一个从i=2到i=n的序列。其中两下标(i,j)若互质,则a[i]不能等于a[j]。让你使得序列中最大值最小。输出这个序列。

思路:直接看互质不好下手,我们可以在遍历到一个数的时候把它的n内倍数都赋予相同的值,这一步是贪心。那没被筛到的就是和它互质的,自然和它的值不同(每筛一趟要赋值的数就++)。那么外层遍历一遍1->n,内层遍历倍数,为一个调和级数的时间复杂度。总时间复杂度O(n(log)n)。

view code
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 1e5+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0',  ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };

ll a[maxn];
bool vis[maxn];

int main()
{
    ll n = read();
    int cur = 2;
    ll ma = 1;
    while(cur<=n)
    {
        if(vis[cur])
        {
            cur++;
            continue;
        }
        for(ll i=1; i*cur<=n; i++)
        {
            if(!vis[i*cur]) a[i*cur] = ma, vis[i*cur] = 1;
        }
        cur++;
        ma++;
    }
    rep(i,2,n) cout<<a[i]<<' '; cout<<endl;
    return 0;
}


D. Ehab and the Expected XOR Problem

题意:让你构造一个序列,a[i]<(2^n),同时任意子段的异或和不等于x或者0。让你输出一个长度最长的符合条件序列。

思路:有一个重要的结论要用到:
若b[i]为a前i个数的异或和。那么a[l]a[l+1]...^a[r] = b[r]^b[l-1]。
所以这个题要任意子段异或和不等于x或者0,就相当于任意b[r]^b[l-1]不等于0或x。
因此我们只需要枚举前缀和,看看到当前这个数的时候如果前缀和是i,会不会有ix这个前缀和已经存在,如果不存在,这个前缀和就合法,这个数就是ipre,pre为当前凑出来的数的前缀和。

view code
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 2e6+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0',  ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };

ll a[maxn];
ll vis[maxn];

int main()
{
    ll n = read();
    ll x = read();
    vector<ll> ans;
    vis[0] = 1;
    ll pre = 0;
    for(ll i=1; i<=(1<<n)-1;i ++)
    {
        if(vis[i^x]) continue;
        vis[i] = 1;
        ans.pb(i^pre);
        pre = i;
    }
    cout<<ans.size()<<'
';
    for(int i=0; i<ans.size(); i++) cout<<ans[i]<<' '; cout<<endl;
    return 0;
}


原文地址:https://www.cnblogs.com/Bgwithcode/p/13645523.html