2020年湖南省赛——I . 战场的数目(矩阵快速幂)

思路:

(1.)当n为偶数时才有解,因为出现的面是成对出现的。
(2.)分析一下递推式,假设先不排除是矩形的情况,那么对于一个周长为(n)的图形,可以有三种操作:

  • 左边或右边仅某一边有一个单独的小正方形,删除后变成周长为(n-2)的图形
  • 左边和右边都没有单独的小正方形,删除最下面一行,变成周长为(n-2)的图形
  • 左边和右边都有一个单独的小正方形,在第一种中统计了,所以要减去,变成周长为(n-4)的图形

(f[n]=3*f[n-2]-f[n-4])
(n)变为(n/2)(f[n/2]=3*f[(n-2)/2]-f[(n-4)/2])
(f[n]=3*f[n-1]-f[n-2]) (注意这里的(n)为原来的(1/2))
考虑矩阵快速幂优化。
3. 要去掉矩形的情况,一个周长为(n)的矩形有(n/2-1)种。
假设长为(x),宽为(y),则(2*(x+y)=n)
(x+y=n/2)
(n/2-1种组合)
(4.)注意矩阵快速幂的边界

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll>PLL;
typedef pair<int, int>PII;
typedef pair<double, double>PDD;
#define I_int ll
inline ll read()
{
    ll x = 0, f = 1;
    char ch = getchar();
    while(ch < '0' || ch > '9')
    {
        if(ch == '-')f = -1;
        ch = getchar();
    }
    while(ch >= '0' && ch <= '9')
    {
        x = x * 10 + ch - '0';
        ch = getchar();
    }
    return x * f;
}
#define read read()
#define closeSync ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define multiCase int T;cin>>T;for(int t=1;t<=T;t++)
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repp(i,a,b) for(int i=(a);i<(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define perr(i,a,b) for(int i=(a);i>(b);i--)
ll ksm(ll a, ll b, ll p)
{
    ll res = 1;
    while(b)
    {
        if(b & 1)res = res * a % p;
        a = a * a % p;
        b >>= 1;
    }
    return res;
}

const int inf = 0x3f3f3f3f;
#define PI acos(-1)
const int maxn=1e6+100;
const double eps=1e-7;
const ll mod=987654321;
struct mat{
	ll v[3][3];
	mat(){
		memset(v,0,sizeof v);
		v[1][1]=v[2][2]=1;
	}
};
mat mul(mat a,mat b){
	mat c;
	for(int i=0;i<=2;i++)
		for(int j=0;j<=2;j++)
			c.v[i][j]=0;
		for(int i=1;i<=2;i++)
			for(int j=1;j<=2;j++)
				for(int k=1;k<=2;k++)
				c.v[i][j]=(c.v[i][j]+a.v[i][k]*b.v[k][j])%mod;
	return c;
}

mat MatPow(mat a,ll p){
	mat c;
	
	for(int i=0;i<=2;i++)
		for(int j=0;j<=2;j++)
			c.v[i][j]=0;
	for(int i=1;i<=2;i++) c.v[i][i]=1;
	while(p){
		if(p&1) c=mul(c,a);
		a=mul(a,a);
		p>>=1;
	}
	return c;
}

struct data
{
	ll v[3][3];
	data() {memset(v , 0 , sizeof(v));}
	data(int x) {memset(v , 0 , sizeof(v)) , v[2][2] = v[1][1] = 1;}
	data operator*(const data a)const
	{
		data ans;
		int i , j , k;
		for(i = 1 ; i <=2 ; i ++ )
			for(j = 1; j <= 2 ; j ++ )
				for(k = 1; k <= 2 ; k ++ )
					ans.v[i][j] = (ans.v[i][j] + v[i][k] * a.v[k][j]) % mod;
		return ans;
	}
	data operator^(const ll a)const
	{
		data x = *this , ans(1);
		ll y = a;
		while(y)
		{
			if(y & 1) ans = ans * x;
			x = x * x , y >>= 1;
		}
		return ans;
	}
};

int main(){
	int n;
	while(cin>>n){
		if(!n) break;
		if(n==2||n&1) puts("0");
		//else if(n==8) puts("2");
		//else if(n==10) puts("9");
		else{
			mat A,B;
			for(int i=0;i<=2;i++)
				for(int j=0;j<=2;j++)
					A.v[i][j]=0,B.v[i][j]=0;
			A.v[1][1] = A.v[1][2] = 1;
			B.v[1][2] = mod - 1 , B.v[2][1] = 1 , B.v[2][2] = 3;
			n/=2;
			B=MatPow(B,n-1);
			A=mul(A,B);
			//A=mul(A,MatPow(B,n-1));
			ll res=(A.v[1][1] - n + 1 + mod) % mod;
			printf("%lld
",res);
		}
	}
	return 0;
}











原文地址:https://www.cnblogs.com/OvOq/p/15034190.html