ゼロサムカウント


25

与えられたプログラムまたは機能を記述≥1 N ±1±2、±3±...±へ戻るソリューションの数N = 0。

n = 6の場合、解は存在しないため、答えは0です。n= 4の場合、解は2つありますので、答えは2です(2つの解は1-2-3 + 4 = -1 + 2 + 3-4 = 0)。

これは、OEISシーケンスA063865です。入力/出力の例は次のとおりです。

n       a(n)
1       0
2       0
3       2
4       2
5       0
6       0
7       8
8       14
9       0
10      0
11      70
12      124
13      0
14      0
15      722
16      1314

バイト単位の最短コードが優先されます。



1
@ManishKunduうーん、それは私にとって可能なだまされやすいターゲットのように見えると思います、最後に「長さ」をタックするか、「合計でフィルターする」代わりに「合計してからカウントする」の代わりに答えを出す。
エリックアウトゴルファー

2
@EriktheOutgolfer私はその挑戦に気づいていませんでしたが、これに対する答えは実質的に異なる場合があります。たとえば私のものを見てください。
orlp

2
@ManishKunduこのチャレンジの違いを説明しました
...-orlp

2
はい、見ました。誤って自分の質問を打ち込んだのは残念ですが、反対の票を投じることを強いられるべきではありません。
デニス

回答:






5

C(gcc)、45 62 52 50バイト

f(n,r){n=n?f(n-1,r+n)+f(n-1,r-n):!r;}F(n){f(n,0);}

Kevin CruijssenのJava 8 回答のポート。

こちらからオンラインでお試しください。

コメントで提案された改善により、コードはclangでコンパイルされたときに機能しないという点まで未定義の動作を生成することに注意してください。

3バイトのゴルフをしてくれたeteneに感謝します。さらに10バイトのゴルフをしてくれたKevin Cruijssenに感謝します。さらに2バイトのゴルフをしてくれたChristophに感謝します。

ゴルフされていないバージョン:

f(n, r) { // recursive function - return type and parameter type are omitted, they default to int
    n = // instead of returning, we set n - dirty trick
        n ? // if n is not 0, recurse
        f(n-1,r+n) // +n
       +f(n-1,r-n) // -n
        !r; // else if r != 0 return 0 else return 1
}
F(n) { // function to start the recursion; again implicitly int(int)
    n = f(n, 0); // call the recursive function; this time we simply don't return
}

1
あなたは置き換えることにより、3つのバイトを剃り落とすことができr?0:1!r42バイト
-etene

2
の初期値を設定するためにここで追加の入力を行っているようですがr、これは許可されていません。
シャギー

1
@eteneまあ、ありがとうございます!
OOBalance

2
@KevinCruijssenの方が優れていますが、2番目n=も必要ありませんf(n,r){n=n?f(n-1,r+n)+f(n-1,r-n):!r;}F(n){f(n,0);}
クリストフ

2
@OOBalanceトリックは2の補数です。これはそれ-x = ~x+1を意味します~x = -x-1
クリストフ

5

05AB1E9 8バイト

バイトを保存してくれたエミグナに感謝します!

コード:

LæO·sLO¢

05AB1Eエンコードを使用します。オンラインでお試しください!

説明

L           # Create the list [1, 2, .., input]
 æ          # Compute the powerset of this list
  O         # Sum each list
   ·        # Double each element
    sLO     # Compute the sum of [1, 2, .., input]
       ¢    # Count the number of occurrences

4

MATL14 13バイト

[la]Z^G:!Y*~s

1バイトを節約してくれた@Giuseppeに感謝します!

オンラインでお試しください!または、すべてのテストケースを確認します

説明

n = 3例として考えてみましょう。スタックは上下逆に表示されます。つまり、最新が下に表示されます。

[la]   % Push array [1 -1]
       % STACK: [1 -1]
Z^     % Cartesian power with inplicit input n
       % STACK: [ 1  1  1
                  1  1 -1
                  1 -1  1
                  1 -1 -1
                 -1  1  1
                 -1  1 -1
                 -1 -1  1
                 -1 -1 -1]
G:     % Push n, range: gives [1 2 ... n]
       % STACK: [ 1  1  1
                  1  1 -1
                  1 -1  1
                  1 -1 -1
                 -1  1  1
                 -1  1 -1
                 -1 -1  1
                 -1 -1 -1],
                 [1  2  3]
!      % Transpose
       % STACK: [ 1  1  1
                  1  1 -1
                  1 -1  1
                  1 -1 -1
                 -1  1  1
                 -1  1 -1
                 -1 -1  1
                 -1 -1 -1],
                 [1
                  2
                  3]
Y*     % Matrix multiplication
       % STACK: [6
                 0
                 2
                -4
                 4
                -2
                 0
                -6]
~      % Logical negation
       % STACK: [0
                 1
                 0
                 0
                 0
                 0
                 1
                 0]
s      % Sum of vector. Implicit display
       % STACK: 2

4

ゼリー、8バイト

ŒPS€ċÆṁ$

オンラインでお試しください!

使い方

ŒPS€ċÆṁ$  Main link. Argument: n

ŒP        Take the powerset of [1, ..., n].
  S€      Take the sum of each subset.
       $  Combine the two links to the left into a monadic chain.
     Æṁ       Compute the median of the sums, i.e, (1 + ... + n)/2.
    ċ         Count the occurrences of the median.

3

Python 2、74バイト

