这题采用分治的思想

首先,根据最后一位是否为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 ;
}

bestcoder44#1002的更多相关文章

  1. Bestcoder#5 1002

    Bestcoder#5 1002 Poor MitsuiTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (J ...

  2. acm 1002 算法设计

    最近突然想往算法方向走走,做了做航电acm的几道题 二话不说,开始 航电acm 1002 题主要是处理长数据的问题,算法原理比较简单,就是用字符数组代替int,因为int太短需要处理的数据较长 下面是 ...

  3. BestCoder Round 69 Div 2 1001&& 1002 || HDU 5610 && 5611

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5610 如果杠铃总质量是奇数直接impossible 接着就考验耐心和仔细周全的考虑了.在WA了三次后终于发 ...

  4. 1002. A+B for Polynomials (25)

    题目链接:https://www.patest.cn/contests/pat-a-practise/1002 原题如下: This time, you are supposed to find A+ ...

  5. 字符串专题:map POJ 1002

    第一次用到是在‘校内赛总结’扫地那道题里面,大同小异 map<string,int>str 可以专用做做字符串的匹配之类的处理 string donser; str [donser]++ ...

  6. 1002 Phone Numbers 解题报告

    1002. Phone Numbers Time limit: 2.0 secondMemory limit: 64 MB In the present world you frequently me ...

  7. HOJ 1001: A+B; 1002: A+B+C

    两道水题,用来熟悉 HOJ 的提交系统. 1001:输入两个整数 A, B (0 <= A,B <= 10),输出 A+B. #include <iostream> using ...

  8. 杭电acm 1002 大数模板(一)

    从杭电第一题开始A,发现做到1002就不会了,经过几天时间终于A出来了,顺便整理了一下关于大数的东西 其实这是刘汝佳老师在<算法竞赛 经典入门 第二版> 中所讲的模板,代码原封不动写上的, ...

  9. codevs 1002 搭桥

    codevs 第一道题 先贴描述 1002 搭桥  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解  查看运行结果     题目描述 Description ...

随机推荐

  1. oracle系统包——dbms_random用法及order by 小结(转)

    dbms_random是一个可以生成随机数值或者字符串的程序包. 这个包有initialize().seed().terminate().value().normal().random().strin ...

  2. javascript实现图片无缝滚动(scrollLeft的使用方法介绍)

    <!DOCTYPE html > <html> <head> <meta http-equiv="Content-Type" conten ...

  3. 管道实现进程间通讯 、WaitNamedPipe

    一.管道实现进程间通讯 主要的理论知识 1.什么是管道以及分类 管道是两个头的东西,每一个头各连接一个进程或者同一个进程的不同代码,依照管道的类别分有两种管道,匿名的和命名的:依照管道的传输方向分也能 ...

  4. Swift调用Objective-C编写的代码(颜色选择器KKColorListPicker调用)

    在Swift项目中,我们可以导入任意用Objective-C写的框架,代码库等.下面以Swift调用Objective-C编写的颜色选择器KKColorListPicker为例. 效果图如下:     ...

  5. 一int考虑什么类型的多少字节

    一int表示的字节数? 问题是,我们经常得到的答案4. 但int究竟占多少个字节,却跟你的机器环境有关. As you can see, the typical data type sizes mat ...

  6. 关于Thread类中三个interrupt方法的研究与学习(转)

    先看三个方法原型: public void interrupt(): public boolean isInterrupted(): public static boolean interrupted ...

  7. C++ 直方图匹配算法代码

    /*-------------------------------------------------------------------------*/ // 函数名称: histeq() // 传 ...

  8. 破解phpjm.net加密,解密程序,全部公布

    原文:破解phpjm.net加密,解密程序,全部公布 2014-05-23更新: 很久没人找我解密了,看来这加密已过时,现公布我这边最新的解密工具. 若有解不出的可联系qq: 267014855 (不 ...

  9. 欧舒丹 L'Occitane 活力清泉保湿面霜 - 男士护肤 - 香港草莓网StrawberryNET.com

    欧舒丹 L'Occitane 活力清泉保湿面霜 - 男士护肤 - 香港草莓网StrawberryNET.com 欧舒丹 活力清泉保湿面霜 50ml/1.7oz

  10. [LeetCode]Swap Nodes in Pairs 成对交换

    Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2-& ...