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个, 如果你现在手里 ...
随机推荐
- Java线程安全与锁优化,锁消除,锁粗化,锁升级
线程安全的定义 来自<Java高并发实战>"当多个线程访问一个对象的时候,如果不用考虑这些线程在运行时环境下的调度和交替执行,也不需要进行额外的同步,或者在调用方法的时候进行任何 ...
- MySQL45讲:一条update语句是怎样执行的
首先创建一张表: create table T(ID int primary key,c int); 如果要更新ID=2这行+1:应该这样写 update T set c=c+1 where ID=2 ...
- 比较Power BI和Tableau,好比用奔驰对比奥迪
经常会有人问Power BI和Tableau的区别,好吧,为了非IT专业的能看懂,咱们就用车,奔驰和奥迪来对比一下.因为他们确实有太多相似之处. 所以Power BI VS Tableau,就相当于国 ...
- MySQL查询优化之 index 索引的分类和使用
索引的分类 主键索引 (PRIMARY KEY) 唯一的标识符, 主键不可重复, 只能有一列作为主键 唯一索引 (Unique KEY) 避免重复的列出现, 唯一索引可以重复, 多个列都可以标识为唯一 ...
- 【Linux】如何查找命令及历史记录history
如何查找命令及历史记录 文章目录 如何查找命令及历史记录 1.如何找到一个命令 2.命令的历史记录 3.一些实用的快捷键 4.小结 5.参考资料 如何找到一个命令.命令的历史记录.一些实用的快捷键.总 ...
- 【Oracle】查看当前连接数和最大连接数
查看当前数据库连接数 select count(*) from v$session where username is not null; select count(*) from v$process ...
- webpack知识点整理
作用域 es6里模块化的写法 会存在的问题,变量.方法名字雷同,外部文件调用的时候出现问题 如 a.js里 var a='susan' b.js里 var a='jack' 问题解决方案,添加包裹 a ...
- html简单基础
标签语法 标签的语法: <标签名 属性1="属性值1" 属性2="属性值2"-->内容部分</标签名> <标签名 属性1=&quo ...
- .net code+vue 文件上传
后端技术 .net code 官方文档 https://docs.microsoft.com/zh-cn/aspnet/core/mvc/models/file-uploads?view=aspnet ...
- Python干货:了解元组与列表的使用和区别
元组是 Python 对象的集合,跟列表十分相似.下面进行简单的对比. 列表与元组 1.python中的列表list是变量,而元组tuple是常量. 列表:是使用方括号[],元组:则是使用圆括号() ...