计算N位整数乘法溢出


16

给定一个正整数N,输出整数对的数量,0 <= a <= b < 2**N使a*b >= 2**N

规则

  • 您可以假定该N值小于或等于您语言中整数的最大位宽(例如,对于C,N不超过3264,具体取决于计算机的体系结构)。如果您的语言能够处理任意宽度的整数,则没有上限N

测试用例

1 0
2 3
3 19
4 96
5 437
6 1876
7 7804
8 31904
9 129170
10 520135
11 2088143
12 8369175
13 33512744
14 134128704
15 536681553
16 2147082274

注意:我正在努力生成更大的测试用例。我的暴力手段确实很慢。
Mego

@ user202729您正在通过不遵循a <= b条件来复制一些对。
Mego

1
一些更多的测试用例:{0, 3, 19, 96, 437, 1876, 7804, 31904, 129170, 520135, 2088143, 8369175, 33512744, 134128704, 536681553, 2147082274, 8589086503, 34357951447}
user202729

1
是否有可能针对此问题采用封闭式公式?我肯定错过了什么。

1
密切相关:en.wikipedia.org/wiki/Divisor_summatory_function。没有已知的封闭形式。
Orlp

Answers:


8

Python 2,75 68字节

n=input()
a=1<<n
s=~-a*a/2
x=y=0
while y<1:s+=y;x-=1;y=a/x-x
print s

在线尝试!

它以O(2 n / 2)运算而不是O(2 n)或O(2 n)运算运行,因此它适用于更大的输入。

(请注意,存在更快的O(2 n / 3)算法。)

1 0
2 3
3 19
4 96
5 437
6 1876
7 7804
8 31904
9 129170
10 520135
11 2088143
12 8369175
13 33512744
14 134128704
15 536681553
16 2147082274
17 8589086503
18 34357951447
19 137435198086
20 549747939928
21 2199006781125
22 8796058620153
23 35184300378083
24 140737339120148
25 562949643323164
26 2251799170232606
27 9007197921321922
28 36028794259096612
29 144115182370060793
30 576460740519709546
31 2305842984902014765
32 9223371986742908935
33 36893488044218344323
34 147573952377320833218
35 590295809922086353118
36 2361183240537767708679
37 9444732963897547996897
38 37778931859178411534913
39 151115727444080615797321
40 604462909791437463796926
41 2417851639196741979223299
42 9671406556850476410936322
43 38685626227531971124247499
44 154742504910394112443480979
45 618970019642121099638818409
46 2475880078569598086230187969
47 9903520314280668496162705117
48 39614081257127323838921620439
49 158456325028518790167805606609
50 633825300114094540502620959956
51 2535301200456417702087608942034
52 10141204801825751449333352568660
53 40564819207303170200956592005599
54 162259276829213015854387448792578
55 649037107316852746005301421147606
56 2596148429267412374169967907532731
57 10384593717069652326923914077600197
58 41538374868278615068076777292632146
59 166153499473114471992855423428749242
60 664613997892457911812090466987383188
61 2658455991569831695728843704244440740
62 10633823966279326881474627069404687424
63 42535295865117307726213589942623257944

非常好的改进!

2
你可以换x=0;y=0x=y=0
Cyoce

如果您也实施了2^{N/3}解决方案,那将非常酷。

1
完整的程序要短4个字节。
丹尼斯,

1
交换一些符号可以节省一个字节。tio.run/…–
丹尼斯(Dennis)

6

果冻12 10字节

2*ṖµṀ:«ạ¹S

在3秒内完成组合的测试用例。

在线尝试!

怎么运行的

2*ṖµṀ:«ạ¹S  Main link. Argument: n

2*          Yield 2ⁿ.
  Ṗ         Pop; yield A := [1, ..., 2ⁿ-1].
   µ        New monadic chain. Argument: A
    Ṁ       Maximum; yield 2ⁿ-1.
     :      Divide 2ⁿ-1 by each k in A.
      «     Dyadic minimum; yield min((2ⁿ-1)/k, k) for each k in A.
        ¹   Identity; yield A.
       ạ    Absolute difference; yield k - min((2ⁿ-1)/k, k) for each k in A.
         S  Take the sum.

5

MATL10 9字节

Wqt:&*R<z

在线尝试!

这会尝试所有可能的对。在线解释器中的内存不足,无法输入12

说明

W      % Implicitly input N. Push 2^N ('^' denotes power)
q      % Subtract 1: gives 2^N-1
t:     % Duplicate, range: pushes [0 1 2 ... 2^N-1]
&*     % Matrix of all pair-wise products
R      % Upper triangular part (including diagonal)
<      % Less-than comparison; element-wise. This gives true for products
       % that are greater than 2^N-1
z      % Number of non-zeros- Implicitly display


3

05AB1E13 12字节

-1字节感谢Emigna

oDL<ã€{ÙP›_O

在线尝试!

说明

oDL<ã€{ÙP›_O   Argument n
oD             2^n, push twice to the stack
  L<           List: [0 .. a]
    ã          Cartesian product with itself
     €{        Sort each element
       Ù       Uniquify
        P      Total product of each element
         ›_    Each element is greater or equal than 2^n
           O   Total sum

P这里就足够了。
Emigna

@Emigna,谢谢。我将编辑它
kalsowerus

3

JavaScript(ES7),70 65 60字节

n=>[...Array(k=2**n-1)].reduce(p=>p+=k<++i*i&&i-(k/i|0),i=0)

测试用例



1

Clojure,78个字节

#(count(for[l[(bit-shift-left 1 %)]a(range l)b(range a l):when(>=(* a b)l)]1))




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.