贝组定理以及应用_leetcode365. Water and Jug Problem

定理:

贝祖定理是代数几何中一个定理,其内容是若设a,b是整数,则存在整数x,y,使得ax+by=gcd(a,b),(a,b)代表最大公因数,则设a,b是不全为零的整数,则存在整数x,y,使得ax+by=(a,b)。

验证推导:

 

365Water and Jug Problem

You are given two jugs with capacities x and y litres. There is an infinite amount of water supply available. You need to determine whether it is possible to measure exactly z litres using these two jugs.

If z liters of water is measurable, you must have z liters of water contained within one or both buckets by the end.

Operations allowed:

  • Fill any of the jugs completely with water.
  • Empty any of the jugs.
  • Pour water from one jug into another till the other jug is completely full or the first jug itself is empty.

用贝组公式可以证明,所有符合条件的z值均为(x,y)最大公约数的倍数。

(即,xa + yb = gcd(x,y)*k,其中gcd()为x,y的最大公约数,a、b为整数,a、b不全为0)

阅读更多
文章标签: 算法
个人分类: 笔记 打砖
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