HihoCoder - 1509 XOR Sorting

#1509 : 异或排序

时间限制:10000ms

单点时限:1000ms

内存限制:256MB

描述

给定一个长度为 n 的非负整数序列 a[1..n]

你需要求有多少个非负整数 S 满足以下两个条件:

(1).0 ≤ S < 260

(2).对于所有 1 ≤ i < n ,有 (a[i] xor S) ≤ (a[i+1] xor S)

输入

第一行一个正整数 n

第二行 n 个非负整数表示序列 a[1..n]

1 ≤ n ≤ 50

0 ≤ a[i] < 260

输出

一个非负正数,表示答案

样例输入
3
1 2 3
样例输出
288230376151711744

题目大意:

给一个数n,和n个数,(n<=50,a[i]<=2^60)有一个数s,让s xor a[i]<=s xor a[i+1]

分析

对于2个二进制数比如

00011110111

00011111111

它们前7位相同,第8位不同,如果让s满足s xor a[i]<=s xor a[i+1]

那么,他们第8位对应的s那一位也就确定了

这样将a[i]和a[i+1]遍历一遍,就可以得到s中一共有几位数已经被确定了

又因为s中一共有60位数,所以就可以得到s中有多少位不确定,每一个不确定的位有0和1两种可能,设不确定的位数为x,那么s一共有2的x次方种可能。

 1 #include<iostream>
 2 using namespace std;
 3 #include<queue>
 4 #include<cstdio>
 5 #include<map>
 6 #include<set>
 7 typedef long long LL;
 8 LL a[100];
 9 int b1[100],b2[100];
10 int v[100];
11 void deal(LL a,LL b)
12 {
13     for (int i=1;i<=60;i++)//求a的各2进制位
14     {
15         b1[i]=a%2;
16         a/=2;
17     }
18     for (int i=1;i<=60;i++)//求b的各2进制位
19     {
20         b2[i]=b%2;
21         b/=2;
22     }
23     int ans;
24     for (int i=60;i>=1;i--)//找到第一个不相同的位,存在ans里
25     {
26         if (b1[i]==b2[i]) continue;
27         ans=i;
28         break;
29     }
30     v[ans]=1;//将这一位做上标记
31 }
32 int main()
33 {
34     int n;
35     cin>>n;
36     for (int i=1;i<=n;i++)
37     {
38         scanf("%lld",&a[i]);
39     }
40     for (int i=1;i<=n-1;i++)
41     {
42         deal(a[i],a[i+1]);
43     }
44     LL ans=0;
45     for (int i=1;i<=60;i++)
46     {
47         if (v[i]) continue;
48         ans++;//计算不确定的位数共有几个
49     }
50     //cout<<ans<<endl;
51     ans=(LL)1<<ans;//答案为2^ans
52     cout<<ans<<endl;
53     return 0;
54 }

时间: 2024-08-25 19:19:28

HihoCoder - 1509 XOR Sorting的相关文章

hihocoder 1866 XOR

题面在这里 拆位分析一下就OK啦 /* y + (y xor x) */ #include<bits/stdc++.h> #define ll long long using namespace std; int n,k,ans=1; int main(){ scanf("%d%d",&n,&k); for(int i=0;i<=30;i++) if((1<<i)&n) ans<<=1; for(int i=29;i&g

hihoCoder.1509.异或排序(位运算 思路)

题目链接 \(Description\) 给定长为\(n\)的序列\(A\).求有多少\(S\),满足\(0\leq S<2^{60}\),且对于所有\(i\in[1,n-1]\),\(a[i]^{\wedge}S\leq a[i+1]^{\wedge}S\). \(n\leq20\). \(Solution\) 考虑对于\(a,b\),有哪些\(S\)满足\(a^{\wedge}S\leq b^{\wedge}S\). 找出\(a^{\wedge}b\)的最高位\(1\)(也就是\(a,b\)

[hihoCoder]1509_异或排序

链接:https://hihocoder.com/problemset/problem/1509 题意:异或排序 给定一个序列a[],给定一个函数 f(i) = a[i] XOR S, S为常数 问有多少个 0 <= S < 2^60 使得函数 f 为单调不减函数 (既然是中文题估计都能看得懂) 题解: 水题 ( 啪 x ) 由于每一个数都有一个二进制表示,由数论知识我们知道比较两个数是从高位开始比较的,当比较到某一位发现一个数该位为1而另一个数该位为0时就知道前面那个数一定比后面那个要大,而

HihoCoder 1236 Scores - bitset - 分块

Kyle is a student of Programming Monkey Elementary School. Just as others, he is deeply concerned with his grades. Last month, the school held an examination including five subjects, without any doubt, Kyle got a perfect score in every single subject

hihoCoder太阁最新面经算法竞赛15

Link: http://hihocoder.com/contest/hihointerview24 题目1 : Boarding Passes 时间限制:10000ms 单点时限:1000ms 内存限制:256MB   描述 Long long ago you took a crazy trip around the world. You can not remember which cities did you start and finish the trip.  Luckily you

2015多校 #5 1002 MZL&#39;s xor

MZL's xor Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1509    Accepted Submission(s): 619 Problem Description MZL loves xor very much.Now he gets an array A.The length of A is n.He wants to

hihocoder #1178 : 计数 暴力

#1178 : 计数 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://hihocoder.com/problemset/problem/1178 Description Rowdark是一个邪恶的魔法师.在他阅读大巫术师Lich的传记时,他发现一类黑魔法来召唤远古生物,鱼丸. 魔法n能召唤类型i鱼丸当且仅当i能够被表示为x xor n*x对于某个正整数x和固定的n. Rowdark想知道类型为[L,R]之间的鱼丸有多少种能被魔法n召唤. I

bzoj 2337: [HNOI2011]XOR和路径

Description Input Output Sample Input Sample Output HINT Source Day2 终于把这个史前遗留的坑给填了... 首先异或的话由位无关性,可以按位处理... 那么对于每一位,设f[i]表示从i出发第一次到达n且xor和为1的概率,out[i]为i的出边,那么转移就比较容易了... if(w(i,j)&xxx) f[i]+=(1-f[j)/out[i];// 这条边该位为1,需要xor上0,xor和才为1 else f[i]+=f[j]/

【BZOJ】2337: [HNOI2011]XOR和路径

[算法]期望+高斯消元 [题解]因为异或不能和期望同时运算,所以必须转为加乘 考虑拆位,那么对于边权为1取反,边权为0不变. E(x)表示从x出发到n的路径xor期望. 对于点x,有E(x)=Σ(1-E(y))(边权1)||E(y)(边权0)/t[x]  t[x]为x的度. 那么有n个方程,整体乘上t[x]确保精度,右项E(x)移到左边--方程可以各种变形. 每次计算完后*(1<<k)就是贡献. 逆推的原因在于n不能重复经过,而1能重复经过,所以如果计算"来源"不能计算n,