查找两首或更多歌曲的交集的算法

Spe*_*oat 7 c algorithm

让我们说我们有一堆收音机,每个收音机一遍又一遍地播放同一首歌.是否可以同步所有收音机中的所有歌曲?我们能找到一个从一开始就听到所有歌曲的时间吗?

为简单起见,我们会说我们只有两个无线电.

我有以下公式:

c和z表示以秒为单位的歌曲长度.a和x表示歌曲中的当前位置(以秒为单位)S表示C和Z同步的时间.(当两首歌同时开始时)

例如 :

Song 1
a = 17 : the time before the song ends.
b = 8 : the rest of the song.
c = a + b which is the full song in seconds.
And
Song 2 
x = 8 : the time before the song ends.
y = 9 : the rest of the song.
z = 8 + 9 which is the full song in seconds.

Song 1 : a + ( a + b) => S
Song 2 : x +(( x + y ) × n) => S

Song 1 : 17 + ( 17 + 8) => 42
Song 2 : 8 + ((8 + 9)) = 25
So in order to synchronize song 2 with song 1 we have to multiply (x + y)    
by two and add x to it.

Song 2 : 8 + ((8 + 9) x 2) => 42

So S = 42 and so the two songs will synchronize after 42 seconds.
Run Code Online (Sandbox Code Playgroud)

现在,第一个例子是最简单的例子.对于其他情况,我必须将z和c乘以两个以上,以便为它们获得适当的S.

我有一些其他的输入,我试图提出一个算法,将为我返回S,但我没有运气.

这是我到目前为止提出的:

c = a + b
a = 16
b = 4
c = 20
s = 216
Run Code Online (Sandbox Code Playgroud)

z = x + y
x = 12
y = 5
z = 17
s = 216
S is the LCM of c and z
Run Code Online (Sandbox Code Playgroud)

在第一个例子中,S就是这样找到的:

s = x +(z × n)
n = ( s ? x ) ÷ b
12 + ( 17 × 12) = 216
Run Code Online (Sandbox Code Playgroud)

s = a + (c × n)
n = ( s ? a ) ÷ b
16 + ( 20 × 10 ) = 216
Run Code Online (Sandbox Code Playgroud)

我想出了下面的两个公式基于S的值.但我需要找出一种方法来找到没有实际使用S的n.换句话说,我需要找出一种方法来找到我应该乘以多少次(a + b)乘n和(x + y)乘n得到S.

n = ( s ? a ) ÷ b
S = x + ( y × n)
Run Code Online (Sandbox Code Playgroud)

但是这些公式显然不会起作用,因为它们需要S.而我们不能使用它,因为这应该是我试图提出的公式的结果.

以下是一些计算的其他示例:

a2 = 52
b2 = 4
c2 = 56
s2 = 276

x2 = 60
y2 = 12
z2 = 72
s2 = 276
Run Code Online (Sandbox Code Playgroud)

这是一种永远不会同步的情况:

A1 = 14
B1 = 4
C1 = 18
S1 = Never synchronizes

A2 = 19
B2 = 5
C2 = 24
S2 = Never synchronizes
Run Code Online (Sandbox Code Playgroud)

以下是歌曲已经同步的情况:

情况1

A2 = 17  
B2 = 0 
C2 = 17 
S4 = 0

A3 = 25  
B3 = 0 
C4 = 25  
S4 = 0
Run Code Online (Sandbox Code Playgroud)

案例2

A4 = 0 
B4 =  13  
C4 = 13  
S4 = 0


A5 = 0 
B5 = 21 
C5 = 21  
S5 = 0
Run Code Online (Sandbox Code Playgroud)

我正在考虑使用最小公倍数,但我不确定如何在这种情况下实现它,或者它是否是解决此问题的正确方法.

如果有超过2首歌曲,我想要提出的算法也应该有用.例如,为3或4首歌曲找到S.

这个算法的主要问题是判断两首歌是否同步,计算本身并不那么难.你能帮我吗 ?提前致谢

Dav*_*tat 4

c和的最小公倍数z是歌曲同步的连续时间之间的间隔(如果它们同步的话)。这意味着,如果我们可以确定一个时间,我们可以通过添加(或减去)LCM 的倍数来找到其余的时间。要找到这个时间(实际上是 LCM),请使用扩展欧几里得算法来查找T, U满足方程的整数

\n\n
 (c - a) + T*c = (z - x) + U*z\n
Run Code Online (Sandbox Code Playgroud)\n\n

这相当于替换V = -U

\n\n
 T*c + V*z = (c - a) - (z - x).\n
Run Code Online (Sandbox Code Playgroud)\n\n

具体来说,求c和的 GCD z,检查它是否整除(c - a) - (z - x),然后将 B\xc3\xa9zout 系数乘以((c - a) - (z - x))/GCD(c, z)

\n