gym 101170 Problem H Hamiltonian Hypercube(找规律?)

题目链接

Problem HHamiltonian Hypercube

Hypercube graphs are fascinatingly regular, hence you have devoted a lot of time studying themathematics related to them. The vertices of a hypercube graph of dimensionn are all binarystrings of lengthn, and two vertices are connected if they differ in a single position. There aremany interesting relationships between hypercube graphs and error-correcting code.

One such relationship concerns then-bit Gray Code, which is an ordering of the binary stringsof lengthn, defined recursively as follows. The sequence of words in then-bit code first consistsof the words of the(n1)-bit code, each prepended by a0, followed by the same words inreverse order, each prepended by a1. The1-bit Gray Code just consists of a0 and a1. Forexample the3-bit Gray Code is the following sequence:

000,001,011,010,110,111,101,100
Now, the n-bit Gray Code forms a Hamiltonian path in then-dimensional hypercube, i.e., a path

page17image9632

that visits every vertex exactly once (see Figure H.1).

page17image10560 page17image10720 page17image10880 page17image11304 page17image11464 page17image11888 page17image12048 page17image12208 page17image12632 page17image12792 page17image12952 page17image13112 page17image13272

010

000

110

100

011

001

111

101

page17image16224 page17image16384 page17image16544 page17image16704 page17image17128 page17image17288 page17image17448 page17image17872 page17image18032 page17image18192 page17image18352 page17image18512 page17image18672 page17image19096

Figure H.1: The3-dimensional hypercube and the Hamiltonian path corresponding to the3-bitGray Code.

You wonder how many vertices there are between the vertices0n(nzeros) and1n(nones) onthat path. Obviously it will be somewhere between2n11and 2n2, since in general0nis thefirst vertex, and1nis somewhere in the second half of the path. After finding an elegant answerto this question you ask yourself whether you can generalise the answer by writing a programthat can determine the number of vertices between two arbitrary vertices of the hypercube, inthe path corresponding to the Gray Code.

Input

The input consists of a single line, containing:

one integern (1n60), the dimension of the hypercube

two binary stringsa andb, both of lengthn, wherea appears beforeb in then-bit GrayCode.

NWERC 2016 Problem H: Hamiltonian Hypercube15

Output


page18image1384

Output the number of code words betweena andb in then-bit Gray Code.Sample Input 1 Sample Output 1

Sample Input 2 Sample Output 2

3 001 111

3

3 110 100

2



题解:

由样例可发现,110到000的距离为4.

假设一个码字为s,定义a[i]=a[i-1]^(s[i]-'0');

那么s到0...00的距离为a作为二进制的十进制数。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define fi first
#define se second
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const int maxn=60+10;
char s1[maxn],s2[maxn];
int a[maxn];
ll getpos(char *s,int n)
{
    a[0]=s[0]-'0';
    rep(i,1,n) a[i]=a[i-1]^(s[i]-'0');
    ll ans=0;
    rep(i,0,n) ans=ans*2+a[i];
    return ans;
}
int main()
{
    int n;
    scanf("%d%s%s",&n,s1,s2);
    ll p1=getpos(s1,n),p2=getpos(s2,n);
    printf("%lld\n",p2-p1-1);
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值