#!/usr/bin/ruby #coding:utf-8 def egcd(x,y) return [x,1,0] if y==0 g,a,b=egcd(y,x%y) [g,b,a-x/y*b] end a,b=gets.split.map(&:to_i).sort if b==0 gets;p $<.count{|e| x,y=e.split.map(&:to_i) x==0&&y==0 } elsif a==0 gets;p $<.count{|e| x,y=e.split.map(&:to_i) x%b==0&&y%b==0 } else g=a.gcd b a/=g b/=g z=egcd(a,b) gets;p $<.count{|e| x,y=e.split.map{|e|e.to_i.abs}.sort next if x%g!=0 || y%g!=0 x/=g y/=g α=z[1]*x β=z[2]*x #(β-2d)A + (α-2b)B == y #2Ad + 2Bb == Aβ+Bα-y (a*β+b*α-y)%(2*a).gcd(2*b)==0 } end