bestcoder44#1002
这题采用分治的思想
首先,根据最后一位是否为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的更多相关文章
- Bestcoder#5 1002
Bestcoder#5 1002 Poor MitsuiTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- acm 1002 算法设计
最近突然想往算法方向走走,做了做航电acm的几道题 二话不说,开始 航电acm 1002 题主要是处理长数据的问题,算法原理比较简单,就是用字符数组代替int,因为int太短需要处理的数据较长 下面是 ...
- BestCoder Round 69 Div 2 1001&& 1002 || HDU 5610 && 5611
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5610 如果杠铃总质量是奇数直接impossible 接着就考验耐心和仔细周全的考虑了.在WA了三次后终于发 ...
- 1002. A+B for Polynomials (25)
题目链接:https://www.patest.cn/contests/pat-a-practise/1002 原题如下: This time, you are supposed to find A+ ...
- 字符串专题:map POJ 1002
第一次用到是在‘校内赛总结’扫地那道题里面,大同小异 map<string,int>str 可以专用做做字符串的匹配之类的处理 string donser; str [donser]++ ...
- 1002 Phone Numbers 解题报告
1002. Phone Numbers Time limit: 2.0 secondMemory limit: 64 MB In the present world you frequently me ...
- HOJ 1001: A+B; 1002: A+B+C
两道水题,用来熟悉 HOJ 的提交系统. 1001:输入两个整数 A, B (0 <= A,B <= 10),输出 A+B. #include <iostream> using ...
- 杭电acm 1002 大数模板(一)
从杭电第一题开始A,发现做到1002就不会了,经过几天时间终于A出来了,顺便整理了一下关于大数的东西 其实这是刘汝佳老师在<算法竞赛 经典入门 第二版> 中所讲的模板,代码原封不动写上的, ...
- codevs 1002 搭桥
codevs 第一道题 先贴描述 1002 搭桥 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Description ...
随机推荐
- ThinkPHP 3 的CURD管理用户信息 修改和删除
本节课大纲: 一.ThinkPHP 3 的CURD管理用户信息 http://localhost:8080/thinkphp/index.php/User/index 访问User类的index方法 ...
- Visual Studio的SDK配置
Visual Studio的SDK Visual Studio 6.0自带的SDK是1998年的,目录为C:\Program Files\Microsoft Visual Studio\VC98\,这 ...
- drupal进入不了后台时候的解决办法,作者使用drush方案,已验证
drupal把正在用的主题不小心删了,怎么进后台? 方法一: 去variable表里把默认主题换了 方法二: ?q=user 登录到管理区,开启简洁连接使用user(网站根目录下面) admin/ap ...
- 使用Xcode无法发布程序(Archive按钮一直为灰色不可点击)
问题现象:想在Xcode中把代码编译发布成ipa程序,但“Product”->“Archive”按钮一直不可使用. 解决办法:目前的运行配置是使用模拟器,改成“iOS Device”即可 ...
- apache的开源项目-模板引擎(Velocity)(转)
然后修改conf文件下的server.xml文件,在server.xml里的 <Connector port="8080" .... />字段后 ...
- linux 怎么使用一个c/c++库
linux 怎么使用一个c/c++库 使用一个库,库有动态和静态之分. 静态库只要能够编译通过,运行时就没有提示缺少这个库可能,因为把这个库就是这个使用者的一部分了.动态库.两部分.1 库信息的注册 ...
- 终于懂了:TControl.Perform是有返回值的,且看VCL框架如何利用消息的返回值(全部例子都在这里)——它的存在仅仅是为了方便复用消息的返回值
代码如下: function TControl.Perform(Msg: Cardinal; WParam, LParam: Longint): Longint; var Message: TMess ...
- mysql+ssh整合样例,附源代码下载
项目引用jar下载:http://download.csdn.net/detail/adam_zs/7262727 项目源代码下载地址:http://download.csdn.net/detail/ ...
- 千万别用模板给的list.size()巨坑
要用!list.empty()取代i<list.size();否则就不会AC了.
- c#常见stream操作
原文: c#常见stream操作 常见并常用的stream一共有 文件流(FileStream), 内存流(MemoryStream), 压缩流(GZipStream), 加密流(CrypToStre ...