Codefoeces 734F

题目大意:

给定两个正整数序列\(b,c\)构造一个正整数序列\(a\)使其满足

\[\left\{
\begin{array}{}
b_i=(a_i\text{ and }a_1)+(a_i\text{ and }a_2)+...+(a_i\text{ and }a_n) \\
c_i = (a_i\text{ or }a_1)+(a_i\text{ or }a_2)+...+(a_i\text{ or }a_n)
\end{array}
\right.
\]

题解:

我们有这么一个性质\((a_i \text{ and } b_i)+(a_i\text{ or }b_i)= a+b\)

所以我们把所有的\(b_i,c_i\)加和,得到

\[\left\{
\begin{array}{}
b_1+c_1 = na_1 + \sum{a_i} \\
b_2+c_2 = na_2 + \sum{a_i} \\
... \\\
b_n+c_n = na_n + \sum{a_i}
\end{array}
\right.
\]

然后再把所有的式子加和得到

\(\sum{a_i} = \frac{\sum{b_i} + \sum{c_i}}{2n}\)

然后我们可以利用这个解出所有的\(a_i\)

我也不知道为什么必须对每一位都特殊判定,不过不判定的话下面这组数据过不去

1

3

5

应该是不可行,但是如果不进行数位判定的话会输出4...

哪位大神可以来救救我。。。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
template<typename T>inline void read(T &x){
x=0;char ch;bool flag = false;
while(ch=getchar(),ch<'!');if(ch == '-') ch=getchar(),flag = true;
while(x=10*x+ch-'0',ch=getchar(),ch>'!');if(flag) x=-x;
}
inline int cat_max(const int &a,const int &b){return a>b ? a:b;}
inline int cat_min(const int &a,const int &b){return a<b ? a:b;}
const int maxn = 200010;
ll b[maxn],c[maxn],a[maxn];
int cnt[32][2];
int main(){
int n;read(n);
ll sum = 0;
for(int i=1;i<=n;++i) read(b[i]),sum += b[i];
for(int i=1;i<=n;++i) read(c[i]),sum += c[i];
if(sum % (n<<1) != 0) return puts("-1");
sum /= (n<<1);
for(int i=1;i<=n;++i){
ll x = b[i] + c[i] - sum;
if(x < 0 || (x%n != 0)) return puts("-1");
a[i] = x/n;
for(int j=0;j<31;++j) ++cnt[j][(a[i]>>j)&1];
}
for(int i=1;i<=n;++i){
for(int j=0;j<31;++j) b[i] -= ((a[i]>>j)&1)*cnt[j][1]<<j;
if(b[i] != 0) return puts("-1");
}
for(int i=1;i<=n;++i){
printf("%I64d",a[i]);
if(i != n) putchar(' ');
else putchar('\n');
}
getchar();getchar();
return 0;
}

Codefoeces 734F. Anton and School 数学的更多相关文章

  1. Codeforces 734F Anton and School(位运算)

    [题目链接] http://codeforces.com/problemset/problem/734/F [题目大意] 给出数列b和数列c,求数列a,如果不存在则输出-1 [题解] 我们发现: bi ...

  2. CodeForces 734F Anton and School

    位运算. 两个数的和:$A+B=(AandB)+(AorB)$,那么$b[i]+c[i]=n*a[i]+suma$.可以解出一组解,然后再按位统计贡献验证一下. #pragma comment(lin ...

  3. Codeforces Round #404 (Div. 2) D. Anton and School - 2 数学

    D. Anton and School - 2 题目连接: http://codeforces.com/contest/785/problem/D Description As you probabl ...

  4. CodeFoeces 1215 D Ticket Game(数学,思维)

    CodeFoeces 1215 D Ticket Game 题目大意 M和B轮流玩游戏(每一轮M先手 现在给出一个长度为偶数的串,包含字符'?'和数字 现在两人依次在'?'上填数字\(0\)~\(9\ ...

  5. 【codeforces 734F】Anton and School

    [题目链接]:http://codeforces.com/problemset/problem/734/F [题意] 给你两个数组b和c; 然后让你找出一个非负数组a满足题中所给关系; [题解] 有个 ...

  6. C. Anton and Fairy Tale(数学推式子)

    \(数学题,式子并不难推,但边界是真的烦\) \(\color{Red}{Ⅰ.其实可以发现,当m>=n时,每次都可以粮食补到n,所以一定是在第n天消耗完毕}\) \(\color{Purple} ...

  7. PDF分享:国外优秀数学教材选评

    <国外优秀数学教材选评>推荐书目下载 具体内容请查看原内容: http://www.library.fudan.edu.cn/wjzx/list/373-1-20.htm 或者http:/ ...

  8. 数学思想:为何我们把 x²读作x平方

    要弄清楚这个问题,我们得先认识一个人.古希腊大数学家 欧多克索斯,其在整个古代仅次于阿基米德,是一位天文学家.医生.几何学家.立法家和地理学家. 为何我们把 x²读作x平方呢? 古希腊时代,越来越多的 ...

  9. 速算1/Sqrt(x)背后的数学原理

    概述 平方根倒数速算法,是用于快速计算1/Sqrt(x)的值的一种算法,在这里x需取符合IEEE 754标准格式的32位正浮点数.让我们先来看这段代码: float Q_rsqrt( float nu ...

随机推荐

  1. js错误: Unexpected number in JSON at position 2792 value里面有双引号怎么解决

    源头  出现这个报错提示,大家从错误就可以看的出来,这就是json的错误,一般来说都是json格式出现了错误,本人遇到比较多的情况就是json字符串里面出现了一些会影响json格式的符号,这次出现这个 ...

  2. Windows 命令集合

    查看端口占用 查看8080端口使用情况: C:\>netstat -aon|findstr "8080" 结果:TCP    0.0.0.0:8080           0 ...

  3. 近期公共祖先(LCA)——离线Tarjan算法+并查集优化

    一. 离线Tarjan算法 LCA问题(lowest common ancestors):在一个有根树T中.两个节点和 e&sig=3136f1d5fcf75709d9ac882bd8cfe0 ...

  4. Using ADO.NET Data Service

    ADO.NET Data Service是随同Visual Studio 2008 SP1提供的用于构建在数据对象模型 (如EF-DEMX, LINQ-DBML) 之时来快速提供企业网内外的轻量级数据 ...

  5. 点击textbox弹出对话框,返回弹出对话框的值

    主要是在父页面使用 function PopupWindow() {            window.open(url, "", "status=no,resizab ...

  6. 【BZOJ4520】[Cqoi2016]K远点对 kd-tree+堆

    [BZOJ4520][Cqoi2016]K远点对 Description 已知平面内 N 个点的坐标,求欧氏距离下的第 K 远点对. Input 输入文件第一行为用空格隔开的两个整数 N, K.接下来 ...

  7. CDH使用Solr实现HBase二级索引

      一.为什么要使用Solr做二级索引二.实时查询方案三.部署流程3.1 安装HBase.Solr3.2 增加HBase复制功能3.3创建相应的 SolrCloud 集合3.4 创建 Lily HBa ...

  8. java中随机生成汉字

    main方法中使用: //随机生成100个汉字 String ss=""; for(int i=0;i<100;i++){ ss+=getChinese(i); } Syst ...

  9. 九度OJ 1165:字符串匹配 (模式匹配)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3219 解决:1149 题目描述: 读入数据string[ ],然后读入一个短字符串.要求查找string[ ]中和短字符串的所有匹配,输出 ...

  10. 关于gcc

    1 the architecture of gcc 2 自己编译gcc时的 --build --host --target选项的含义和用法 <1> --build 执行本次的gcc编译的主 ...