HDU-5793 A Boring Question(矩阵快速幂)



A Boring Question

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 60    Accepted Submission(s): 19


Problem Description
There are an equation.
0k1,k2,kmn1j<m(kj+1kj)%1000000007=?
We define that (kj+1kj)=kj+1!kj!(kj+1kj)! . And (kj+1kj)=0 while kj+1<kj .
You have to get the answer for each n and m that given to you.
For example,if n=1 , m=3 ,
When k1=0,k2=0,k3=0,(k2k1)(k3k2)=1 ;
When k1=0,k2=1,k3=0,(k2k1)(k3k2)=0 ;
When k1=1,k2=0,k3=0,(k2k1)(k3k2)=0 ;
When k1=1,k2=1,k3=0,(k2k1)(k3k2)=0 ;
When k1=0,k2=0,k3=1,(k2k1)(k3k2)=1 ;
When k1=0,k2=1,k3=1,(k2k1)(k3k2)=1 ;
When k1=1,k2=0,k3=1,(k2k1)(k3k2)=0 ;
When k1=1,k2=1,k3=1,(k2k1)(k3k2)=1 .
So the answer is 4.
 

Input
The first line of the input contains the only integer T , (1T10000)
Then T lines follow,the i-th line contains two integers n , m , (0n109,2m109)
 

Output
For each n and m ,output the answer in a single line.
 

Sample Input
  
  
2 1 2 2 3
 

Sample Output
  
  
3 13
 

关键在于递推公式的推导,真是想了太久     Ai=Ai-1(m)+1;
剩下的就是裸的矩阵幂了。。
#include <cstdio>
#define inf   1000000007
long long n,ms;
const int MAX = 2;
struct  node
{
   long long m[MAX][MAX];
}Str,A;
node matrixmul(node a,node b) 
{
	   node c;
	   for(int i=0;i<MAX;i++)
	      for(int j=0;j<MAX;j++)
	              c.m[i][j]=0;
       int i,j,k;
       for (i = 0 ; i < MAX; i++)
           for (j = 0; j < MAX;j++)
             {
                 for (k = 0; k < MAX; k++)
                     c.m[i][j] += (a.m[i][k]%inf * b.m[k][j]%inf)%inf;
                 c.m[i][j] %= inf;
             }
       return c;
}       
node quickpow(long long n)
{
       node m = A, b = Str;
       node c;
       for(int i=0;i<MAX;i++)
          for(int j=0;j<MAX;j++)
          {
              if(i==j)
                c.m[i][j]=1;
                else
                    c.m[i][j]=0;
            }
       while (n >= 1)
       {
       	     if(n&1)
       	     c=matrixmul(c,b);
         	 b = matrixmul(b,b);
         	 n = n /2;
       }
       return c;
}
int main()
{
    node re;
    int T;
    scanf("%d",&T);
    while (T--)
    {
    	 A.m[0][0]=1;
    	 A.m[0][1]=1;
    	 A.m[1][0]=0;
    	 A.m[1][1]=0;
    	scanf("%lld%lld",&n,&ms);
    	Str.m[0][0]=ms;
    	Str.m[0][1]=0;
    	Str.m[1][0]=1;
    	Str.m[1][1]=1;
       re=quickpow(n);
    long long t=(re.m[0][0]+re.m[1][0])%inf;
    printf("%lld\n",t);
   }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值