zoukankan      html  css  js  c++  java
  • uva10655矩阵快速幂

    a^(n+2)+b^(n+2)=(a+b)*(a^(n+1)+b^(n+1))-a*b*(a^n+b^n)

    坑爹的题目关系式都推出来了居然还是wa了。。。。。

    不能只看p,q=0就退出,因为a,b不一定为0啊,卧槽,出题人简直就是个心机婊

    还有n=0的情况要考虑

    #include<map>
    #include<set>
    #include<cmath>
    #include<queue>
    #include<stack>
    #include<vector>
    #include<cstdio>
    #include<iomanip>
    #include<cstdlib>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #define pi acos(-1)
    #define ll long long
    #define mod 1000000007
    #define ls l,m,rt<<1
    #define rs m+1,r,rt<<1|1
    #pragma comment(linker, "/STACK:1024000000,1024000000")
    
    using namespace std;
    
    const double g=10.0,eps=1e-9;
    const int N=10+5,maxn=1<<10+5,inf=0x3f3f3f3f;
    
    struct Node{
        ll row,col;
        ll a[N][N];
    };
    Node mul(Node x,Node y)
    {
        Node ans;
        ans.row=x.row,ans.col=y.col;
        memset(ans.a,0,sizeof ans.a);
        for(ll i=0;i<x.row;i++)
            for(ll j=0;j<x.col;j++)
                for(ll k=0;k<y.col;k++)
                   ans.a[i][k]=(ans.a[i][k]+x.a[i][j]*y.a[j][k]);
        return ans;
    }
    Node quick_mul(Node x,ll n)
    {
        Node ans;
        ans.row=x.row,ans.col=x.col;
        memset(ans.a,0,sizeof ans.a);
        for(ll i=0;i<ans.col;i++)ans.a[i][i]=1;
        while(n){
            if(n&1)ans=mul(ans,x);
            x=mul(x,x);
            n>>=1;
        }
        return ans;
    }
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
     //   cout<<setiosflags(ios::fixed)<<setprecision(2);
        ll p,q,n;
        while(cin>>p>>q>>n){
            if(n==1)
            {
                cout<<p<<endl;
                continue;
            }
            if(n==0)
            {
                cout<<2<<endl;
                continue;
            }
            Node A;
            A.row=2,A.col=2;
            A.a[0][0]=0,A.a[0][1]=1;
            A.a[1][0]=-q,A.a[1][1]=p;
            A=quick_mul(A,n-1);
            Node B;
            B.row=2,B.col=1;
            B.a[0][0]=p,B.a[1][0]=p*p-2*q;
            A=mul(A,B);
            cout<<A.a[0][0]<<endl;
        }
        return 0;
    }
    View Code
  • 相关阅读:
    Python装饰器
    Python常用内建模块
    Python文件的操作
    Python集合的操作
    Python字典的操作
    Python列表元组的操作
    os.path
    Python字符串的操作
    线性回归
    随机森林
  • 原文地址:https://www.cnblogs.com/acjiumeng/p/6868331.html
Copyright © 2011-2022 走看看