题目链接

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);
}
}

最新文章

  1. Pycharm 快捷键
  2. 【NodeJS 学习笔记01】不学就老了
  3. SQL语句实现取消自增列属性
  4. topcoder-srm701-div2-900 博弈\计算二进制位1的个数\dp\状态压缩
  5. 给jdk写注释系列之jdk1.6容器(2)-LinkedList源码解析
  6. (step4.3.8)hdu 2181(哈密顿绕行世界问题——DFS)
  7. C++ Primer中文版(第5版)
  8. C#中的动态特性
  9. mybatis的那些事
  10. android仿漫画源码、抽奖转盘、Google相册、动画源码等
  11. 大战Java虚拟机【0】——目录
  12. K8S 部署 ingress-nginx (二) 部署后端为 tomcat
  13. 【开源GPS追踪】 之 硬件开源
  14. linux之常见命令
  15. QML获取随机颜色
  16. uoj #14.【UER #1】DZY Loves Graph
  17. fiddler 手机 https 抓包 以及一些fiddler无法解决的https问题http2、tcp、udp、websocket证书写死在app中无法抓包
  18. 【高德地图SDK】如何实现轨迹平滑移动?
  19. C# Log4net根据日志等级输出到不同文件
  20. oracle:ORA-00911: 无效字符 问题和解决---Eclipse中的SQL语句不能加分号

热门文章

  1. 【GLSL教程】(一)图形流水线 【转】
  2. Python代码优化概要
  3. Zookeeper协调分布式节点demo
  4. Android之——流量管理程序演示样例
  5. springBoot与多数据源的配置
  6. vue2.X computed 计算属性
  7. log4j email EmailDailyRollingFileAppender
  8. vue proxyTable
  9. win10 1709正式版iso镜像下载|windows10 1709秋季创意者更新官方下载地址
  10. Chrome自带恐龙小游戏的源码研究(完)