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

 #include<bits/stdc++.h>
using namespace std;
#define ll long long
#define eps 1e-4
const int N=1e6+,M=1e6+; ///数组大小
ll MOD; struct Matrix
{
ll matri[][];
Matrix()
{
memset(matri,,sizeof(matri));
}
void init()
{
for(int i=;i<;i++)
for(int j=;j<;j++)
matri[i][j]=(i==j);
}
Matrix operator + (const Matrix &B)const
{
Matrix C;
for(int i=;i<;i++)
for(int j=;j<;j++)
C.matri[i][j]=(matri[i][j]+B.matri[i][j])%MOD;
return C;
}
Matrix operator * (const Matrix &B)const
{
Matrix C;
for(int i=;i<;i++)
for(int k=;k<;k++)
for(int j=;j<;j++)
C.matri[i][j]=(C.matri[i][j]+1LL*matri[i][k]*B.matri[k][j])%MOD;
return C;
}
Matrix operator ^ (const ll &t)const
{
Matrix A=(*this),res;
res.init();
ll p=t;
while(p)
{
if(p&)res=res*A;
A=A*A;
p>>=;
}
return res;
}
};
int main()
{
Matrix base; ///初始化矩阵
base.matri[][]=;base.matri[][]=;
base.matri[][]=;base.matri[][]=; int T;
scanf("%d",&T);
while(T--)
{
int n,m,p;
scanf("%d%d%d",&n,&m,&p);
int x=__gcd(n+,m+);
MOD=p;
if(x<=)
printf("%d\n",%p);
else
{
Matrix ans=base^(x-);
printf("%lld\n",(ans.matri[][]+ans.matri[][])%MOD);
}
}
return ;
}

hzau 1202 GCD(矩阵快速幂)的更多相关文章

  1. hdu 5451 Best Solver 矩阵循环群+矩阵快速幂

    http://acm.hdu.edu.cn/showproblem.php?pid=5451 题意:给定x    求解 思路: 由斐波那契数列的两种表示方法, 之后可以转化为 线性表示 F[n] = ...

  2. poj 2888 Magic Bracelet(Polya+矩阵快速幂)

    Magic Bracelet Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 4990   Accepted: 1610 D ...

  3. CF1106F Lunar New Year and a Recursive Sequence 原根、矩阵快速幂、BSGS

    传送门 好久没写数论题了写一次调了1h 首先发现递推式是一个乘方的形式,线性递推和矩阵快速幂似乎都做不了,那么是否能够把乘方运算变成加法运算和乘法运算呢? 使用原根!学过\(NTT\)的都知道\(99 ...

  4. UVaLive 3704 Cellular Automaton (循环矩阵 + 矩阵快速幂)

    题意:一个细胞自动机包含 n 个格子,每个格子取值是 0 ~ m-1,给定距离,则每次操作后每个格子的值将变成到它距离不超过 d 的所有格子在操作之前的值之和取模 m 后的值,其中 i 和 j 的距离 ...

  5. UVa 10870 Recurrences (矩阵快速幂)

    题意:给定 d , n , m (1<=d<=15,1<=n<=2^31-1,1<=m<=46340).a1 , a2 ..... ad.f(1), f(2) .. ...

  6. Codeforces1106F 【BSGS】【矩阵快速幂】【exgcd】

    首先矩阵快速幂可以算出来第k项的指数,然后可以利用原根的性质,用bsgs和exgcd把答案解出来 #include<bits/stdc++.h> using namespace std; ...

  7. 2018.08.30 NOIP模拟 kfib(矩阵快速幂+exgcd)

    [输入] 一行两个整数 n P [输出] 从小到大输出可能的 k,若不存在,输出 None [样例输入 1] 5 5 [样例输出] 2 [样例解释] f[0] = 2 f[1] = 2 f[2] = ...

  8. BZOJ 1009 [HNOI2008]GT考试 (KMP + 矩阵快速幂)

    1009: [HNOI2008]GT考试 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 4266  Solved: 2616[Submit][Statu ...

  9. 【2018北京集训十二】 coin 矩阵快速幂

    矩阵快速幂原来还可以这么用?? 你们城里人还真会玩. 我们令$f[i][j][k]$表示总的钱数为i,当前使用的最大面值硬币的面值为$v_j$,最小为$v_k$的方案数量. 不难发现$f[i][j][ ...

  10. hdu4549矩阵快速幂+费马小定理

    转移矩阵很容易求就是|0  1|,第一项是|0| |1  1|             |1| 然后直接矩阵快速幂,要用到费马小定理 :假如p是质数,且gcd(a,p)=1,那么 a(p-1)≡1(m ...

随机推荐

  1. bash常见命令

    pwd (Print Working Directory) 查看当前目录 cd (Change Directory) 切换目录,如 cd /etc ls (List) 查看当前目录下内容,如 ls - ...

  2. ThreadPoolExecutor 线程池任务队列分析 与 利特尔法则(Little's law)

    一. 演示 public class ThreadPoolTest { static class MyThread implements Runnable { private String name; ...

  3. If you ever have a broken heart

    If you ever have a broken heart Pablo Neruda Tonight i can write the saddest lines Write ,for exampl ...

  4. 通过js代码来制作数据库增删改查插件

    代码流程 1.订制表头:table_config 2.订制显示内容: table_config,data_list 3.加载框: 图片,position:fixed       4.-字符串格式化   ...

  5. jmeter 测试restful接口

    jmeter 测试restful接口,JSON数据格式 1.添加线程组 2.添加HTTP信息头管理器 请求发送JSON数据格式参数,需要设置Content-Type为application/json ...

  6. Python基础(14)_python模块之configparser模块、suprocess

    9.configparser模块 模块适用于配置文件的格式与windows ini文件类似,可以包含一个或多个节(section),每个节可以有多个参数(键=值). 常见的软件格式文档格式如下: [D ...

  7. requirejs源码分析: config中shim

    shim处理的源码: //Merge shim                 if (cfg.shim) {                     eachProp(cfg.shim, funct ...

  8. 深度学习3--caffe的安装(only CPU)

    1. 本来按照视频走的,但是在cmake的时候报错,然后参考了这篇文章,稀里糊涂的就好了,总结就是把“视频/本文”说的依赖都安装上,就可以了,先安装opencv,再安装caffe第三方依赖 在安装ca ...

  9. 函数没有返回值,默认返回undefined

    var  a =( function(){return})(); a = undefined;

  10. 前端之 Ajax(补)

    概述 对于WEB应用程序:用户浏览器发送请求,服务器接收并处理请求,然后返回结果,往往返回就是字符串(HTML),浏览器将字符串(HTML)渲染并显示浏览器上. 1.传统的Web应用 一个简单操作需要 ...