【codeforces 734F】Anton and School
【题目链接】:http://codeforces.com/problemset/problem/734/F
【题意】
给你两个数组b和c;
然后让你找出一个非负数组a满足题中所给关系;
【题解】
有个结论吧;
(x and y + x or y)=x+y
然后把那n个式子全都加起来;
令d[i]=b[i]+c[i]···①;
则
d[i]=n∗a[i]+∑a⋅⋅⋅②
再把所有的①式加起来;
∑d=2∗n∑a⋅⋅⋅③
则由③式可得
∑a=∑d/(2∗n)
再代入②式
a[i]=(d[i]−∑d/(2∗n))/n
即
a[i]=(2∗n∗d[i]−∑d)/(2∗n2)
对于a[i]<0或a[i]不为整数的情况.
返回无解就好;
但是做完这些还不够;
还是可能错解..
还要验证一下得到的a[i]是不是能够按照那个规则得到b[i]和c[i];
但是直接强算是O(N2)的复杂度;
这里一位一位的算比较快;
我们算出所有的aj在第i位上上为1的个数kj;
然后就有
这里看我们可以通过Ai,j和kj快速获取Bi,j和Ci,j
即
然后根据得到的Ci,j和Bi,j;
根据位权,乘上相应的2的x次方;
然后累加起来;
看看c数组和b数组和所给的c、b数组是否相同.
【Number Of WA】
0
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
#define Open() freopen("F:\\rush.txt","r",stdin)
#define Close() ios::sync_with_stdio(0),cin.tie(0)
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 2e5+100;
int n;
LL b[N],c[N],d[N],sumd = 0,a[N],ans[N];
int B[N][65],C[N][65];
LL bin[65];
void out()
{
cout << -1 << endl;
exit(0);
}
int main()
{
//Open();
Close();//scanf,puts,printf not use
bin[0] = 1;
rep1(i,1,62)
bin[i] = bin[i-1]<<1;
cin >> n;
rep1(i,1,n)
cin >> b[i];
rep1(i,1,n)
{
cin >> c[i];
d[i] = b[i]+c[i];
sumd += d[i];
}
rep1(i,1,n)
{
a[i] = 1LL*2*n*d[i]-sumd;
LL temp = 1LL*2*n*n;
if (a[i]%temp!=0) out();
a[i]/=temp;
if (a[i]<0) out();
ans[i] = a[i];
}
LL ma = *max_element(a+1,a+1+n);
int limit = 0;
while (ma)
{
ma>>=1;
limit++;
}
rep1(i,0,limit-1)
{
int k = 0;
rep1(j,1,n)
if (a[j]&bin[i])
k++;
rep1(j,1,n)
{
if (a[j]&bin[i])
{
B[j][i]+=k,C[j][i]+=n;
}
else
//a[j]%1==0
C[j][i]+=k;
}
}
rep1(i,1,n)
{
LL tb = 0;
rep1(j,0,limit-1)
tb = tb+B[i][j]*bin[j];
LL tc = 0;
rep1(j,0,limit-1)
tc = tc+C[i][j]*bin[j];
if (tb!=b[i]||tc!=c[i])
out();
}
rep1(i,1,n)
cout << ans[i] <<' ';
//init??????
return 0;
}
【codeforces 734F】Anton and School的更多相关文章
- 【27.91%】【codeforces 734E】Anton and Tree
time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【29.89%】【codeforces 734D】Anton and Chess
time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【13.77%】【codeforces 734C】Anton and Making Potions
time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【81.37%】【codeforces 734B】Anton and Digits
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【77.39%】【codeforces 734A】Anton and Danik
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【25.00%】【codeforces 584E】Anton and Ira
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【codeforces 508B】Anton and currency you all know
[题目链接]:http://codeforces.com/contest/508/problem/B [题意] 给你一个奇数; 让你交换一次数字; 使得这个数字变成偶数; 要求偶数要最大; [题解] ...
- 【codeforces 785E】Anton and Permutation
[题目链接]:http://codeforces.com/problemset/problem/785/E [题意] 给你一个初始序列1..n顺序 然后每次让你交换任意两个位置上面的数字; 让你实时输 ...
- 【codeforces 785D】Anton and School - 2
[题目链接]:http://codeforces.com/contest/785/problem/D [题意] 给你一个长度为n的括号序列; 让你删掉若干个括号之后,整个序列变成前x个括号为左括号,后 ...
随机推荐
- SSM整合(spring,spirngmvc,mybatis)
整合思路 准备环境:导入jar包(spring mybatis dbcp连接池 mysql驱动包 log4j) 工程结构: --------------------------- 1. 整合 ...
- CF789C. Functions again
/* CF789C. Functions again http://codeforces.com/contest/789/problem/C 水题 题意:求数组中的连续和的最大值 */ #includ ...
- [Beginning SharePoint Designer 2010]Chapter5 主题和样式
本章概要: 1.什么是CSS 2.样式表的组成 3.如何管理CSS适应SPD的工具和特性 4.被SPS使用的关键的CSS类别 5.使用SPD应用样式到你的站点上
- WinSCP介绍、安装、使用
前言 如果说XManager通过Xshell.Xftp可以很方便的进行远程管理,那么PuTTY显然不能满足我们的需求,所以这也是今天要介绍的另外一个工具-WinSCP. 简介 WinSCP是一个Win ...
- HDU 4355
只能说感觉是三分吧,因为两端值肯定是最大的,而中间肯定存在一点使之最小,呃,,,,猜 的... #include <iostream> #include <cstdio> #i ...
- NEFU 109
n最大为2000000000(不知为什么OJ上是1000),若为判断2000000000是素数,则必有一个素数在sqrt(n)内,求出这个范围 的所有素数,其比最大数据小的n'的sqrt(n')也在这 ...
- poj 2396 Budget 边容量有上下界的最大流
题意: 给一个矩阵的每行和及每列和,在给一些行列或点的限制条件.求一个满足的矩阵. 分析: 转化为有上下界的网络流,注意等于也是一种上下界关系,然后用dinic算法. 代码: //poj 2396 / ...
- ftk学习记(首篇)
[ 声明:版权全部,欢迎转载,请勿用于商业用途. 联系信箱:feixiaoxing @163.com] 非常早之前就知道ftk了,当时主要是由于买了李先静的书,所以知道了这么一个项目.由于对这样的g ...
- php的异步并行扩展swoole
Swoole是PHP的异步并行扩展,有点像Node.js,但swoole既支持同步又支持异步,比node更强大.Swoole扩展是基于epoll高性能事件轮询,并且是多线程的,性能非常好. Swool ...
- mysql_udf_http(根据mysql表自动触发发送http请求)
下载 tar包wget http://mysql-udf-http.googlecode.com/files/mysql-udf-http-1.0.tar.gz解压tar -vzxf mysql-ud ...