νμ μνμ€λ μμμ μμ λ¬Έμλ₯Ό μμ νμ¬ λ€λ₯Έ μνμ€μμ μ»μ μμλ μνμ€μ
λλ€. μ λ³κ°μ λΉμ΄ μμ§ μμ μνμ€ 100
μ΄λ€ 0
, 1
, 00
, 10
, 100
. μ λ³κ°μ λΉμ΄ μμ§ μμ μνμ€κ° 1010
μλ€ 0
, 1
, 00
, 01
, 10
, 11
, 010
, 100
, 101
, 110
, 1010
.
μμ μ μλ₯Ό μ 곡νλ νλ‘κ·Έλ¨μ΄λ κΈ°λ₯μ μ°κΈ° N μ λ°μ΄λ리 νμ₯ λ³κ°μ λΉμ΄ μμ§ μμ μλΈ μνμ€μ μλ₯Ό λ°ν Nμ .
μ :λΆν° 4
μ΄λ€ 100
μ§, μ°λ¦¬λ κ·Έλ κ² λ€μ― λ³κ°μ λΉμ΄ μμ§ μμ μνμ€ μμ μλ€κ³ 보μλ€ f(4) = 5
. μμμμ N = 1 , μνμ€κ° μμλλ€ :
1, 3, 2, 5, 6, 5, 3, 7, 10, 11, 9, 8, 9, 7, 4, 9, 14, 17, 15, 16, 19, 17, 12
κ·Έλ¬λ νλ‘κ·Έλ¨μ μ΅μ λ¨Έμ μμ 1 μ΄ λ―Έλ§μ n <2 50 μΈμΉ λμ μλν΄μΌν©λλ€ . λͺ κ°μ§ ν° μ :
f(1099511627775) = 40
f(1099511627776) = 81
f(911188917558917) = 728765543
f(109260951837875) = 447464738
f(43765644099) = 5941674
λ΅λ³
νμ΄μ¬ 3 , 95 λ°μ΄νΈ 83 λ°μ΄νΈ
[Mr.XCoder λλΆμ -12 λ°μ΄νΈ :)]
def f(x):
v=[2,1];c=1
for i in bin(x)[3:]:k=int(i);c+=v[k];v[1-k]+=v[k]
return c
μ¨λΌμΈμΌλ‘ μ¬μ©ν΄λ³΄μμμ€!
μκ³ λ¦¬μ¦μ λν μ°Έκ³ μ¬ν. μκ³ λ¦¬μ¦μ μ£Όμ΄μ§ μμΉ (t)μμ λΉνΈμ μν΄ μ£Όμ΄μ§ κ³ μ ν μλΈ μνμ€μ μ¦λΆμ κ³μ°νλ€. 첫 λ²μ§Έ λΉνΈμ μ¦λΆμ νμ 1μ λλ€. κ·Έλ° λ€μ μκ³ λ¦¬μ¦μ λΉνΈ s (t)μ μνμ€λ₯Ό μ€ννκ³ μ¦λΆ v [s (t)]λ₯Ό μΆκ°ν©λλ€. κ° λ¨κ³μμ s (t), v [1-s (t)]μ 보μμ λν μ¦λΆμ v [1] + v [0]μΌλ‘ μ λ°μ΄νΈλ©λλ€. λ§μ§λ§ μ«μλ λͺ¨λ μ¦λΆμ ν©μ λλ€.
O (log2 (n))μμ μ€νν΄μΌν©λλ€. μ¬κΈ°μ nμ μ λ ₯ λ²νΈμ λλ€.
λ΅λ³
μλ° μ€ν¬λ¦½νΈ (ES6), 53 51 λ°μ΄νΈ
f=(n,r=~(a=[]))=>n<1?~r:f(n/2,r*2-~~a[n&=1],a[n]=r)
ν μ€νΈ μ¬λ‘
f=(n,r=~(a=[]))=>n<1?~r:f(n/2,r*2-~~a[n&=1],a[n]=r)
console.log(f(1099511627775)) // 40
console.log(f(1099511627776)) // 81
console.log(f(911188917558917)) // 728765543
console.log(f(109260951837875)) // 447464738
console.log(f(43765644099)) // 5941674
νμν λ° μ견
f = ( // f is a recursive function taking:
n, // n = integer
r = ~( // r = last result, initially set to -1
a = [] // and using a[] = last results for 0 and 1,
) // implicitly initialized to [0, 0]
) => //
n < 1 ? // if n is less than 1:
~r // we're done: return -(r + 1)
: // else:
f( // do a recursive call with:
n / 2, // n / 2
r * 2 - ~~a[n &= 1], // updated result = r * 2 - last result for this binary digit
a[n] = r // update last result for this binary digit
) // end of recursive call
λΉ μ¬κ· λ²μ , 63 λ°μ΄νΈ
@ThePirateBay λλΆμ 3 λ°μ΄νΈ μ μ½
s=>[...s.toString(2)].map(l=c=>l[p=r,r=r*2-~~l[c],c]=p,r=1)|r-1
ν μ€νΈ μ¬λ‘
let f =
s=>[...s.toString(2)].map(l=c=>l[p=r,r=r*2-~~l[c],c]=p,r=1)|r-1
console.log(f(1099511627775)) // 40
console.log(f(1099511627776)) // 81
console.log(f(911188917558917)) // 728765543
console.log(f(109260951837875)) // 447464738
console.log(f(43765644099)) // 5941674
λ΅λ³
νμ΄μ¬ 2 , 56 λ°μ΄νΈ
f=lambda x,a=1,b=1:x and f(x/2,a+~x%2*b,x%2*a+b)or a+b-2
μ¨λΌμΈμΌλ‘ μ¬μ©ν΄λ³΄μμμ€!
NofP μμ λ°©λ²μ μ¬μ© ν©λλ€.
λ°λ³΅μ μΌλ‘ 59 λ°μ΄νΈ :
x=input()
v=[1,1]
while x:v[x%2]=sum(v);x/=2
print sum(v)-2
λ΅λ³
μ €λ¦¬ , 10 λ°μ΄νΈ
B3;BSαΉΒ¦/βS
μ΄κ²μ @NofPμ μκ³ λ¦¬μ¦ μμ @xnorμ κ°μ μ μ¬μ© ν©λλ€ .
μ¨λΌμΈμΌλ‘ μ¬μ©ν΄λ³΄μμμ€!
λ°°κ²½
νμ (aλ 1 , β¦νλ nμ ) νμ λ μ΄μ§ μνμ€ μΌ. κ°κ°μ μμ΄ μλ μ μλ₯Ό λ€μ΄ K β€ N μ μ O K κ³ μ μνμ€μ μ (a 1 , β¦νλ K ) μ€ νλλ₯Ό λΉμ°κ±°λ λ¨λΆμ 1 , Zμ K μ΄λ€ κ³ μ μνμ€μ κ°μ λΉμ΄ μκ±°λ 0μΌλ‘ λλ©λλ€ .
λΆλͺ ν, O 0 Z = 0 = 1 , λΉ μνμ€μ μνμ€ λ§μ΄ λΉ μμλλ‘.
κ° μΈλ±μ€ k μ λν΄ (a 1 , β¦, a k ) μ μ΄ μλΈ μνμ€ μ λ o k + z k -1μ λλ€ ( 1μ λΉΌλ©΄ o k λ° z k κ° λΉ μνμ€λ₯Ό κ³μ° νλ€λ μ¬μ€μ΄ μ€λͺ λ©λλ€). λΉμ΄ μμ§ μμ μλΈ μνμ€ μ μ΄ μλ o k + z k -2 μ λλ€. μ΄ κ³Όμ λ o n + z n -2 λ₯Ό κ³μ°νλλ‘ μμ²ν©λλ€ .
λ§λ€λ K> 0 , μ°λ¦¬κ° ν μμλ μ»΄ν¨ν O K λ° Zμ K μ¬κ·. λ κ°μ§ κ²½μ°κ° μμ΅λλ€.
-
a k = 1
z k = z k-1 μ΄λ―λ‘ (a 1 , β¦, k-1 ) λ° (a 1 , β¦, k-1 , 1) μ 0μΌλ‘ λλλ λμΌν νμ μνμ€λ₯Ό κ°μ΅λλ€ .
μ κ°κ° O K β 1 μ λΉμ΄ μμ§ μμ μνμ€ (a 1 , β¦νλ K ) μ κ·Έ λ¨λΆ 1 μ°λ¦¬ λμ΄μ μ κ±° ν μμλ ν κ²μ νλλ₯Ό νλνκΈ° μν΄ O K-1 + Z K-1 β 1 κ°μ μλΈ μνμ€ (a 1 , β¦, k-1 ) . λ°λλ‘, μ²¨λΆ 1, νμμ κ°κ° O K-1 + Z K-1 β 1 κ° μ€ νλμ μμ΄ κ²°κ³Ό O K β 1 κ° μ μμ΄. λ°λΌμ o k -1 = oK-1 + Z K-1 β 1 κ³Ό O K = O K-1 + Z K-1 .
-
kλ 0 =
μ΄μ μ κ²½μ°μ λ§μ°¬κ°μ§λ‘ μ¬κ· 곡μ o k = o k-1 λ° z k = z k-1 + o k-1 μ μ»μ΅λλ€ .
μλ μ리
B3;BSαΉΒ¦/βS Main link. Argument: n (positive integer)
B Binary; convert n to base 2.
3; Prepend a 3.
B Binary; convert all integers in the resulting array to base 2, mapping
0 to [0], 1 to [1], and the prepended 3 to [1, 1].
/ Reduce the resulting array by the quicklink to the left, which will be
called with left argument [x, y] (integer pair) and right argument [j]
(either [0] or [1]).
Β¦ Sparse application.
S Compute the sum (x + y) and...
αΉ for each index in the right argument (i.e., for j)...
replace the element of [x, y] at that index with (x + y).
β Decrement both integers in the resulting pair.
S Take the sum.
λ΅λ³
05AB1E , 12 λ°μ΄νΈ
0ΒΈsbvDO>yΗ}O
μ¨λΌμΈμΌλ‘ μ¬μ©ν΄λ³΄μμμ€! μ€λͺ
: ASλ λ€λ₯Έ λ΅λ³μΌλ‘ μ§μ λ°μ΄λ리 λ¬Έμμ΄ μνμ€μ μλ₯Ό a..y0
1μμ λμ΄ λ°μ΄λ리 λ¬Έμμ΄μ λ²νΈμ λμΌν κ²μΌλ‘ a..y
Aμ λμ΄ μνλ λμ, 0
μ§μμν μλΈ μνμ€μ μ΄ μλ λ¬Έμμ΄ a..y
(κ°κ° 0
μ λ―Έμ¬λ₯Ό μ»μ )μ 1μ λν λ¬Έμμ΄ 0
. λ€λ₯Έ λ΅λ³κ³Ό λ¬λ¦¬ μ΄κΈ° μνλ₯Ό ꡬμ±νλ λ°μ΄νΈλ₯Ό μ μ₯νλ―λ‘ λΉ νμ μνμ€λ₯Ό ν¬ν¨νμ§ μμ΅λλ€.
0ΒΈs Push [0] under the input
b Convert the input to binary
v } Loop over the digits
D Duplicate the array
O Take the sum
> Increment
yΗ Replace the index corresponding to the binary digit
O Take the sum of the final array
λ΅λ³
μλ° 8, 97 λ°μ΄νΈ
n->f(n,1,1)long f(long n,long a,long b){return n>0?f(n/2,a+Math.floorMod(~n,2)*b,n%2*a+b):a+b-2;}
ν¬νΈ @xnor μ νμ΄μ¬ 2 λλ΅ μ°¨λ‘μ κ°μ , @NofP μ νμ΄μ¬ 3 λλ΅ β .
μ¬κΈ°μμ μλνμμμ€.
μ΄μ©λ©΄ μ νλ μκ° νκ·Έκ° μλ κ²μ΄ μ’μ κ²μ λλ€. μ²μμ λͺ¨λ νμ μνμ€λ₯Ό λ¬΄μ°¨λ³ μ²λ¦¬νκΈ° μν΄ λ€μμ μννκΈ° λλ¬Έμ λλ€.
import java.util.*;n->p(n.toString(n,2)).size()-1;Set p(String s){Set r=new HashSet();r.add("");if(s.isEmpty())return r;Set q=p(s.substring(1));r.addAll(q);for(Object o:q)r.add(""+s.charAt(0)+o);return r;}
μ¬κΈ°μμ μλνμμμ€.
μ΄λ κ²λ ν¨κ³Όκ° μμμ§λ§ λ§μ§λ§ μΈ κ°μ§ ν
μ€νΈ μ¬λ‘μλ λ무 μ€λ κ±Έλ Έμ΅λλ€. λ κΈΈλ€λ κ²μ λ§ν κ²λ μμ΅λλ€ ( 208 204 λ°μ΄νΈ ).
λ΅λ³
6502 κΈ°κ³ μ½λ (C64), 321 λ°μ΄νΈ
00 C0 20 FD AE A2 00 9D 4F C1 E8 20 73 00 90 F7 9D 4F C1 A0 FF C8 B9 4F C1 D0
FA A2 15 CA 88 30 0A B9 4F C1 29 0F 9D 4F C1 10 F2 A9 00 9D 4F C1 CA 10 F8 A9
00 A0 07 99 64 C1 88 10 FA A0 40 A2 6C 18 BD E4 C0 90 02 09 10 4A 9D E4 C0 E8
10 F2 A2 07 7E 64 C1 CA 10 FA 88 F0 13 A2 13 BD 50 C1 C9 08 30 05 E9 03 9D 50
C1 CA 10 F1 30 D1 A2 0F A9 00 9D 3F C1 CA D0 FA A9 01 8D 3F C1 8D 47 C1 A2 08
CA BD 64 C1 F0 FA A0 09 1E 64 C1 88 90 FA B0 0A CA 30 28 A0 08 1E 64 C1 90 04
A9 47 B0 02 A9 4F 8D AF C0 86 FE A2 F8 18 BD 47 C0 7D 4F C0 9D 47 C0 E8 D0 F4
A6 FE 88 D0 DC F0 D5 A2 F8 BD 47 C0 7D 4F C0 9D 6C C0 E8 D0 F4 AD 64 C1 E9 01
8D 64 C1 A2 F9 BD 6C C0 E9 00 9D 6C C0 E8 D0 F5 A0 15 A9 00 99 4E C1 88 D0 FA
A0 40 A2 13 BD 50 C1 C9 05 30 05 69 02 9D 50 C1 CA 10 F1 0E 64 C1 A2 F9 3E 6C
C0 E8 D0 FA A2 13 BD 50 C1 2A C9 10 29 0F 9D 50 C1 CA 10 F2 88 D0 D1 E0 14 F0
06 E8 BD 4F C1 F0 F6 09 30 99 4F C1 C8 E8 E0 15 F0 05 BD 4F C1 90 F0 A9 00 99
4F C1 A9 4F A0 C1 4C 1E AB
μ€λ₯ κ²μ¬ κΈ°λ₯μ΄μλ μ¨λΌμΈ λ°λͺ¨ (346 λ°μ΄νΈ)
μ©λ²: sys49152,[n]
μ sys49152,911188917558917
.
μκ° μ ν λ° ν μ€νΈ μ¬λ‘μλ 64 λΉνΈ μ«μλ‘ κ³μ°νλ μ루μ μ΄ νμνλ―λ‘ C64κ° β νλ κΈ°κ³ β μ λλ€ .
λ¬Όλ‘ ,μ΄ μ½λμ κ½€ νμ OSκ° μ 곡νμ§ μμ΅λλ€ μ무κ²λ 16 λΉνΈλ³΄λ€ λμ μ μμ λν΄. μ λ¦λ°μ΄ λΆλΆ : κ·Έκ²μ NofP μκ³ λ¦¬μ¦ resp μ λ λ€λ₯Έ ꡬν (μ½κ° μμ )μ λλ€ .xnorμ κ°μ λ λ³ν . μμ΄λμ΄ μ£Όμ μ κ°μ¬ν©λλ€;)
μ€λͺ
λ€μμ μκ³ λ¦¬μ¦μ μννλ κ΄λ ¨ λΆνμ λν μ£Όμ μ²λ¦¬ λ λΆν΄ λͺ©λ‘μ λλ€.
.C:c06c A2 0F LDX #$0F ; 15 bytes to clear
.C:c06e A9 00 LDA #$00
.C:c070 .clearloop:
.C:c070 9D 3F C1 STA .num_a,X
.C:c073 CA DEX
.C:c074 D0 FA BNE .clearloop
.C:c076 A9 01 LDA #$01 ; initialize num_a and num_b
.C:c078 8D 3F C1 STA .num_a ; to 1
.C:c07b 8D 47 C1 STA .num_b
.C:c07e A2 08 LDX #$08 ; 8 bytes of input to check,
.C:c080 .findmsb: ; start at most significant
.C:c080 CA DEX
.C:c081 BD 64 C1 LDA .nc_num,X
.C:c084 F0 FA BEQ .findmsb ; repeat until non-0 byte found
.C:c086 A0 09 LDY #$09 ; 8 bits to check (+1 for pre dec)
.C:c088 .findbit:
.C:c088 1E 64 C1 ASL .nc_num,X ; shift left, highest bit to carry
.C:c08b 88 DEY
.C:c08c 90 FA BCC .findbit ; bit was zero -> repeat
.C:c08e B0 0A BCS .loopentry ; jump into calculation loop
.C:c090 .mainloop:
.C:c090 CA DEX ; next byte
.C:c091 30 28 BMI .done ; index -1? -> done calculating
.C:c093 A0 08 LDY #$08 ; 8 bits to check
.C:c095 .bitloop:
.C:c095 1E 64 C1 ASL .nc_num,X ; shift left, highest bit to carry
.C:c098 90 04 BCC .tgt_b ; if 0, store addition result in num_b
.C:c09a .loopentry:
.C:c09a A9 47 LDA #$47
.C:c09c B0 02 BCS .tgt_a ; ... else store in num_a ...
.C:c09e .tgt_b:
.C:c09e A9 4F LDA #$4F
.C:c0a0 .tgt_a:
.C:c0a0 8D AF C0 STA $C0AF ; ... using self-modification.
.C:c0a3 86 FE STX $FE ; save byte index
.C:c0a5 A2 F8 LDX #$F8 ; index for adding
.C:c0a7 18 CLC
.C:c0a8 .addloop:
.C:c0a8 BD 47 C0 LDA $C047,X ; load byte from num_a
.C:c0ab 7D 4F C0 ADC $C04F,X ; add byte from num_b
.C:c0ae 9D 47 C0 STA $C047,X ; store to num_a or num_b
.C:c0b1 E8 INX ; next index
.C:c0b2 D0 F4 BNE .addloop ; done if index overflown
.C:c0b4 A6 FE LDX $FE ; restore byte index
.C:c0b6 88 DEY ; decrement bit index
.C:c0b7 D0 DC BNE .bitloop ; bits left in current byte -> repeat
.C:c0b9 F0 D5 BEQ .mainloop ; else repeat main loop
.C:c0bb .done:
.C:c0bb A2 F8 LDX #$F8 ; index for adding
.C:c0bd .addloop2:
.C:c0bd BD 47 C0 LDA $C047,X ; load byte from num_a
.C:c0c0 7D 4F C0 ADC $C04F,X ; add byte from num_b
.C:c0c3 9D 6C C0 STA $C06C,X ; store to nc_num (result)
.C:c0c6 E8 INX ; next index
.C:c0c7 D0 F4 BNE .addloop2 ; done if index overflown
.C:c0c9 AD 64 C1 LDA .nc_num ; load least significant result byte
.C:c0cc E9 01 SBC #$01 ; subtract 2 (1 + negated carry)
.C:c0ce 8D 64 C1 STA .nc_num ; store least significant result byte
.C:c0d1 A2 F9 LDX #$F9 ; index for subtract
.C:c0d3 .subloop:
.C:c0d3 BD 6C C0 LDA $C06C,X ; subtract 0 from all other bytes
.C:c0d6 E9 00 SBC #$00 ; for handling carry if necessary
.C:c0d8 9D 6C C0 STA $C06C,X
.C:c0db E8 INX
.C:c0dc D0 F5 BNE .subloop
λλ¨Έμ§λ μ / μΆλ ₯μ΄λ©° λλΈ λλΈ μκ³ λ¦¬μ¦μ μ¬μ©νμ¬ λ¬Έμμ΄κ³Ό 64 λΉνΈ λΆνΈμλ μ μ (little-endian) μ¬μ΄μμ λ³νλ©λλ€. κ΄μ¬μ΄ μμΌμλ€λ©΄, μ¬κΈ°μ μ€λ₯ κ²μ¬ κΈ°λ₯μ΄μλ λ²μ μ μ 체 μ΄μ λΈλ¦¬ μμ€κ° μμ΅λλ€. . βgolfedβλ²μ μ βgolfβλΈλμΉμ μμ΅λλ€.