1 const mo=1000000007; 2 var dp:array[-1000..1000]of int64; 3 x,y,a,b:array[-2000..2000]of longint; 4 fac,exf:array[0..2000]of int64; 5 q,time:array[-2000..2000]of longint; 6 inq:array[-1000..1000]of boolean; 7 cas,v,n1,m1,k,i,ax,bx,ay,by:longint; 8 9 procedure swap(var x,y:longint); 10 var t:longint; 11 begin 12 t:=x; x:=y; y:=t; 13 end; 14 15 procedure qsort(l,r:longint); 16 var i,j,mid1,mid2:longint; 17 begin 18 i:=l; j:=r; mid1:=a[(l+r)>>1]; mid2:=b[(l+r)>>1]; 19 repeat 20 while (mid1>a[i])or(mid1=a[i])and(mid2>b[i]) do inc(i); 21 while (mid1<a[j])or(mid1=a[j])and(mid2<b[j]) do dec(j); 22 if i<=j then 23 begin 24 swap(a[i],a[j]); 25 swap(b[i],b[j]); 26 inc(i); dec(j); 27 end; 28 until i>j; 29 if l<j then qsort(l,j); 30 if i<r then qsort(i,r); 31 end; 32 33 function c(x,y:longint):int64; 34 begin 35 exit(fac[x]*exf[y] mod mo*exf[x-y] mod mo); 36 end; 37 38 procedure do1; 39 var i,j,n,tx,ty,st,ed,m:longint; 40 begin 41 n:=0; 42 fillchar(dp,sizeof(dp),0); 43 fillchar(a,sizeof(a),0); 44 fillchar(b,sizeof(b),0); 45 for i:=1 to k do 46 begin 47 read(tx,ty); 48 // if (abs(tx)<=abs(n1))and(abs(ty)<=abs(m1)) then 49 // begin 50 inc(n); x[n]:=tx; y[n]:=ty; 51 // end; 52 end; 53 inc(n); x[n]:=0; y[n]:=0; 54 inc(n); x[n]:=n1; y[n]:=m1; 55 if n1<0 then 56 begin 57 n1:=-n1; m1:=-m1; 58 for i:=1 to n do 59 begin 60 x[i]:=-x[i]; y[i]:=-y[i]; 61 end; 62 end; 63 m:=0; 64 for i:=1 to n do 65 begin 66 tx:=(x[i]*by-y[i]*bx) div (ax*by-ay*bx); 67 if (tx<0)or(tx*(ax*by-ay*bx)<>x[i]*by-y[i]*bx) then continue; 68 ty:=(x[i]*ay-y[i]*ax) div (ay*bx-ax*by); 69 if (ty<0)or(ty*(ay*bx-ax*by)<>x[i]*ay-y[i]*ax) then continue; 70 inc(m); a[m]:=tx; b[m]:=ty; 71 if (x[i]=n1)and(y[i]=m1) then ed:=m; 72 end; 73 qsort(1,m); st:=0; 74 for i:=1 to ed do 75 begin 76 if (a[i]=0)and(b[i]=0) then 77 begin 78 dp[i]:=1; st:=i; continue; 79 end; 80 if st=0 then continue; 81 dp[i]:=c(a[i]+b[i],a[i]); 82 for j:=st+1 to i-1 do 83 if (a[j]<=a[i])and(b[j]<=b[i]) then 84 dp[i]:=(dp[i]-dp[j]*c(a[i]+b[i]-a[j]-b[j],a[i]-a[j]) mod mo) mod mo; 85 dp[i]:=(dp[i]+mo) mod mo; 86 end; 87 writeln(dp[ed]); 88 89 end; 90 91 function gcd(x,y:longint):longint; 92 var r:longint; 93 begin 94 repeat 95 r:=x mod y; 96 x:=y; 97 y:=r; 98 until r=0; 99 exit(x); 100 end; 101 102 function spfa:longint; 103 var t,w,t1,w1,u,e,v:longint; 104 p1,p2:boolean; 105 begin 106 fillchar(dp,sizeof(dp),0); 107 fillchar(inq,sizeof(inq),0); 108 fillchar(time,sizeof(time),0); 109 t:=0; t1:=0; w:=1; w1:=1; q[1]:=0; dp[0]:=1; time[0]:=1; p1:=false; p2:=false; 110 while t<w do 111 begin 112 inc(t); inc(t1); 113 if t1=2000 then t1:=1; 114 u:=q[t1]; inq[u]:=false; 115 if (u<-500)or(u>500) then continue; 116 v:=u+ax; 117 if (not inq[v])and(b[v]=0) then 118 begin 119 inc(w); inc(w1); 120 if w1=2000 then w1:=1; 121 q[w1]:=v; 122 dp[v]:=(dp[v]+dp[u]) mod mo; 123 inq[v]:=true; inc(time[v]); 124 if time[0]>1100 then t:=w+1000; 125 if v<-500 then p1:=true; 126 if v>500 then p2:=true; 127 end; 128 v:=u+bx; 129 if (not inq[v])and(b[v]=0) then 130 begin 131 inc(w); inc(w1); 132 if w1=2000 then w1:=1; 133 q[w1]:=v; 134 dp[v]:=(dp[v]+dp[u]) mod mo; 135 inq[v]:=true; inc(time[v]); 136 if time[0]>1100 then t:=w+1000; 137 if v<-500 then p1:=true; 138 if v>500 then p2:=true; 139 end; 140 if p1 and p2 then exit(-1); 141 end; 142 if time[0]>1100 then exit(-1); 143 exit(dp[n1]); 144 end; 145 146 procedure do2; 147 var i,k1,k2,n,tx,ty:longint; 148 begin 149 if ax>bx then 150 begin 151 swap(ax,bx); 152 swap(ay,by); 153 end; 154 155 k1:=gcd(ax,bx); k2:=gcd(ay,by); 156 if (n1 mod k1<>0)or(m1 mod k2<>0) then 157 begin 158 writeln(0); 159 exit; 160 end; 161 fillchar(x,sizeof(x),0); 162 fillchar(y,sizeof(y),0); 163 fillchar(b,sizeof(b),0); 164 n:=0; 165 for i:=1 to k do 166 begin 167 read(tx,ty); 168 if (tx mod k1<>0)or(ty mod k2<>0) then continue; 169 b[tx div k1]:=1; 170 end; 171 n1:=n1 div k1; m1:=m1 div k2; 172 ax:=ax div k1; bx:=bx div k1; 173 writeln(spfa); 174 end; 175 176 begin 177 assign(input,‘1.in‘); reset(input); 178 assign(output,‘1.out‘); rewrite(output); 179 read(cas); 180 fac[0]:=1; exf[0]:=1; exf[1]:=1; 181 for i:=1 to 2000 do fac[i]:=fac[i-1]*i mod mo; 182 for i:=2 to 2000 do exf[i]:=exf[mo mod i]*(mo-mo div i) mod mo; 183 for i:=1 to 2000 do exf[i]:=exf[i-1]*exf[i] mod mo; 184 for v:=1 to cas do 185 begin 186 read(n1,m1,k); 187 read(ax,ay,bx,by); 188 if (ax=0)and(ay=0)and(bx=0)and(by=0) then 189 begin 190 if (n1=0)and(m1=0) then writeln(-1) 191 else writeln(0); 192 continue; 193 end; 194 if (ax=0)and(bx=0)and(n1<>0) then 195 begin 196 writeln(0); 197 continue; 198 end; 199 if (ay=0)and(by=0)and(m1<>0) then 200 begin 201 writeln(0); 202 continue; 203 end; 204 if (ax=0)and(ay<>0)and(bx<>0)and(by=0) then do1 205 else if ax*by=ay*bx then do2 206 else do1; 207 end; 208 close(input); 209 close(output); 210 end.
时间: 2024-10-05 05:01:00