码迷,mamicode.com
首页 > 其他好文 > 详细

UVAlive 7361 Immortal Porpoises(矩阵快速幂)

时间:2016-08-17 23:12:57      阅读:353      评论:0      收藏:0      [点我收藏+]

标签:

题目地址:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=5383

思路:

技术分享

技术分享

type arr=array[1..2,1..2] of int64;
var
 x,p:int64;
 a,b,c,w:arr;
 cas,i,t:longint;
procedure mul(var a,b,c:arr);
var i,j,k,n:longint;
begin
 n:=2;
 fillchar(w,sizeof(w),0);
 for i:=1 to n do
  for j:=1 to n do
   for k:=1 to n do
    w[i][j]:=(w[i][j]+a[i][k]*b[k][j]) mod p;
 for i:=1 to n do
  for j:=1 to n do
   c[i][j]:=w[i][j];
end;
begin
 readln(cas);
 p:=1000000000;
 for i:=1 to cas do
 begin
   read(t);
   readln(x);
  a[1,1]:=0;a[1,2]:=1;a[2,1]:=0;a[2,2]:=0;
  b[1,1]:=0;b[1,2]:=1;b[2,1]:=1;b[2,2]:=1;
  while (x<>0) do
   begin
    if x mod 2=1 then mul(a,b,a);
    x:=x div 2;
    mul(b,b,b);
   end;
  write(t);
  write(' ');
  writeln(a[1][1]);
 end;
end.



UVAlive 7361 Immortal Porpoises(矩阵快速幂)

标签:

原文地址:http://blog.csdn.net/wang2147483647/article/details/52235119

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!