HDU - 4990 Reading comprehension 【矩阵快速幂】
2024-10-11 23:34:11
题目链接
http://acm.hdu.edu.cn/showproblem.php?pid=4990
题意
初始的ans = 0
给出 n, m
for i in 1 -> n
如果 i 为奇数
ans = (ans * 2 + 1) % m
反之
ans = ans * 2 % m
思路
如果我们只计算 偶数项 那么递推公式就是
ans[n] = 4 * ans[n - 2] + 2
如果 n 是偶数 那么刚好 就按这个公式推 第 n / 2 项
如果 n 是奇数 那么就是 第 【 n / 2 项 】 * 2 + 1
可以推知的矩阵为
然后矩阵快速幂
AC代码
#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits>
#define CLR(a, b) memset(a, (b), sizeof(a))
#define pb push_back
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair<string, int> psi;
typedef pair<string, string> pss;
const double PI = acos(-1.0);
const double E = exp(1.0);
const double eps = 1e-8;
const int INF = 0x3f3f3f3f;
const int maxn = 1e2 + 5;
//const int MOD = 1e4;
int MOD;
struct Matrix
{
ll a[2][2];
Matrix () {}
Matrix operator * (Matrix const &b)const
{
Matrix res;
CLR(res.a, 0);
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
for (int k = 0; k < 2; k++)
res.a[i][j] = (res.a[i][j] + this->a[i][k] * b.a[k][j]) % MOD;
return res;
}
};
Matrix pow_mod(Matrix base, int n)
{
Matrix ans;
CLR(ans.a, 0);
ans.a[0][1] = 1;
while (n > 0)
{
if (n & 1)
ans = ans * base;
base = base * base;
n >>= 1;
}
return ans;
}
int main()
{
Matrix base;
base.a[0][0] = 4;
base.a[0][1] = 0;
base.a[1][0] = 2;
base.a[1][1] = 1;
int n;
while (~scanf("%d%d", &n, &MOD))
{
Matrix ans = pow_mod(base, n / 2);
if (n & 1)
printf("%lld\n", (ans.a[0][0] * 2 + 1) % MOD);
else
printf("%lld\n", ans.a[0][0] % MOD);
}
}
最新文章
- Pycharm 快捷键
- 【NodeJS 学习笔记01】不学就老了
- SQL语句实现取消自增列属性
- topcoder-srm701-div2-900 博弈\计算二进制位1的个数\dp\状态压缩
- 给jdk写注释系列之jdk1.6容器(2)-LinkedList源码解析
- (step4.3.8)hdu 2181(哈密顿绕行世界问题——DFS)
- C++ Primer中文版(第5版)
- C#中的动态特性
- mybatis的那些事
- android仿漫画源码、抽奖转盘、Google相册、动画源码等
- 大战Java虚拟机【0】——目录
- K8S 部署 ingress-nginx (二) 部署后端为 tomcat
- 【开源GPS追踪】 之 硬件开源
- linux之常见命令
- QML获取随机颜色
- uoj #14.【UER #1】DZY Loves Graph
- fiddler 手机 https 抓包 以及一些fiddler无法解决的https问题http2、tcp、udp、websocket证书写死在app中无法抓包
- 【高德地图SDK】如何实现轨迹平滑移动?
- C# Log4net根据日志等级输出到不同文件
- oracle:ORA-00911: 无效字符 问题和解决---Eclipse中的SQL语句不能加分号