Bzoj 1008 [hnoi2008]越狱prison

组合数学一下,快速幂..

之前写过了= =,只是忘记了(a-b)mod def = ((a mod def+def) –b)mod def

View Code
 1 //bzoj 1008 hnoi2008prison
 2 const
 3         def=100003;
 4 var
 5         tmp, ans, m, n: qword;
 6 function power(m, n: qword): qword;
 7 var
 8         base: qword;
 9 begin
10   base := m;
11   power := 1;
12   while n<>0 do begin
13     if n and 1 = 1 then power := power * base mod def;
14     base := (base mod def)*(base mod def) mod def;
15     n := n >> 1;
16   end;
17 end;
18 
19 procedure main;
20 begin
21   readln(m, n);
22   ans := power(m, n) mod def;
23   tmp := m*power(m-1,n-1) mod def;
24   ans := ((ans+def)-tmp)mod def;
25   writeln(ans);
26 end;
27 
28 begin
29   main;
30 end.
posted @ 2012-04-12 19:22  F.D.His.D  阅读(232)  评论(0编辑  收藏  举报