zoukankan      html  css  js  c++  java
  • HDU 2256 Problem of Precision(矩阵)

    Problem of Precision

    【题目链接】Problem of Precision

    【题目类型】矩阵

    &题解:

    参考:点这里
    这题做的好玄啊,最后要添加一项,之后约等于,但是有double的时候一定不能取余,还是要记住的

    &代码:

    #include <cstdio>
    #include <iostream>
    #include <set>
    #include <cmath>
    #include <cstring>
    #include <algorithm>
    #include <map>
    #include <queue>
    #include <vector>
    using namespace std;
    #define INF 0x3f3f3f3f
    using ll=long long;
    const int maxn= 1e3 +9;
    ll n,M=1024;
    typedef vector<ll> vec;
    typedef vector<vec> mat;
    mat mul(mat &A,mat &B)
    {
    	mat C(A.size(),vec(B[0].size()));
    	for(int i=0;i<A.size();i++)
    	for(int k=0;k<B.size();k++)
    	for(int j=0;j<B[0].size();j++){
    		C[i][j]=(C[i][j]+A[i][k]*B[k][j])%M;
    	}
    	return C;
    }
    mat bin_pow(mat A,ll n)
    {
    	mat B(A.size(),vec(A.size()));
    	for(int i=0;i<A.size();i++)
    		B[i][i]=1;
    	while(n>0){
    		if(n&1)
    			B=mul(B,A);
    		A=mul(A,A);
    		n>>=1;
    	}
    	return B;
    }
    mat A(2,vec(2));
    ll fin[]={5,2};
    void init()
    {
    	A[0][0]=5,A[0][1]=12;
    	A[1][0]=2,A[1][1]=5;
    }
    int main()
    {
    //	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    	freopen("E:1.txt","r",stdin);
    	int T;cin>>T;
    	while(T--){
    		init();
    		cin>>n;
    		//这块返回来的mat一定要赋值给A啊 不要忘了
    		A=bin_pow(A,n-1);
    		ll ans=0;
    		for(int i=0;i<2;i++){
    			ans=(ans+A[0][i]*fin[i])%M;
    		}
    		cout<<(2*ans-1)%M<<endl;
    	}
    	return 0;
    }
    
  • 相关阅读:
    L3-015. 球队“食物链”【DFS + 剪枝】
    L3-002. 堆栈【主席树 or 线段树 or 分块】
    PTA L1-006 连续因子【暴力模拟】
    【路由和交换之H3C自导自演】
    【ospf-stub区域配置】
    【ospf-链路验证】
    【ospf-vlink虚拟连接】
    【c学习-14】
    【c学习-13】
    【php学习-5】
  • 原文地址:https://www.cnblogs.com/s1124yy/p/6603146.html
Copyright © 2011-2022 走看看