http://acm.nyist.net/JudgeOnline/problem.php?pid=301&&递推求值

矩阵运算。。。这一题让我明白了一些事,在做题的时候一定要考虑数的取值范围。。。否者会多吃WA的这一题我就是因为这wa了好几次。。。

#include<iostream>
#include<string.h>
#include<cstdio>
#include<algorithm>
#define M 1000007
typedef long long L;
typedef struct
{   L s[3][3];
}Node;
Node a,b;
Node ceil(Node p,Node q)
{   Node c;
   memset(c.s,0,sizeof(c.s));
    for(int i=0;i<3;++i)
	  for(int j=0;j<3;++j)
		  for(int t=0;t<3;++t)
		    c.s[i][j]=((c.s[i][j]+p.s[i][t]*q.s[t][j])%M+M)%M;
	      return c;
}
Node doit(int k)
{   Node p=b,q=a;
    while(k)
	{  if(k&1) p=ceil(p,q);
	      q=ceil(q,q);
		  k=k>>1;
	}
	return p;
}
int main()
{  int T;
    scanf("%d",&T);
	while(T--)
	{  int f1,f2,a1,b1,c1,n;
	    scanf("%d%d%d%d%d%d",&f1,&f2,&a1,&b1,&c1,&n);
		if(n==1) printf("%d\n",f1);
	    else if(n==2) printf("%d\n",f2);
	   else if(n>=3)
	   {  a.s[0][0]=0;a.s[0][1]=a1;a.s[0][2]=0;a.s[1][0]=1;a.s[1][1]=b1;a.s[1][2]=0;a.s[2][0]=0;a.s[2][1]=1;a.s[2][2]=1;
	      b.s[0][0]=1;b.s[0][1]=0;b.s[0][2]=0;b.s[1][0]=0; b.s[1][1]=1;b.s[1][2]=0;b.s[2][0]=0;b.s[2][1]=0;b.s[2][2]=1;
		  n-=2;
		 Node c=doit(n);
		 L k=((f1*c.s[0][1]+f2*c.s[1][1]+c1*c.s[2][1])%M+M)%M;
		 printf("%lld\n",k);
	}
	}return 0;
}

一般一个题这样算下来不超过1亿的话,一秒左右都能过;


嗯,大致数量级就是亿级别
十亿就很可能会挂
不过,这都只是一般情况
;
编译器不同,机器配置不同,操作系统不同;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值