[BZOJ1297][SCOI2009]迷路

矩阵快速幂技巧解析
本文通过一个具体的编程实例,详细解析了使用矩阵快速幂解决特定问题的方法。文章首先介绍了问题背景,随后展示了如何通过矩阵运算及快速幂技巧来优化算法效率,并提供了完整的代码实现。

被卡了两个小时,发现是自己对这个东西没有理解透,是要让边数一一对应而不是点数一一对应,去掉一个+1后就过了,惨啊.

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<string>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<iomanip>
using namespace std;
#define ll long long
#define db double 
#define up(i,j,n) for(int i=j;i<=n;i++)
#define pii pair<int,int>
#define uint unsigned int
#define FILE "dealing"
#define eps 1e-4
int read(){
	int x=0,f=1,ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
	return x*f;
}
template<class T> bool cmax(T& a,T b){return a<b?a=b,true:false;}
template<class T> bool cmin(T& a,T b){return a>b?a=b,true:false;}
const int maxn=110,limit=50100,inf=1000000000,r=3,mod=2009;
int N=100;
struct Matrix{
	int a[maxn][maxn];
	void print(){
		up(i,1,N){
			up(j,1,N)printf("%d ",a[i][j]);
			cout<<endl;
		}
		cout<<endl;
	}
	Matrix(){memset(a,0,sizeof(a));}
	Matrix operator*(const Matrix& b){
		memset(c,0,sizeof(c));
		up(i,1,N)up(j,1,N)up(k,1,N)c.a[i][j]=(c.a[i][j]+a[k][j]*b.a[i][k])%mod;
		return c;
	}
}b,a,ans,c;
int main(){
	freopen(FILE".in","r",stdin);
	freopen(FILE".out","w",stdout);
	int T,n;
	n=read();T=read();
	char ch;
	up(i,1,n)up(j,1,8){
			b.a[(i-1)*9+j][(i-1)*9+j+1]++;
	}
	up(i,1,n){
		up(j,1,n){
			scanf(" %c",&ch);
			if(ch=='0')continue;ch-='0';
			b.a[(i-1)*9+ch][(j-1)*9+1]++;
		}
	}
	up(i,1,N)ans.a[i][i]=1;
	for(;T;T>>=1,b=b*b)
		if(T&1)ans=ans*b;
	printf("%d\n",ans.a[1][(n-1)*9+1]);
	return 0;
}

  

转载于:https://www.cnblogs.com/chadinblog/p/6560396.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值