hdu 5270 ZYB loves Xor II
2015-06-30 05:06
218 查看
题目链接
此类数论题大多按位考虑,显然暴力会超时。
不妨对(a+b)的第i位做考虑,将a,b都模2^(i+1),显然在(a+b)∈[2^i,2^(i+1))∪[3*2^i,2^(i+2))时该位为1.
所以我们只要求大于某个数的(a+b)的个数即可
值得注意的是,为了在排序时降低复杂度,可以让mod值从大到小然后归并排序,具体见代码。
此类数论题大多按位考虑,显然暴力会超时。
不妨对(a+b)的第i位做考虑,将a,b都模2^(i+1),显然在(a+b)∈[2^i,2^(i+1))∪[3*2^i,2^(i+2))时该位为1.
所以我们只要求大于某个数的(a+b)的个数即可
值得注意的是,为了在排序时降低复杂度,可以让mod值从大到小然后归并排序,具体见代码。
#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #include <string> #include <math.h> #include <stdlib.h> #include <time.h> using namespace std; typedef unsigned long long ll; const int maxn=(int)1e5+5; int n; ll a[maxn],b[maxn],c[maxn]; void merge (ll *p,ll mod) { //在给定的mod下归并排序 int i,j,k,m; for(i=1;i<=n;i++) { c[i]=p[i]; if(p[i]>=mod) break; } m=j=i; for(i=j;i<=n;i++) c[i]=p[i]%mod; i=1;c[n+1]=mod<<1; for(k=1;k<=n;k++) { if(c[i]<c[j]&&i<m) p[k]=c[i++]; else p[k]=c[j++]; } } ll bigthan (ll m) { //大于m的数对对数 int i,j=n;ll ans=0; for (i=1;i<=n;i++) { while ((a[i]+b[j])>=m&&j>0) j--; ans+=n-j; } return ans; } bool get (ll mod) { //考虑mod位是否为1 merge (a,mod<<1); merge (b,mod<<1); ll ans = bigthan(mod) + bigthan(3*mod) - bigthan(mod<<1); if ( ans%2 ) return true; return false; } int main () { // freopen("aa.txt","r",stdin); int cas,T,i;ll mod,ans; scanf("%d",&T); for(cas=1;cas<=T;cas++) { scanf("%d",&n); for(i=1;i<=n;i++) scanf("%I64d",a+i); for(i=1;i<=n;i++) scanf("%I64d",b+i); sort(a+1,a+n+1);sort(b+1,b+n+1); ans=0;mod=(ll)1<<61; while(mod) { if (get(mod)) ans+=mod; mod=mod>>1; } cout<<"Case #"<<cas<<": "<<ans<<endl; } }
相关文章推荐
- LeetCode "Roman to Integer"
- ORACLE 如何查询被锁定表及如何解锁释放session
- LeetCode "Integer to Roman"
- 点赞和吐糟Adblock Plus~进阶教程
- House Robber II
- 【Leetcode】【hard】Binary Tree Postorder Traversal
- House Robber
- PMC Flashtec + Memblaze PBlaze 4 = 性能惊人的NVM Express SSD!
- UML期末复习题——2.5:System Sequence Diagram & Post-condition
- summary ranges
- CentOS 6.5配置nfs服务
- CentOS 6.5配置nfs服务
- linux操作系下RAR的使用
- linux操作系下RAR的使用
- BLOB二进制对象(blob.c/h)
- BLOB二进制对象(blob.c/h)
- Tomato-Dualwan的强大功能
- Tomato-Dualwan的强大功能
- 崇拜无线路由天线增益功率的 兄弟看过来
- 崇拜无线路由天线增益功率的 兄弟看过来