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()<<'\n';
for(int i=0; i<ans.size(); i++) cout<<ans[i]<<' '; cout<<endl;
return 0;
}
Codeforces Round #563 (Div. 2) ABCD 题解的更多相关文章
- Codeforces Round #315 (Div. 2) (ABCD题解)
比赛链接:http://codeforces.com/contest/569 A. Music time limit per test:2 seconds memory limit per test: ...
- Codeforces Round #258 (Div. 2)[ABCD]
Codeforces Round #258 (Div. 2)[ABCD] ACM 题目地址:Codeforces Round #258 (Div. 2) A - Game With Sticks 题意 ...
- # Codeforces Round #529(Div.3)个人题解
Codeforces Round #529(Div.3)个人题解 前言: 闲来无事补了前天的cf,想着最近刷题有点点怠惰,就直接一场cf一场cf的刷算了,以后的题解也都会以每场的形式写出来 A. Re ...
- Codeforces Round #557 (Div. 1) 简要题解
Codeforces Round #557 (Div. 1) 简要题解 codeforces A. Hide and Seek 枚举起始位置\(a\),如果\(a\)未在序列中出现,则对答案有\(2\ ...
- Codeforces Round #563 (Div. 2)/CF1174
Codeforces Round #563 (Div. 2)/CF1174 CF1174A Ehab Fails to Be Thanos 其实就是要\(\sum\limits_{i=1}^n a_i ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- Codeforces Round #268 (Div. 2) ABCD
CF469 Codeforces Round #268 (Div. 2) http://codeforces.com/contest/469 开学了,时间少,水题就不写题解了,不水的题也不写这么详细了 ...
- Codeforces Round #540 (Div. 3) 部分题解
Codeforces Round #540 (Div. 3) 题目链接:https://codeforces.com/contest/1118 题目太多啦,解释题意都花很多时间...还有事情要做,就选 ...
- Codeforces Round #538 (Div. 2) (A-E题解)
Codeforces Round #538 (Div. 2) 题目链接:https://codeforces.com/contest/1114 A. Got Any Grapes? 题意: 有三个人, ...
- Codeforces Round #531 (Div. 3) ABCDEF题解
Codeforces Round #531 (Div. 3) 题目总链接:https://codeforces.com/contest/1102 A. Integer Sequence Dividin ...
随机推荐
- Redis 分布式锁的正确实现原理演化历程与 Redission的源码
当线程A,加锁并设置过期时间-->执行业务-->判断锁id完成后,但这时CPU线程调度其它工作了在这里卡住了, 而且也到了锁的过期时间了被动被删除,当线程B,加锁并设置过期时间--> ...
- 如何在 MySQL 中监控和优化慢 SQL?
如何在 MySQL 中监控和优化慢 SQL? 在 MySQL 中,慢 SQL 查询是指那些执行时间较长的查询,通常会影响数据库的性能和响应时间.通过监控和优化这些慢 SQL 查询,可以提高数据库的效率 ...
- Asp .net core JWT验证
Jwt .wiz-editor-body .wiz-code-container { position: relative; padding: 8px 0; margin: 5px 0; text-i ...
- 被LangChain4j坑惨了!
最近在深度体验和使用 Spring AI 和 LangChain4j,从开始的满怀期待五五开,但最后极具痛苦的使用 LangChain4j,让我真正体验到了正规军和草台班子的区别. Spring AI ...
- PHP采集图片实例(PHP采集)
以下为引用的内容: <?php/** * 采集图片php程序** Copyright(c) 2008 by 小超(ccxxcc) All rights reserved** To cont ...
- mysql设置时区
参考:https://blog.csdn.net/vkingnew/article/details/82149726 查看时区 show variables like '%time_zone%'; 设 ...
- 【记录】ChatGPT|近期三次更新一览(更新至2023年2月3日)
如果你还没有使用过ChatGPT,可以先看看我的上一篇文章:[记录]ChatGPT|使用技巧与应用推荐(更新至2023年2月8日). 1月11号晚上,ChatGPT突然很多人都无法登录,包括我 ...
- 『Plotly实战指南』--在科学数据可视化中的应用(下)
科学数据往往涉及多个维度,例如分子结构中的空间坐标.物理实验中的时间序列以及化学反应中的温度变化等. 传统的二维可视化方法已经难以满足这些复杂数据的展示需求. 而Plotly,作为一种强大的可视化库, ...
- Vite 3 来了!新增功能 + 如何迁移
@charset "UTF-8"; .markdown-body { line-height: 1.75; font-weight: 400; font-size: 15px; o ...
- 高性能深度学习推理引擎 -- OpenPPL
OpenPPL OpenPPL是商汤基于自研高性能算字库的开源深度学习推理平台,能够让人工智能应用高效可靠地运行在现有的CPU/GPU等计算平台上,为云端场景提供人工智能推理服务 OpenPPL基于全 ...