def f(n):l=k=1;exec"l+=l<<n*k;k+=1;"*n;return(l>>n*n*-~n/4)%2**n*(~-n%4>1)

楽しい投稿、直接生成関数の計算の詳細。


3

オクターブ (通信パッケージ付き)、39バイト

@(n)sum((2*de2bi(0:2^n-1)-1)*(1:n)'==0)

オンラインでお試しください!

説明:

範囲0 ... n ^ 2-1を取り、それをバイナリに変換します。これにより、01のすべての組み合わせを持つ行列が得られます。2を乗算し、1を減算して、-1+1のすべての組み合わせの行列を取得します。

範囲1 ... nの内積を取り、±1±2 ...±nのすべての組み合わせを取得します。ゼロの数を数えます。

基本的に同じこと、同じバイト数:

@(n)nnz(~((2*de2bi(0:2^n-1)-1)*(1:n)'))


3

Python 2および3、50バイト

ほとんどの答えのような再帰的アプローチ:

f=lambda n,r=0:f(n-1,r+n)+f(n-1,r-n)if n else r==0

オンラインで試す

二重再帰呼び出しのバイト数が多すぎます...おそらく簡単にする方法があります。


3

Java 8、72 71 70バイト

n->f(0,n)int f(int r,int n){return n>0?f(r+n,--n)+f(r+~n,n):r==0?1:0;}

@ArnauldのJavaScript(ES6)回答のポート。@OlivierGrégoireの
おかげで-2バイト。

オンラインでお試しください。

説明:

n->                 // Method with integer parameter and integer return-type
  f(0,n)            //  Call the recursive method with 0 and this parameter

int f(int r,int n){ // Recursive method with integer as both two parameters and return-type
  return n>0?       //  If `n` is not 0 yet:
    f(r+n,--n)      //   Recursive call with `r+n` (and `n` lowered by 1 first with `--n`)
    +f(r+~n,n)      //   + Recursive call with `r-n` (and `n` also lowered by 1)
   :r==0?           //  Else-if `r` is 0
     1              //   Return 1
    :               //  Else:
     0;}            //   Return 0


3

Bash + GNUユーティリティ、63バイト

Bashはおそらく再帰関数でこれよりもうまくいくでしょうが、私はこの種のeval/ escape / expansionの怪物に抵抗することはできません:

p=eval\ printf\ %s
$p\\\\n \$[$($p \\\{+,-}{1..$1})]|grep -c ^0

オンラインでお試しください!


更新:bashが再帰関数でより良くなるとは思わない。これは、スコアが90の場合にできる最善の方法です。 eval地獄それはそうです。










1

Pyth、14 13バイト

lf!s.nT*F_BRS

ここで試してみてください

説明

lf!s.nT*F_BRS
            SQ  Take the list [1, ..., <implicit input>].
         _BR    Get the pairs [[1, -1], [2, -2], ...].
       *F       Take the Cartesian product.
 f!s.nT         Find the ones where the flattened sum is 0.
l               Take the length.


1

スタックス、9 バイト

è%é┐╬@₧╠¬

実行してデバッグする

これまで にゼリーに敗れた最短の回答の一つ

どの符号の合計がゼロになるかを明示的に確認するのはそれほどゴルフではないので、代わりにpowersetを取得し、n番目の三角数の半分の合計を持つpowersetのセットの数を確認します。この方法は、驚くべきことではありませんが、どの符号の合計がゼロになるかをチェックするのと同じ時間の複雑さです。

同等のASCII:

RS{|+Hmx|+#


0

J、28バイト

(*>:){1j3#1+//.@(*/)/@,.=@i.

OEISの別の定義を使用しa(n) = coefficient of x^(n(n+1)/4) in Product_{k=1..n} (1+x^k) if n = 0 or 3 mod 4 else a(n) = 0ます。

オンラインでお試しください!

説明

(*>:){1j3#1+//.@(*/)/@,.=@i.  Input: n
                          i.  Range [0, n)
                        =     Self-Classify. Forms an identity matrix of order n
          1           ,.      Stitch. Prepend 1 to each row
                    /         Reduce using
                                Convolution
                 */               Product table
           +//.                   Sum along anti-diagonals
      1j3#                    Copy each once, padding with 3 zeroes after
     {                        Index at n*(n+1)
  >:                            Increment n
 *                              Times n


0

Gol> <>、26バイト

:IFPlMF2K+}:@-}||0lMF$z+|h

オンラインでお試しください!または1〜16のテストケースを実行します。

使い方

:IFPlMF2K+}:@-}||0lMF$z+|h

Main outer loop
:IFPlMF ...... ||
:        Duplicate top; effectively generate two explicit zeroes
         Top is the loop counter `i`;
         the rest is the generated 2**i sums
 I       Take input as number
  F ........... |  Pop n and loop n times
   P     i++
    lM   Push stack length - 1, which is 2**(i-1)
      F ...... |   Loop 2**(i-1) times

Main inner loop: generate +i and -i from 2**(i-1) previous sums
2K+}:@-}
          Stack: [... x i]
2K        [... x i x i]    Copy top two
  +}      [x+i ... x i]    Add top two and move to the bottom
    :@    [x+i ... i i x]  Duplicate top and rotate top 3
      -}  [i-x x+i ... i]  Subtract and move to the bottom

Counting zeroes
0lMF$z+|h
0lM        Push zero (zero count) and 2**n (loop count)
   F...|   Loop 2**n times
    $z+    Swap top two; Take logical not; add to the count
        h  Print top as number and halt
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.