• hzau 1202 GCD(矩阵快速幂)


    1202: GCD

    Time Limit: 1 Sec  Memory Limit: 1280 MB
    Submit: 201  Solved: 31
    [Submit][Status][Web Board]

    Description

     

    Input

     The first line is an positive integer  T . (1<=T<= 10^3) indicates the number of test cases. In the next T lines, there are three positive integer n, m, p (1<= n,m,p<=10^9) at each line.

    Output

    Sample Input

    1 
    1 2 3
    
    

    Sample Output

    1
    

    HINT

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 #define ll long long
     4 #define eps 1e-4
     5 const int N=1e6+10,M=1e6+10;
     6 
     7 ///数组大小
     8 ll MOD;
     9 
    10 struct Matrix
    11 {
    12     ll matri[2][2];
    13     Matrix()
    14     {
    15         memset(matri,0,sizeof(matri));
    16     }
    17     void init()
    18     {
    19         for(int i=0;i<2;i++)
    20             for(int j=0;j<2;j++)
    21                 matri[i][j]=(i==j);
    22     }
    23     Matrix operator + (const Matrix &B)const
    24     {
    25         Matrix C;
    26         for(int i=0;i<2;i++)
    27             for(int j=0;j<2;j++)
    28                 C.matri[i][j]=(matri[i][j]+B.matri[i][j])%MOD;
    29         return C;
    30     }
    31     Matrix operator * (const Matrix &B)const
    32     {
    33         Matrix C;
    34         for(int i=0;i<2;i++)
    35             for(int k=0;k<2;k++)
    36                 for(int j=0;j<2;j++)
    37                     C.matri[i][j]=(C.matri[i][j]+1LL*matri[i][k]*B.matri[k][j])%MOD;
    38         return C;
    39     }
    40     Matrix operator ^ (const ll &t)const
    41     {
    42         Matrix A=(*this),res;
    43         res.init();
    44         ll p=t;
    45         while(p)
    46         {
    47             if(p&1)res=res*A;
    48             A=A*A;
    49             p>>=1;
    50         }
    51         return res;
    52     }
    53 };
    54 int main()
    55 {
    56     Matrix base;  ///初始化矩阵
    57     base.matri[0][0]=1;base.matri[0][1]=1;
    58     base.matri[1][0]=1;base.matri[1][1]=0;
    59 
    60     int T;
    61     scanf("%d",&T);
    62     while(T--)
    63     {
    64         int n,m,p;
    65         scanf("%d%d%d",&n,&m,&p);
    66         int x=__gcd(n+2,m+2);
    67         MOD=p;
    68         if(x<=2)
    69             printf("%d
    ",1%p);
    70         else
    71         {
    72             Matrix ans=base^(x-2);
    73             printf("%lld
    ",(ans.matri[0][0]+ans.matri[0][1])%MOD);
    74         }
    75     }
    76     return 0;
    77 }
  • 相关阅读:
    1988-B. 有序集合
    1987-A. 集训队选拔
    1964-NP
    1963-带妹子去看电影
    1962-Fibonacci
    1961-计算机基础知识大赛 2 (new)
    TCP/IP协议详解 卷一:协议 18章、TCP连接的建立与终止
    3、剑指offer--从尾到头打印链表
    2、剑指offer--替换空格
    1、剑指offer--二维数组中查找
  • 原文地址:https://www.cnblogs.com/gongpixin/p/6769576.html
Copyright © 2020-2023  润新知