【BZOJ2326】【HNOI2011】数学作业

【Description】

  这里写图片描述


【Solution】

  矩阵快速幂。递推式:
  

10k00110111f[i1]i11=f[i]i1

  代码如下:

/**************************************************************
    Problem: 2326
    User: llgyc
    Language: Pascal
    Result: Accepted
    Time:56 ms
    Memory:224 kb
****************************************************************/

type matrix = array[1..3,1..3] of qword;
var n,m,base:qword;
    ans,t:matrix;
    i:longint;
function mul(x,y:qword):qword;
  var tmp:qword;
  begin
    tmp:=0;
    while (y>0) do begin
      if (y mod 2=1) then tmp:=(tmp+x) mod m;
      x:=(x*2) mod m; y:=y div 2;
    end; exit(tmp);
  end;
procedure multi(var a,b,c:matrix);
  var i,j,k:longint;
      tmp:matrix;
  begin
    fillchar(tmp,sizeof(tmp),0);
    for i:=1 to 3 do
      for j:=1 to 3 do
        for k:=1 to 3 do
          tmp[i,j]:=(tmp[i,j]+mul(a[i,k],b[k,j])) mod m;
    c:=tmp;
  end;
procedure cal(base,last:qword);
  var tmp:qword;
      i,j:longint;
  begin
    fillchar(t,sizeof(t),0);
    t[1,1]:=base; t[1,2]:=1; t[1,3]:=1; t[2,2]:=1; t[2,3]:=1; t[3,3]:=1;
    tmp:=last-base div 10+1;
    while (tmp>0) do begin
      if (tmp mod 2=1) then multi(t,ans,ans);
      multi(t,t,t); tmp:=tmp div 2;
    end;
  end;
begin
  readln(n,m); for i:=1 to 3 do ans[i,i]:=1;
  base:=10;
  while (n>=base) do begin cal(base,base-1); base:=base*10; end; cal(base,n);
  writeln(ans[1,3]);
end.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值