Codeforces Round #626 Div2 D,E
比赛链接:
Codeforces Round #626 (Div. 2, based on Moscow Open Olympiad in Informatics)
D.Present
题意:
给定大小为$n$的a数组,求下面式子的结果:
$$ (a_1 + a_2) \oplus (a_1 + a_3) \oplus \ldots \oplus (a_1 + a_n) \\ \oplus (a_2 + a_3) \oplus \ldots \oplus (a_2 + a_n) \\ \ldots \\ \oplus (a_{n-1} + a_n) \\$$
分析:
这题看了题解补的
分别求结果的第$k$(以0开始计数)位是否为1
显然,我们不需要关心每个数第$k$位以上是什么,那么对数组取模$2^{k+1}$
两个数的和的第$k$位为1时,才对答案的第$k$位有贡献,那么和的第$k$位为1需要属于$[2^k; 2^{k+1})$或者$[2^{k+1} + 2^k; 2^{k+2} - 2]$,求出这样的和的对数,如果对数为奇数,那么答案的第$k$位为1,否则为0
求对数可以用二分查找来求
AC代码:
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for (int i=a;i<=b;i++)
#define per(i,a,b) for (int i=b;i>=a;i--)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define SZ(x) ((int)(x).size()) typedef long long ll;
typedef vector<int> VI;
typedef pair<int,int> PII; const ll mod=1e5+7;
const int maxn=4e5+7;
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;} int ans,a[maxn],b[maxn],n;
int pow2[30]; int cal(int k){
ll res=0;
rep(i,1,n)b[i]=a[i]%(1<<(k+1));
sort(b+1,b+1+n);
rep(i,1,n){
int x=b[i];
// cout<<"x="<<x<<endl;
if(x*2>=pow2[k]&&x*2<=pow2[k+1]-1)res--;
else if(x*2>=pow2[k]+pow2[k+1])res--;
res+=upper_bound(b+1,b+1+n,pow2[k+1]-1-x)-lower_bound(b+1,b+1+n,pow2[k]-x);
res+=upper_bound(b+1,b+1+n,1e9)-lower_bound(b+1,b+1+n,pow2[k]+pow2[k+1]-x);
}
// cout<<"res="<<res<<endl;
return res/2%2;
} int main() {
// cout<<(1<<24)<<endl;
rep(i,0,29)pow2[i]=(1<<i);
scanf("%d",&n);
rep(i,1,n)scanf("%d",&a[i]);
rep(i,0,25)if(cal(i))ans+=(1<<i);
printf("%d\n",ans); return 0;
}
E.Instant Noodles
题意:
在一个$2n$个节点,$m$条边的二分图中,右边部分每个节点有一个权值
构建一个左边节点的子集$S$,所有和这些子集有边的右边节点构成点集$N(S)$,$N(S)$的所有节点权值和为$F(S)$
求所有$F(S)$的最大公约数
分析:
首先是结论,给右边点分类,如果两个点的边集相同,那么他们属于一类
边集相同的意思是,他们所连接的左边节点的数量和类型一模一样
属于相同类的节点权值相加,然后再取所有类的最大公约数,就是最后的答案了
证明:如果两个点属于一类,那么只要有其中一个点出现,另一个点肯定出现,这是显然的
所以,如果这些类的权值依次为$a,b,c$的话,$F(S)$只能取$a,b,c,a+b,a+c,b+c,a+b+c$,这些数的$gcd$是等于$gcd(a,b,c)$的
哈希的话,居然可以用vector直接哈希,这个我完全没想到
注意:vector需要排好序
long long 哈希:
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for (int i=a;i<=b;i++)
#define per(i,a,b) for (int i=b;i>=a;i--)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define SZ(x) ((int)(x).size()) typedef long long ll;
typedef vector<int> VI;
typedef pair<int,int> PII; const ll mod=1e5+7;
const int maxn=5e5+7; ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;} int g=1331,n,m;
map<ll,ll>ma;
ll powg[maxn];
VI ve[maxn];
ll c[maxn]; int main() {
powg[0]=1;
rep(i,1,maxn-1)powg[i]=powg[i-1]*g;
int T;
scanf("%d",&T);
while(T--){ scanf("%d %d",&n,&m);
rep(i,1,n)scanf("%lld",&c[i]);
rep(i,1,m){
int a,b;
scanf("%d %d",&a,&b);
ve[b].pb(a);
}
rep(i,1,n){
ll res=0;
for(auto j:ve[i])
res+=powg[j];
if(SZ(ve[i]))ma[res]+=c[i];
}
ll ans=0;
for(auto i:ma)
ans=__gcd(i.se,ans);
printf("%lld\n",ans);
ma.clear();
rep(i,1,n)ve[i].clear();
}
return 0;
}
vector哈希:
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for (int i=a;i<=b;i++)
#define per(i,a,b) for (int i=b;i>=a;i--)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define SZ(x) ((int)(x).size()) typedef long long ll;
typedef vector<int> VI;
typedef pair<int,int> PII; const ll mod=1e5+7;
const int maxn=5e5+7; ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;} ll c[maxn];
int n,m;
map<VI,ll>ma;
VI ve[maxn]; int main() {
int T;
scanf("%d",&T);
while(T--){ scanf("%d %d",&n,&m);
rep(i,1,n)scanf("%lld",&c[i]);
rep(i,1,m){
int a,b;
scanf("%d %d",&a,&b);
ve[b].pb(a);
}
rep(i,1,n){ if(SZ(ve[i])){
sort(ve[i].begin(),ve[i].end());
ma[ve[i]]+=c[i];
}
}
ll ans=0;
for(auto i:ma){
ans=gcd(i.se,ans);
// cout<<i.se<<endl;
}
printf("%lld\n",ans);
ma.clear();
rep(i,1,n)ve[i].clear();
}
return 0;
}
总结:
Codeforces Round #626 Div2 D,E的更多相关文章
- Codeforces Round #626 Div2 D. Present(位掩码,二分)
题目链接:https://codeforces.com/contest/1323/problem/D 题意:给了大小为4e5的数组a,其中1<=ai<=1e7.求所有点对和的异或和,即: ...
- Codeforces Round #539 div2
Codeforces Round #539 div2 abstract I 离散化三连 sort(pos.begin(), pos.end()); pos.erase(unique(pos.begin ...
- 【前行】◇第3站◇ Codeforces Round #512 Div2
[第3站]Codeforces Round #512 Div2 第三题莫名卡半天……一堆细节没处理,改一个发现还有一个……然后就炸了,罚了一啪啦时间 Rating又掉了……但是没什么,比上一次好多了: ...
- Codeforces Round#320 Div2 解题报告
Codeforces Round#320 Div2 先做个标题党,骗骗访问量,结束后再来写咯. codeforces 579A Raising Bacteria codeforces 579B Fin ...
- Codeforces Round #564(div2)
Codeforces Round #564(div2) 本来以为是送分场,结果成了送命场. 菜是原罪 A SB题,上来读不懂题就交WA了一发,代码就不粘了 B 简单构造 很明显,\(n*n\)的矩阵可 ...
- Codeforces Round #626 (Div. 2, based on Moscow Open Olympiad in Informatics)
A. Even Subset Sum Problem 题意 给出一串数,找到其中的一些数使得他们的和为偶数 题解 水题,找到一个偶数或者两个奇数就好了 代码 #include<iostream& ...
- Codeforces Round #361 div2
ProblemA(Codeforces Round 689A): 题意: 给一个手势, 问这个手势是否是唯一. 思路: 暴力, 模拟将这个手势上下左右移动一次看是否还在键盘上即可. 代码: #incl ...
- CodeForces Round 192 Div2
This is the first time I took part in Codeforces Competition.The only felt is that my IQ was contemp ...
- Codeforces Round #359 div2
Problem_A(CodeForces 686A): 题意: \[ 有n个输入, +\space d_i代表冰淇淋数目增加d_i个, -\space d_i表示某个孩纸需要d_i个, 如果你现在手里 ...
随机推荐
- sql server 用触发器记录增删改操作(转载)
数据库结构: CREATE TABLE [dbo].[cg_tz_log] ( [logid] int NOT NULL IDENTITY(1,1) , operate varchar(10), -- ...
- Kubernetes官方java客户端之七:patch操作
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...
- C中的dll 、lib和exe文件
参考:链接1 链接2 DLL 动态链接库(Dynamic Link Library,缩写为DLL),运行时加载是一个可以被其它应用程序共享的程序模块,其中封装了一些可以被共享的例程和资源.动态链接 ...
- 更改mysql的密码
mysql> set password for 'root'@'localhost' =PASSWORD('');Query OK, 0 rows affected (0.17 sec) mys ...
- 【函数分享】每日PHP函数分享(2021-1-19)
substr 函数返回字符串的一部分.注释:如果 start 参数是负数且 length 小于或等于 start,则 length 为 0. string substr (string $string ...
- LeetCode590. N叉树的后序遍历
题目 1 class Solution { 2 public: 3 vector<int>ans; 4 vector<int> postorder(Node* root) { ...
- Java高并发与多线程(四)-----锁
今天,我们开始Java高并发与多线程的第四篇,锁. 之前的三篇,基本上都是在讲一些概念性和基础性的东西,东西有点零碎,但是像文科科目一样,记住就好了. 但是本篇是高并发里面真正的基石,需要大量的理解和 ...
- uni-app 微信小程序 picker 三级联动
之前做过一个picker的三级联动功能,这里分享代码给大家 具体代码: // An highlighted block <template> <view> <picker ...
- uni-app开发经验分享七: 有关列表数据下拉加载方法的解析及记录
在使用uni.request获取后台数据时,我们往往碰到一个问题,列表的懒加载及数据实时更新,这里记录下我制作这类功能的方法. 问题描述:后台返回数据,前端需要进行10个为一组来分页,先显示前10个, ...
- k8s之共享存储概述以及演示
共享存储机制 k8s对有状态的容器应用或者需要对数据进行持久化的应用,在之前的篇章说过,可以将容器内的目录挂载到宿主机的容器目录或者emptyDir临时存储卷. 另外,k8s还开放了两个资源,分别是P ...