这题采用分治的思想

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

    Processing.js Processing.js 1.4.1 released!

  2. MySQL 採用Xtrabackup对数据库进行全库备份

    1,xtrabackup简单介绍 关于数据库备份以及备份工具.參考:http://blog.itpub.net/26230597/viewspace-1460065/,这里来介绍xtrabackup已 ...

  3. H-index因素

    Problem Description Paper quality and quantity have long been used to measure a research's scientifi ...

  4. js基础语法(一)

    学习网站参考:http://www.w3school.com.cn/ 写入html输出: document.write('test out put'); 例子: http://www.w3school ...

  5. 运行Dos命令并得到dos的输出文本(使用管道函数CreatePipe和PeekNamedPipe)

    function RunDOS(const CommandLine: string): string;var  HRead, HWrite: THandle;  StartInfo: TStartup ...

  6. SQLite/嵌入式数据库

    SQLite/嵌入式数据库 的项目要么不使用数据库(一两个文配置文件就可以搞定),要么就会有很多的数据,用到 postgresql,操练sqlite的还没有.现在我有个自己的小测试例子,写个数据库对比 ...

  7. 生产者、消费者 C源码,gcc编译通过

    /*生产者.消费者*/ #include<stdio.h> #include<pthread.h> #define BUFFER_SIZE 16 /***struct prod ...

  8. ezw证件照芯片压缩算法

    相关网站:http://m.blog.csdn.net/blog/kimwu/12654517 http://blog.sina.com.cn/s/blog_4be751690100bsgb.html ...

  9. Python heapq 模块的实现 - A Geek's Page

    Python heapq 模块的实现 - A Geek's Page Python heapq 模块的实现

  10. Hawk-数据抓取工具

    Hawk-数据抓取工具:简明教程   Hawk: Advanced Crawler& ETL tool written in C#/WPF 1.软件介绍 HAWK是一种数据采集和清洗工具,依据 ...