http://www.contesthunter.org/contest/CH Round %2364 - MFOI杯水题欢乐赛 day1/Solve

Solve

CH Round #64 - MFOI杯水题欢乐赛 day1

题目描述

给定 n 和 X0,X1,...,Xn-1,求解 Y0,Y1,...,Yn-1,其中:

f(x) 等于把 x 写成二进制后 1 的个数,比如说:

f(0)=0 , f(1)=1 , f(4)=1 , f(7)=3

其中  表示二进制下的按位异或运算。

请依次输出 Y0,Y1,...,Yn-1 对  取模的结果。

输入格式

输入第一行为一个正整数 n

输入第二行为 n 个非负整数,第 i 个数表示 Xi-1

输出格式

输出仅一行 n 个非负整数,第 i 个数表示 Yi-1 对  取模的结果。

样例输入

3

1 1 1

样例输出

2 3 3

数据范围及约定

测试数据点 n Xi 特殊性质
1 <= 10 <= 10
2 <= 100 <= 100
3 <= 1000 <= 1000
4 = 65536 <= 109 所有 Ai 都相同
5 <= 105 <= 109 所有 Ai 都相同
6,7,8,9,10 <= 105 <= 109

分析:

方法1,

  可以先预处理W[j][0/1] 表示第j位是0/1的i的i的Xi值和,然后对于每位i,根据其二进制表示来累加答案即可。

  时间复杂度O(n log n) , 可以拿下全部测试点。

方法2.

考虑把n增加为2的幂,并令新增加的数字为0.

然后先把n == 2时转移矩阵写出来:

0 1
1 0

可以发现矩阵大概是这样的;

A A + T
A + T A

其中A的边长为2的幂,T为全1矩阵。

如果对于所有的边长为2的幂的矩阵,都能像这样分解的话,那么是不是就可以分治了?

首先,对于2 * 2 的矩阵来说,很显然是满足条件的。

假设对于2^k * 2 ^k的矩阵来说是满足条件的,那么考虑2 ^(k + 1) * 2 ^(k + 1)的矩阵,其左上角就是原来的2^k * 2 ^k的那个矩阵,设为A,显然对于所有的0 <= i < 2^k和0 <=j < 2 ^k,都有:

f((i+2k)⊕j)=f(i⊕j)+1

是不是说明其左上角的矩阵等于A + T ?

同理:右上角和右下角也是满足条件的。

于是就可以分治的去做了。

时间复杂度:O(n log n),可以拿下所有测试点。

AC代码:

代码很严谨,包括每个字符。。。orz

 #include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; #define N 100000 + 5
#define Mod 1000000007 int n, W[][]; inline int getint()
{
char ch = '\n';
for (; ch != '-' && (ch > '' || ch < ''); ch = getchar()) ;
int f = ch == '-' ? - : ;
int res = ch == '-' ? : ch - '';
for (ch = getchar(); ch >= '' && ch <= ''; ch = getchar())
res = (res << ) + (res << ) + ch - '';
return res * f;
} inline int Inc(int a, int b)
{
return a + b - (a + b >= Mod ? Mod : );
} int main()
{
//#ifndef ONLINE_JUDGE
// freopen("solve.in", "r", stdin);
// freopen("solve.out", "w", stdout);
// #endif n = getint();
int d = (int) log2(n);
for (int i = ; i < n; i ++)
{
int t = getint();
for (int j = ; j <= d; j ++)
W[j][((i >> j) & )] = Inc(W[j][((i >> j) & )], t);
}
for (int i = ; i < n; i ++)
{
int res = ;
for (int j = ; j <= d; j ++)
res = Inc(res, W[j][((i >> j) + ) & ]);
printf("%d%c", res, (i == n - ) ? '\n' : ' ');
} // #ifndef ONLINE_JUDGE
// fclose(stdin);
// fclose(stdout);
// #endif
return ;
}

