同等公式


10

中国剩余定理可以在模运算非常有用。

例如,考虑以下同余关系集:

套一致

对于这样的全等关系集,其中所有基数(3, 5, 7在此示例中)都是互质的,则在满足此关系的基数(在此示例中)与的乘积n之间将只有一个整数,1且其中的乘积等于底数(3*5*7 = 105在此示例中) 。

在此示例中,数字将为14,由以下公式生成:

式

其中2, 4, and 0,从上面的例子中给出。

70, 21, 15是公式的系数,它们取决于基数3, 5, 7

为了计算70, 21, 15一组基的公式的系数(在我们的示例中),我们使用以下过程。

对于a一组基准中的每个数字:

  1. 找到所有其他基数的乘积,记为P
  2. 找出除以P的余数的第一个倍数。这是的系数。1aa

例如,要计算对应于底数的系数3,我们找到所有其他底数的乘积(即5*7 = 35),然后找到该乘积1除以底数后剩下的余数的第一个倍数。

在这种情况下,35留下的剩余部分2时除以3,但35*2 = 70叶子的其余部分1时除以3,所以70对于对应的系数3。同样,3*7 = 21留下的剩余部分1时,除以53*5 = 15留下的剩余部分1时,除以7

简而言之

对于a一组数字中的每个数字:

  1. 找到所有其他数字的乘积,记为P
  2. 找出除以P的余数的第一个倍数。这是的系数。1aa

挑战

  • 对于两个或多个碱基的集合,挑战是找到对应系数的集合。
  • 基数的集合保证是成对的互质,每个基数保证大于1。
  • 您的输入是一个整数列表,作为输入[3,4,5]或以空格分隔的字符串"3 4 5",但是您的输入有效。
  • 您的输出应该是整数列表或以空格分隔的表示系数集的字符串。

测试用例

input             output
[3,5,7]           [70,21,15]
[2,3,5]           [15,10,6]
[3,4,5]           [40,45,36]
[3,4]             [4,9]
[2,3,5,7]         [105,70,126,120]
[40,27,11]        [9801,7480,6480]
[100,27,31]       [61101,49600,56700]
[16,27,25,49,11]  [363825,2371600,2794176,5583600,529200]

非常感谢Leaky Nun在编写此挑战中所提供的帮助。与往常一样,如果问题仍然不清楚,请告诉我。祝你好运,打高尔夫球!


输入中总是会有3个数字吗?
xnor

@xnor不。测试用例已编辑。
Sherlock16年

Answers:


5

Haskell,61 55 53字节

f x=[[p|p<-[0,product x`div`n..],p`mod`n==1]!!0|n<-x]

定义一个f接受输入并以整数列表形式输出的函数。

f x=[                                          |n<-x]  (1)
              product x                                (2)
                       `div`n                          (3)

首先,我们遍历输入(1)中的所有整数。然后,我们取所有整数(2)的乘积,然后除以n,就得到非n整数乘积P(3)。

           [0,               ..]                       (4)
     [p|p<-                     ,p`mod`n==1]           (5)
                                            !!0        (6)

然后,我们将结果(P)用作从零(4)开始的范围的步长值。我们取结果,[0, P, 2P, 3P, ...]并将其过滤为mod-n运算结果为一(5)的值。最后,我们采用第一个元素,这要归功于惰性评估(6)。

感谢@xnor 2个字节!


1
欢迎来到Haskell!我想你quot可以是div,也head可以是!!0
xnor

4

果冻11 7 个字节

P:*ÆṪ%P

在线尝试!验证所有测试用例

背景

Pa严格为正,互质整数。

问题中的两步过程-找到P的倍数,当将其除以a时剩下1的余数-可以用以下等式描述。

线性同余方程

根据欧拉-费马定理,我们有

欧拉-费马定理

其中φ表示Euler的totient函数。从这个结果,我们得出以下结论。

线性同余方程的公式

最后,由于挑战要求我们计算Px,因此我们注意到

最终结果的公式

其中Pa可计算为所有模的乘积。

怎么运行的

P:*ÆṪ%P  Main link. Argument: A (list of moduli)

P        Yield the product of all moduli.
 :       Divide the product by each modulus in A.
   ÆṪ    Apply Euler's totient function to each modulus.
  *      Raise each quotient to the totient of its denominator.
     %P  Compute the remainder of the powers and the product of all moduli.

2

J,13个字节

*/|5&p:^~*/%]

基于@Dennis的惊人答案

用法

一些测试用例将需要输入作为具有后缀的扩展整数x

   f =: */|5&p:^~*/%]
   f 3 5 7
70 21 15
   f 40x 27 11
9801 7480 6480
   f 16x 27 25 49 11
363825 2371600 2794176 5583600 529200

说明

