# 在超立方体上行走

9

a[m+1] = xor(a[m], 2^mod(a[m],n)).

## 测试用例

n   a[0]   Output
-----------------
3   0      6
14  0      50
5   6      8
17  3      346

## 规则

• 禁止出现标准漏洞
• 输出/输入可以采用任何合适的格式
• 您可以假设a[0]是有效的顶点

Luis Mendo

4

# 果冻，9个字节

%⁴2*^µÐḶL

%⁴2*^µÐḶL        A monadic link. Inputs: a_0. b also taken from command line.
%⁴                   a modulo b. ⁴ is second input, b.
2*                 Get 2 to that power
^                and bitwise xor with a.
µ             Start a new, monadic link (input: a_0)
ÐḶ             All elements of the cycle created when the preceding link
is applied repeatedly, starting with a_0.
L            Length.

2

# 哈斯克尔124

import Data.Bits
(y:z:w)%(x:s)|x==y||x==z=[i|(i,r)<-zip[1..]s,r==x]!!0|0<1=w%s
g n=(tail>>=(%)).iterate(\a->xor a\$2^mod a n)

g是计算答案的函数。给它n，然后a[0]它将把数字返回给您（请注意，n应将其定义为类型Int以避免类型歧义）。

1

## JavaScript（ES6），69个字节

(n,a)=>{g=m=>m^1<<m%n;for(c=1,b=a;(b=g(g(b)))!=(a=g(a));)c++;return c}

1

# MATL，38 37 28字节

xi`vt0)2y1G\^Z~yywP=fn~]2M1\$

### 说明

x       % take first input: n. Delete (gets copied into clipboard G)
i       % take second input: initial value of a
`       % do...while loop
v     %   concatenate all stack contents vertically
t0)   %   duplicate. Get last element of that array: current a
2     %   push 2
y     %   duplicate second-top element in stack: current a
1G    %   push first input (n)
\     %   a modulo n
^     %   2 raised to that
Z~    %   xor of that with current a
yy    %   duplicate top two elements in stack: array of old a's and new a
w     %   swap: move array of old a's to top
P     %   reverse that array. So first entry is most recent a (before current)
=f    %   indices of old values that equal current value. There may be 0 or 1
n~    %   is it empty?
]       % if so, continue with a new iteration
2M      % push array of indices. It contains exactly 1 index
1\$      % set 1 input for implicit display function, so it only displays the index

@lirtosiast真的！谢谢。编辑
Luis Mendo

1

## Pyth，22 17字节

Lx^2%bQbl.uyNuyGE

Lx^2%bQbl.uyNuyGE     Implicit: Q=first line n. E=second line a[0].
Lx^2%bQb              y = lambda b: do one iteration
Then
uyGE     Apply y until a previous result is found.
This makes sure we're in the cycle.
.uyN         Then apply y again until a previous result is found.
Keep all intermediate values but not the repeat.
l             Get the length; i.e. the length of the cycle.

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.