UVaLive 7361(矩阵快速幂)
题意:矩阵快速幂求斐波那契数列。
分析:
#include<cstdio> #include<cstring> #include<cctype> #include<cstdlib> #include<cmath> #include<iostream> #include<sstream> #include<iterator> #include<algorithm> #include<string> #include<vector> #include<set> #include<map> #include<deque> #include<queue> #include<stack> #include<list> #define fin freopen("in.txt", "r", stdin) #define fout freopen("out.txt", "w", stdout) #define pr(x) cout << #x << " : " << x << " " #define prln(x) cout << #x << " : " << x << endl typedef long long ll; typedef unsigned long long llu; const int INT_INF = 0x3f3f3f3f; const int INT_M_INF = 0x7f7f7f7f; const ll LL_INF = 0x3f3f3f3f3f3f3f3f; const ll LL_M_INF = 0x7f7f7f7f7f7f7f7f; const double pi = acos(-1.0); const double EPS = 1e-6; const int dx[] = {0, 0, -1, 1}; const int dy[] = {-1, 1, 0, 0}; const ll MOD = 1e9; const int MAXN = 1000 + 10; const int MAXT = 10000 + 10; using namespace std; struct Node {ll c[2][2];Node(){memset(c, 0, sizeof c);} }; Node multi(Node &a, Node &b) {Node ans;for(int i = 0; i < 2; ++i)for(int j = 0; j < 2; ++j)for(int k = 0; k < 2; ++k){ans.c[i][j] += (a.c[i][k] * b.c[k][j]);ans.c[i][j] %= MOD;}return ans; } ll q_pow(ll cur) {Node a;a.c[0][0] = a.c[0][1] = a.c[1][0] = 1;Node ans;ans.c[0][0] = ans.c[1][1] = 1;while(cur){if(cur & 1){ans = multi(ans, a);}a = multi(a, a);cur /= 2;}return ans.c[0][0]; } int main() {int P;scanf("%d", &P);while(P--){int K;ll Y;scanf("%d%lld", &K, &Y);printf("%d %lld\n", K, q_pow(Y - 1));}return 0; }
转载于:https://www.cnblogs.com/tyty-Somnuspoppy/p/5971287.html
UVaLive 7361(矩阵快速幂)相关推荐
- 矩阵快速幂+构造方法
与快速幂一样,可以将递推式通过二进制的方式来进行优化,这个学了快速幂就是十分容易理解 大概的板子如下: struct mat///自己定义大小的矩阵 {ll m[11][11]; }; mat mul ...
- 【做题】SRM701 Div1 Hard - FibonacciStringSum——数学和式&矩阵快速幂
原文链接 https://www.cnblogs.com/cly-none/p/SRM701Div1C.html 题意:定义"Fibonacci string"为没有连续1的01串 ...
- 快速幂 + 矩阵快速幂
快速幂 1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 #define LL lo ...
- HDU4549(矩阵快速幂+快速幂)
f(n)=a^f(n-1) + b^f(n-2):计算矩阵部分用矩阵快速幂:计算a的幂次和b的幂次用快速幂. #include<iostream> #include<algorith ...
- [HNOI2008]GT考试[矩阵快速幂+kmp优化的dp]
解题思路:假如说我们用f[i]表示长度为i的串能组合成无不吉利数字的组合的个数的话我们无法找到f[i]和f[i+1]的关系,就是我们下一位填某个数字会不会出现不吉利串,这就和你前面的串末尾于不吉利串重 ...
- I-Matrix Power Series POJ - 3233 矩阵快速幂+分治
I-Matrix Power Series POJ - 3233 矩阵快速幂+分治 Problem Description Given a n × n matrix A and a positive ...
- H - Fibonacci POJ - 3070 (矩阵快速幂)
H - Fibonacci POJ - 3070 (矩阵快速幂) Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and ...
- HDU 6185 Covering 矩阵快速幂 递推
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=6185 题目描述: 一个4*n的矩形, 你用1*2的矩形覆盖有多少种方案, n <= 1e18 ...
- bzoj 1409 Password 矩阵快速幂+欧拉函数
可以发现,该数组的mi就是斐波那契数列 所以要矩阵快速幂搞出第n位 但是斐波那契数列上涨的很快,这就需要欧拉定理了 p^phi(q)%q=1(gcd(p,q)==1) p是素数,所以可以用 然后需要5 ...
最新文章
- 业界 | 每天1.4亿小时观看时长,Netflix怎样存储这些时间序列数据?
- * ngIf和* ngFor在同一元素上导致错误
- 网页的手机版本是否值得去做?,互联网营销
- C++字符串之一(字符表示)
- java 代码scope注解_Spring学习(15)--- 基于Java类的配置Bean 之 @Bean @Scope 注解
- [十二省联考2019]字符串问题 后缀自动机 + 拓扑排序 + 最长路 + 倍增
- Nexus修改admin密码及其添加用户
- cookie怎样存储数据?
- Web 学习之跨域问题及解决方案
- Trick(十三)—— 数学与函数
- Unity 获得某个物体的主贴图
- linux系统论文题目大全_操作系统专业论文选题 操作系统毕业论文题目如何定
- 手机哔哩哔哩缓存的m4s格式的视频转换成mp4
- JZOJ.5289【NOIP2017模拟8.17】偷笑
- 最近有朋友问我,如何在自媒体上快速发文章?
- 十进制转为二十六进制
- Java swing 写会员卡管理系统
- assign和weak之间的区别
- 数据分析案例——客户流失分析与预测
- 三角函数诱导公式大全
热门文章
- 【MFC】状态栏随对话框的改变而改变
- 提高抗打击能力_如果提高心理抗打击能力?
- Qt5.0中lineEdit的输入输出
- caffe error: #error This file requires compiler and library support for the ISO C++ 2011 standard
- 系统仿真基础与计算机实现,计算机综合仿真实验系统的研究与开发
- 汇编-debug结果正确与执行结果错误
- 双向链表简单实现及图示
- Wannafly挑战赛17 - 求值2 (逆元 + 杨辉三角公式)
- count相加 sqlserver_mssql sqlserver sum(1)、sum(2)、count(1)、count(8)、count(*)、统计函数应用区别简介...
- Paxos、Raft分布式一致性算法应用场景