这题采用分治的思想

首先,根据最后一位是否为1,将数分为两个集合,  集合与集合之间的lowbit为1,

然后将每个集合内的元素,倒数第二位是否为1,将数分为两个集合,集合与集合之间的lowbit为2

以此类推。

首先为什么要将数组进行排序,排序的规则是根据数字二进制数,从右到左,第一个不同的数字进行排序,0左,1右   

 #include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <math.h>
using namespace std;
#pragma warning(disable:4996)
typedef long long LL;
const int INF = <<;
/* */
const int MOD = ;
int a[ + ];
LL ans;
bool cmp(LL a, LL b)
{
while (a || b)
{
if ((a & ) != (b & ))
return (a & ) < (b & );
a >>= ;
b >>= ;
}
return false;
} void dfs(int from, int to, int pos)
{
if (pos>) return;
if (from > to) return;
int i;
for ( i = from; i <= to; ++i)
{
//找到集合的分割点
if (a[i] & ( << pos))
break;
}
ans = ans + (i - from)*(to - i + )*( << pos) % MOD;
//递归求解分割出来的两个集合
dfs(from, i - , pos + );
dfs(i, to, pos + );
}
int main()
{
int t, n, i;
scanf("%d", &t);
for (int k = ; k <= t; ++k)
{
scanf("%d", &n);
for (i = ; i < n; ++i)
{
scanf("%d", &a[i]);
}
ans = ;
sort(a, a + n,cmp);
dfs(, n - , );
ans = ans * % MOD;
printf("Case #%d: %d\n",k, ans);
}
return ;
}

最新文章

  1. ABP理论学习之NHibernate集成
  2. MySQL源码分析以及目录结构 2
  3. junit单元测试中私有方法测试
  4. UWP/Win10新特性系列—UserConsentVerifier
  5. WaitForMultipleObject与MsgWaitForMultipleObjects用法
  6. css 之position用法详解
  7. Android开发之Menu组件
  8. jersey构建rest服务返回json数据
  9. 探讨VMP 2.12.3 导入表修复
  10. 发布Ext JS 5.1 beta版本
  11. 初学Python(二)
  12. 推理机Jess,Racer,Jena
  13. SQL Server DDL
  14. virtualenv+pyenv管理python工作环境
  15. Web网站配置Gzip,压缩js css文件
  16. 80x86的保护模式
  17. 使用ant对JS/CSS 进行压缩以提高网站性能
  18. Linux:回收循环创建的多个线程
  19. GC收集器种类
  20. DROP TABLE 恢复【一】

热门文章

  1. [置顶] 强大的JQuery
  2. (Android) Download Images by AsyncTask API
  3. Android学习-----如何使用sqlite对于后台数据交换,sqlite使用例程入门
  4. Swift调用Objective-C编写的代码(颜色选择器KKColorListPicker调用)
  5. Js实现select联动,option从数据库中读取
  6. SQLite/嵌入式数据库
  7. 第四题(迅雷笔试题):编写一个程序,开启3个线程,这3个线程的ID分别为A、B、C,每个线程将自己的ID在屏幕上打印10遍,要求输出结果必须按ABC的顺序显示;如:ABCABC….依次递推。
  8. Hibernate学习之createSQLQuery与createQuery的区别及使用
  9. 杭电acm阶段之理工大版
  10. 解决windows下的mysql匿名登陆无法使用mysql数据库的问题