zoukankan      html  css  js  c++  java
  • FZU 1683 纪念SlingShot(矩阵水)

    纪念SlingShot

    【题目链接】纪念SlingShot

    【题目类型】矩阵水

    &题解:

    这代码调了十多分钟,结果是Mul没返回值,好zz啊.
    令sum(n)=sum(n-1)+f(n) 那么sum(n)就是答案,可以得出矩阵:

    转载自:http://blog.csdn.net/chenguolinblog/article/details/10309423

    &代码:

    #include <cstdio>
    #include <bitset>
    #include <iostream>
    #include <set>
    #include <cmath>
    #include <cstring>
    #include <algorithm>
    #include <map>
    #include <queue>
    #include <vector>
    using namespace std;
    #define INF 0x3f3f3f3f
    typedef long long ll;
    const int si=4;
    ll n,M=2009,K;
    struct mat
    {
    	ll m[si][si];
    }A;
    void DF(mat A)
    {
    	for(int i=0;i<si;i++)
    	{
    		for(int j=0;j<si;j++)
    			cout<<A.m[i][j]<<" ";
    		cout<<endl;
    	}
    }
    mat Mul(mat a,mat b)
    {
    	mat c;
    	for(int i=0;i<si;i++)
    	for(int j=0;j<si;j++){
    		c.m[i][j]=0;
    		for(int k=0;k<si;k++){
    			c.m[i][j]=(c.m[i][j]+a.m[i][k]*b.m[k][j])%M;
    		}
    	}
    	//TMD 这块居然没返回c 找了半天bug 真ZZ
    	return c;
    }
    mat bPow(mat a,ll z)
    {
    	mat b;
    	for(int i=0;i<si;i++)for(int j=0;j<si;j++)
    		b.m[i][j]=(i==j);
    	while(z){
    		if(z&1)
    			b=Mul(b,a);
    		a=Mul(a,a);
    		z>>=1;
    	}
    	return b;
    }
    int tb[si];
    void Init()
    {
    	tb[0]=5,tb[1]=3,tb[2]=1,tb[3]=9;
    	memset(A.m,0,sizeof(A.m));
    	A.m[0][0]=3,A.m[0][1]=2,A.m[0][2]=7;
    	for(int i=0;i<si;i++) A.m[3][i]=A.m[0][i];
    	A.m[3][3]=A.m[2][1]=A.m[1][0]=1;
    }
    int main()
    {
    	freopen("E:1.txt","r",stdin);
    	int T;cin>>T;
    	while(T--){
    		cin>>n;
    		Init();
    		if(n<=2){
    			printf("Case %d: ",++K);
    			cout<<tb[2-n]<<endl;
    		}
    		else{
    			A=bPow(A,n-2);
    			ll ans=0;
    			for(int i=0;i<si;i++){
    				ans=(ans+A.m[3][i]*tb[i])%M;
    			}
    			printf("Case %d: ",++K);
    			cout<<ans<<endl;
    		}
    	}
    	return 0;
    }
    
    
  • 相关阅读:
    导论
    Array
    Singleton
    Bridge
    Mediator
    interpreter
    Visitor
    Flyweight
    Command
    Chain Of Responsibility
  • 原文地址:https://www.cnblogs.com/s1124yy/p/6667116.html
Copyright © 2011-2022 走看看