vijos1049送给圣诞夜的礼品 矩阵快速幂

矩阵的结合律

所有矩阵都压在一起之后再和ans乘

注意好谁乘谁就行

#include
#include
#include
#include
#include
#include
#define LL long long
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define down(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
inline LL read()
{
	LL d=0,f=1;char s=getchar();
	while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
	while(s>='0'&&s<='9'){d=d*10+s-'0';s=getchar();}
	return d*f;
}
#define N 100
#define K 11
int n,m,nm;

struct matrix
{
	int a[N][N];
	void clear()
	{
		memset(a,0,sizeof(a));
	}
	void OUT()
	{
		fo(i,0,m-1)
		{
			fo(j,0,m-1)
			{
				cout<0)
	{
		if(k&1)ret=a*ret;
		a=a*a;
		k>>=1;
	}
	return ret;
}

int main()
{
	scanf("%d%d%d",&m,&nm,&n);
	getI();
	
	matrix t;t=I;
	fo(i,1,nm)
	{
		tot[i].clear();
		fo(j,1,m)
		{
			int x;scanf("%d",&x);
			tot[i].a[j-1][x-1]=1;
		}
		t=tot[i]*t;
	}
	int num=n/nm;
	n=n%nm;
	t=KSM(t,num);
	
	fo(i,1,n)t=tot[i]*t;
	matrix ans;ans.clear();
	fo(i,0,m-1)ans.a[i][0]=i+1;
	t=t*ans;
	fo(i,0,m-1)
	{
		if(i!=0)cout<<" ";
		cout<


你可能感兴趣的:(数论)