Reading comprehension HDU - 4990 (矩阵快速幂 or 快速幂+等比数列)
for(i=;i<=n;i++)
{
if(i&)ans=(ans*+)%m;
else ans=ans*%m;
}
给定n,m。让你用O(log(n))以下时间算出ans。
打表,推出 ans[i] = 2^(i-1) + f[i-2]
故 i奇数:ans[i] = 2^(i-1) + 2^(i-3) ... + 1;
i偶数:ans[i] = 2^(i-1) + 2^(i-3) ... + 2;
故可以用等比数列求和公式。
公式涉及除法。我也没弄懂为啥不能用逆元,貌似说是啥逆元可能不存在。
所以a/b % m == a%(b*m) / b 直接搞了。
#include <cstdio>
#include<iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include<vector>
typedef long long LL; LL quick_pow(LL a, LL b, LL m)
{
LL ans = , base = a % m;
while(b)
{
if (b & ) ans = (ans * base) % m;
base = (base * base) % m;
b >>= ;
}
return ans;
} int main()
{
LL n, m; while(~scanf("%lld%lld", &n, &m))
{
LL a1 = (n % ) ? : ;
LL sum = a1 * (quick_pow(, (n+)/, *m) - );
LL ans = (sum % ( * m)) / ;
printf("%lld\n", ans);
}
}
第一次学矩阵快速幂,再贴个矩阵快速幂的板子。
f[n] = f[n-1] + 2 * f[n-2] + 1,故可以构造矩阵
f[n-] f[n-] f[n-] f[n]
=
模板来源:https://blog.csdn.net/u012860063/article/details/39123605
#include <cstdio>
#include<iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include<vector>
typedef long long LL; struct Matrix
{
LL m[][];
} I, A, B, T; LL a,b,n, mod;
int ssize = ; Matrix Mul(Matrix a,Matrix b)
{
int i,j,k;
Matrix c;
for (i = ; i <= ssize; i++)
for(j = ; j <= ssize; j++)
{
c.m[i][j]=;
for(k = ; k <= ssize; k++)
{
c.m[i][j]+=(a.m[i][k]*b.m[k][j]);
c.m[i][j]%=mod;
}
} return c;
} Matrix quickpagow(int n)
{
Matrix m = A, b = I;
while(n)
{
if(n & ) b = Mul(b, m);
n >>= ;
m = Mul(m, m);
}
return b;
} int main()
{
while(~scanf("%lld%lld",&n, &mod))
{
memset(I.m,,sizeof(I.m));
memset(A.m,,sizeof(A.m));
memset(B.m,,sizeof(B.m)); for(int i = ; i <= ssize; i++) I.m[i][i] = ;
//I是单位矩阵 B.m[][] = , B.m[][] = , B.m[][] = ;
A.m[][] = ;
A.m[][] = A.m[][] = A.m[][] = A.m[][] = ; if(n == ) printf("%lld\n", % mod);
else if(n == ) printf("%lld\n", % mod);
else
{
T = quickpagow(n-);
T = Mul(B, T);
printf("%lld\n",T.m[][] % mod);
}
}
}
Reading comprehension HDU - 4990 (矩阵快速幂 or 快速幂+等比数列)的更多相关文章
- Reading comprehension HDU - 4990
Read the program below carefully then answer the question. #pragma comment(linker, "/STACK:1024 ...
- HDU4990 Reading comprehension —— 递推、矩阵快速幂
题目链接:https://vjudge.net/problem/HDU-4990 Reading comprehension Time Limit: 2000/1000 MS (Java/Others ...
- hdu-4990 Reading comprehension(快速幂+乘法逆元)
题目链接: Reading comprehension Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- 【模拟题(电子科大MaxKU)】解题报告【树形问题】【矩阵乘法】【快速幂】【数论】
目录: 1:一道简单题[树形问题](Bzoj 1827 奶牛大集会) 2:一道更简单题[矩阵乘法][快速幂] 3:最简单题[技巧] 话说这些题目的名字也是够了.... 题目: 1.一道简单题 时间1s ...
- 求幂大法,矩阵快速幂,快速幂模板题--hdu4549
hdu-4549 求幂大法.矩阵快速幂.快速幂 题目 M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 ...
- hdu 4291 矩阵幂 循环节
http://acm.hdu.edu.cn/showproblem.php?pid=4291 凡是取模的都有循环节-----常数有,矩阵也有,并且矩阵的更奇妙: g(g(g(n))) mod 109 ...
- Cognitive Graph for Multi-Hop Reading Comprehension at Scale(ACL2019) 阅读笔记与源码解析
论文地址为:Cognitive Graph for Multi-Hop Reading Comprehension at Scale github地址:CogQA 背景 假设你手边有一个维基百科的搜索 ...
- 快速幂 ,快速幂优化,矩形快速幂(java)
快速幂形式 public static int f(int a,int b,int c){ int ans =1; int base=a; while(b!=0){ if((b&1)!=0) ...
- 论文选读二:Multi-Passage Machine Reading Comprehension with Cross-Passage Answer Verification
论文选读二:Multi-Passage Machine Reading Comprehension with Cross-Passage Answer Verification 目前,阅读理解通常会给出 ...
随机推荐
- Web 前端安装依赖的时候遇到的问题
- 认识与入门 MarkDown (转Te_Lee)
Markdown 是一种轻量级的「标记语言」,它的优点很多,目前也被越来越多的写作爱好者,撰稿者广泛使用.看到这里请不要被「标记」.「语言」所迷惑,Markdown 的语法十分简单.常用的标记符号也不 ...
- a href="javascript:"与a href="#"
<a href="javascript:;"></a> <a href="#"></a> 这两种写法.这两种写法 ...
- vue-cli建立的项目如何在手机端运行以及如何用charles来抓包
刚开始自己在config文件夹下的index.js中的dev下的host写成的是localhost,但是发现自己不能在手机端访问,并且也不可以在charles进行抓包处理,后来把localhost改成 ...
- 网页编辑器CKEditor4.3.1+CKFinder2.4+JW Player6.7(视频播放器)集成
CKEditor是使用最多的一款在线网页编辑器,不仅好用,而且功能强大.易扩展.浏览器兼容性好.另外,CKEditor网页编辑器经常更新.本程序使用的是最新稳定版CKEditor4.3.1,添加使用了 ...
- 美国L1签证申请的常见问题解析
美国L1是一种允许在美国和中国都有机构的跨国公司从国外的母公司派遣一定层次的经理或专业技术人员去美国分支机构工作的非移民签证.L1签证分两类:美国L1A是跨国公司经理及主管人员签证,L1B是专门技术人 ...
- HDU5152 线段树 + 数论
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5152 ,线段树区间更新 + 点更新 + 数论知识(数论是重点QAQ),好题值得一做. BestCode ...
- 如何用WebIDE打开并运行CRM Fiori应用
访问Web IDE url 在Web IDE里进行项目clone操作: https://:8080/#/admin/projects/fnf/customer/cus.crm.opportunity ...
- NYOJ-596-谁是最好的Coder
原题链接 谁是最好的Coder 时间限制:1000 ms | 内存限制:65535 KB 难度:0 描述 计科班有很多Coder,帅帅想知道自己是不是综合实力最强的coder. 帅帅喜欢帅,所以他 ...
- POJ 3281 Dining(网络流最大匹配)
分析: 数学模型是三个集合A,B,C,(a,b,c)构成一个匹配.因为图一个点只能匹配一次,把a拆点a',a", 在可以匹配的点上连边,s - b - a' - a" - c - ...