*/|5&p:^~*/%]  Input: list B
         */    Reduce B using multiplication to get the product of the values
            ]  Identity function, get B
           %   Divide the product by each value in B, call the result M
   5&p:        Apply the totient function to each value in B, call the result P
       ^~      Raise each value in M to the power of its corresponding value in P
*/             The product of the values in B
  |            Compute each power modulo the product and return

在这里尝试。




1

果冻,14 13 字节

P:×"Ḷð%"’¬æ.ḷ

感谢@ Dennis节省了一个字节!

使用挑战说明中描述的过程。输入是一个基数列表,输出是一个系数列表。

在线尝试验证所有测试用例

说明

P:×"Ḷð%"’¬æ.ḷ  Input: a list B
P              Get the product of the list
 :             Divide it by each value in the B, call it M
    Ḷ          Get a range from 0 to k for k in B
  ×"           Vectorized multiply, find the multiples of each M
     ð         Start a new dyadic chain. Input: multiples of M and B
      %"       Vectorized modulo, find the remainders of each multiple by B
        ’      Decrement every value
               If the remainder was 1, decrementing would make it 0
         ¬     Logical NOT, zeros become one and everything else becomes 0
            ḷ  Get the multiples of M
          æ.   Find the dot product between the modified remainders and the multiples
               Return

1

JavaScript(ES6),80个字节

a.map(e=>[...Array(e).keys()].find(i=>p*i/e%e==1)*p/e,p=a.reduce((i,j)=>i*j))

我尝试了扩展的欧几里得算法,但它占用了98个字节:

a=>a.map(e=>(r(e,p/e)+e)%e*p/e,p=a.reduce((i,j)=>i*j),r=(a,b,o=0,l=1)=>b?r(b,a%b,t,o-l*(a/b|0)):o)

如果所有值都是素数,ES7可以用56个字节来完成:

a=>a.map(e=>(p/e%e)**(e-2)%e*p/e,p=a.reduce((i,j)=>i*j))

1

Python + SymPy,71个字节

from sympy import*
lambda x:[(prod(x)/n)**totient(n)%prod(x)for n in x]

这使用了我的Jelly答案中的算法。I / O采用SymPy编号列表的形式。


1

Python 2,87 84字节

该算法的简单实现。欢迎打高尔夫球。

a=input();p=1
for i in a:p*=i
print[p/i*j for i in a for j in range(i)if p/i*j%i==1]

一个完整的程序将节省3个字节。
丹尼斯

1

切达,64字节

n->n.map(i->(|>i).map(j->(k->k%i>1?0:k)(n.reduce((*))/i*j)).sum)

我应该添加一个.product.reduce((*))用于数组的...
Downgoat

0

GAP,51字节

GAP具有可以使用来计算激励示例的函数ChineseRem([2,5,7],[2,4,0]),但是仍然难以获得系数。我们可以通过使用列表(第n个位置为1,其他位置为零)作为第二个参数来获得第n个系数。因此,我们需要创建这些列表并将该函数应用于所有这些列表:

l->List(Basis(Integers^Size(l)),b->ChineseRem(l,b))

0

批处理,148字节

@set p=%*
@set/ap=%p: =*%
@for %%e in (%*)do @for /l %%i in (1,1,%%e)do @call:l %%e %%i
@exit/b
:l
@set/an=p/%1*%2,r=n%%%1
@if %r%==1 echo %n%

0

其实是14个位元组

这使用了Dennis的Jelly答案中的算法。即将基于我的Python答案的另一个答案。欢迎打高尔夫球。在线尝试!

;π;)♀\;♂▒@♀ⁿ♀%

怎么运行的

                 Implicit input a.
;                Duplicate a.
 π;)             Take product() of a, duplicate and rotate to bottom.
    ♀\           Integer divide the product by each element of a. Call this list b.
      ;♂▒        Take that list, duplicate, and get the totient of each element.
         @♀ⁿ     Swap and take pow(<item in b>, <corresponding totient>)
            ♀%   Modulo each item by the remaining duplicate product on the stack.
                 Implicit return.

另一个基于我的Python答案(22字节)的答案。欢迎打高尔夫球。在线尝试!

;π╖`╝╛╜\╛r*"╛@%1="£░`M

怎么运行的

            Implicit input a.
;π╖         Duplicate, take product of a, and save to register 0.
`...`M      Map over a.
  ╝           Save the item, b, in register 1.
  ╛╜\         product(a) // b. Call it P.
  ╛r          Take the range [0...b].
  *           Multiply even item in the range by P. Call this list x.
  "..."£░     Turn a string into a function f.
              Push values of [b] where f returns a truthy value.
    ╛@%         Push b, swap, and push <item in x> % b.
    1=          Does <item> % b == 1?
            Implicit return.
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.