首先我们知道,对于所有种情况,我们可以将每一位可以放的
数的值加起来,所有位置的乘起来,等于的就是最后的答案,具体
为什么正确,可以根据乘法分配律来想一想。
那么对于所有不做要求的,快速幂直接算就行了,然后快排下,就知道
每个位置不放那些值,减掉后乘进去就行了。
/**************************************************************
Problem:
User: BLADEVIL
Language: Pascal
Result: Accepted
Time: ms
Memory: kb
****************************************************************/ //By BLADEVIL
const
d39 =; var
n, m, k :longint;
a, b :array[..] of longint;
ans :int64; procedure swap(var a,b:longint);
var
c :longint;
begin
c:=a; a:=b; b:=c;
end; procedure qs(low,high:longint);
var
i, j, xx, yy :longint;
begin
i:=low; j:=high;
xx:=a[(i+j) div ]; yy:=b[(i+j) div ];
while i<j do
begin
while (a[i]<xx) or (a[i]=xx) and (b[i]<yy) do inc(i);
while (a[j]>xx) or (a[j]=xx) and (b[j]>yy) do dec(j);
if i<=j then
begin
swap(a[i],a[j]);
swap(b[i],b[j]);
inc(i); dec(j);
end;
end;
if i<high then qs(i,high);
if j>low then qs(low,j);
end; procedure init;
var
i :longint;
begin
read(n,m,k);
for i:= to k do read(a[i],b[i]);
qs(,k);
end; function mi(a,b:int64):int64;
var
sum :int64;
begin
sum:=a;
mi:=;
while b<> do
begin
if b mod = then mi:=mi*sum mod d39;
sum:=sum*sum mod d39;
b:=b div ;
end;
end; procedure main;
var
i :longint;
sum, x, y, z :int64;
begin
sum:=m;
x:=-;
for i:= to k do
begin
if a[i]<>x then
begin
dec(sum);
x:=a[i];
end;
end;
x:=n; y:=n+;
if x mod = then x:=x div else y:=y div ;
x:=x mod d39;
y:=y mod d39;
x:=x*y mod d39;
ans:=mi(x,sum);
for i:= to k do if (a[i]=a[i-]) and (b[i]=b[i-]) then b[i-]:=;
y:=-;
z:=-;
for i:= to k do
begin
if a[i]<>y then
begin
if i<> then ans:=ans*z mod d39;
z:=x;
y:=a[i];
z:=((x-b[i]) mod d39+d39) mod d39;
end else z:=((z-b[i])mod d39+d39) mod d39;
end;
if z<>- then ans:=ans*z mod d39;
writeln(ans);
end; begin
init;
main;
end.