contesthunter CH Round #64 - MFOI杯水题欢乐赛day1 solve的更多相关文章

  1. CH Round #48 - Streaming #3 (NOIP模拟赛Day1)

    A.数三角形 题目:http://www.contesthunter.org/contest/CH%20Round%20%2348%20-%20Streaming%20%233%20(NOIP模拟赛D ...

  2. 队爷的讲学计划 CH Round #59 - OrzCC杯NOIP模拟赛day1

    题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的讲学计划 题解:刚开始理解题意理解了好半天,然后发 ...

  3. 队爷的Au Plan CH Round #59 - OrzCC杯NOIP模拟赛day1

    题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的Au%20Plan 题解:看了题之后觉得肯定是DP ...

  4. 队爷的新书 CH Round #59 - OrzCC杯NOIP模拟赛day1

    题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的新书 题解:看到这题就想到了 poetize 的封 ...

  5. CH Round #54 - Streaming #5 (NOIP模拟赛Day1)

    A.珠 题目:http://ch.ezoj.tk/contest/CH%20Round%20%2354%20-%20Streaming%20%235%20(NOIP模拟赛Day1)/珠 题解:sb题, ...

  6. CH Round #54 - Streaming #5 (NOIP模拟赛Day1)解题报告

    最近参加了很多CH上的比赛呢~Rating--了..题目各种跪烂.各种膜拜大神OTZZZ T1珠 描述 萌蛋有n颗珠子,每一颗珠子都写有一个数字.萌蛋把它们用线串成了环.我们称一个数字串是有趣的,当且 ...

  7. CH Round #54 - Streaming #5 (NOIP模拟赛Day1)(被虐瞎)

    http://ch.ezoj.tk/contest/CH%20Round%20%2354%20-%20Streaming%20%235%20%28NOIP%E6%A8%A1%E6%8B%9F%E8%B ...

  8. CH Round #59 - OrzCC杯NOIP模拟赛day1

    第一题:队爷的新书 题意简述:给定n个闭区间,求出一个数p使它与包含它的区间数的积最大,输出这个积. 分析:使用一个差分数组g,每个区间[l,r],l位置加1,r+1的位置减1,从前往后统计,得到对于 ...

  9. 【强联通分量缩点】【最长路】【spfa】CH Round #59 - OrzCC杯NOIP模拟赛day1 队爷的讲学计划

    10分算法:对于城市网络为一条单向链的数据, 20分算法:对于n<=20的数据,暴力搜出所有的可能路径. 结合以上可以得到30分. 60分算法:分析题意可得使者会带着去的城市也就是这个城市所在强 ...

随机推荐

  1. Summary of java stream classes

    Java’s stream classes are good for streaming sequences of bytes, but they’re not good for streaming ...

  2. Python 虚拟环境Virtualenv

    本人也是Python爱好者,众所周知,Python扩展多,每次为了测试,安装各种各样的扩展,这样导致本地的Python环境非常混乱,就有人想到搞个隔离环境  和 本地环境没有关系,随时可以删除这个隔离 ...

  3. java并发编程-线程池的使用

    参考文章:http://www.cnblogs.com/dolphin0520/p/3932921.html 深入剖析线程池实现原理 将从下面几个方面讲解: 1.线程池状态 2.任务的执行 3.线程池 ...

  4. C++ 安全字符串拼接

    #include <stdio.h> #include <stdint.h> #include <stdarg.h> #if defined(__GNUC__) # ...

  5. php--纯静态和伪静态的区别与关系

    先前说了什么是纯静态和伪静态,现在介绍一下他们的区别? 首先肯定的是纯静态和伪静态都是SEO的产物,但纯静态和伪静态还是有很大区别的.纯静态是生成真实的HTML页面保存到服务器端,用户访问时直接访问这 ...

  6. background:transparent的作用

    background的属性值 background : background-color | background-image | background-repeat | background-att ...

  7. Intersecting Lines---poj1269(求两直线的位置关系)

    题目链接:http://poj.org/problem?id=1269 题意:给你两条直线上的任意不同的两点,然后求两条直线的位置关系,如果相交于一点输出该点坐标; #include<iostr ...

  8. JS之setAttribute和getAttribute

    1.ele.getAttribute(attributeName); 返回元素的指定属性值,如果元素没有该属性,则返回null 2.ele.setAttribute(attributeName,val ...

  9. C#中jQuery Ajax实例(一)

    目标:在aspx页面输入两参数,传到后台.cs代码,在无刷新显示到前台 下面是我的Ajax异步传值的第一个实例 1.前台html代码: <html xmlns="http://www. ...

  10. LeetCode Shortest Word Distance II

    原题链接在这里:https://leetcode.com/problems/shortest-word-distance-ii/ 题目: This is a follow up of Shortest ...