hdu4990 矩阵快速幂
来源:互联网 发布:阿特金斯减肥法 知乎 编辑:程序博客网 时间:2024/04/29 00:57
题意:
给你一短代码,让你优化这个代码,代码如下
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include<iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include<vector>
const int MAX=100000*2;
const int INF=1e9;
int main()
{
int n,m,ans,i;
while(scanf("%d%d",&n,&m)!=EOF)
{
ans=0;
for(i=1;i<=n;i++)
{
if(i&1)ans=(ans*2+1)%m;
else ans=ans*2%m;
}
printf("%d\n",ans);
}
return 0;
},给出n,m让你输出ans <1<=n, m <= 1000000000>.
思路:
直接跑肯定TLE,这个题目我们可以推公式,如果推不出来可以直接打出来一些,然
后自己找公式,一般公式不会很复杂(复杂的自己一般不会呵呵)。
现在我们要求ai:
如果i是奇数
a[i] = a[i-1] * 2 + 1 = (a[i-2] * 2 + a[i-1]) + 1 = a[i-2]*2+a[i-1]+1
如果i是偶数
a[i] = a[i-1] * 2 = (a[i-2] * 2 + 1) + a[i-1] = a[i-2]*2+a[i-1]+1
两个公式一样,那么可以作为通式,然后就构造矩阵,之后跑快速幂就行了,矩阵也很好构
造,我构造下:
a1 a2 1 0 2 0 a2 a3 1
* 1 1 0
给你一短代码,让你优化这个代码,代码如下
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include<iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include<vector>
const int MAX=100000*2;
const int INF=1e9;
int main()
{
int n,m,ans,i;
while(scanf("%d%d",&n,&m)!=EOF)
{
ans=0;
for(i=1;i<=n;i++)
{
if(i&1)ans=(ans*2+1)%m;
else ans=ans*2%m;
}
printf("%d\n",ans);
}
return 0;
},给出n,m让你输出ans <1<=n, m <= 1000000000>.
思路:
直接跑肯定TLE,这个题目我们可以推公式,如果推不出来可以直接打出来一些,然
后自己找公式,一般公式不会很复杂(复杂的自己一般不会呵呵)。
现在我们要求ai:
如果i是奇数
a[i] = a[i-1] * 2 + 1 = (a[i-2] * 2 + a[i-1]) + 1 = a[i-2]*2+a[i-1]+1
如果i是偶数
a[i] = a[i-1] * 2 = (a[i-2] * 2 + 1) + a[i-1] = a[i-2]*2+a[i-1]+1
两个公式一样,那么可以作为通式,然后就构造矩阵,之后跑快速幂就行了,矩阵也很好构
造,我构造下:
a1 a2 1 0 2 0 a2 a3 1
* 1 1 0
0 1 1
#include<stdio.h>#include<string.h>__int64 M;typedef struct{ __int64 mat[5][5];}A;A mat_mat(A a ,A b){ A c; memset(c.mat ,0 ,sizeof(c.mat)); for(int k = 1 ;k <= 3 ;k ++) for(int i = 1 ;i <= 3 ;i ++) { if(a.mat[i][k]) for(int j = 1 ;j <= 3 ;j ++) c.mat[i][j] = (c.mat[i][j] + a.mat[i][k] * b.mat[k][j])%M; } return c;}A quick_mat(A a ,int b){ A c; memset(c.mat ,0 ,sizeof(c.mat)); c.mat[1][1] = c.mat[2][2] = c.mat[3][3] = 1; while(b) { if(b&1) c = mat_mat(c ,a); a = mat_mat(a ,a); b >>= 1; } return c;}int main (){ A a; int n ,i; while(~scanf("%d %d" ,&n ,&M)) { a.mat[1][1] = a.mat[1][3] = a.mat[2][3] = a.mat[3][1] = 0; a.mat[2][1] = a.mat[2][2] = a.mat[3][2] = a.mat[3][3] = 1; a.mat[1][2] = 2; if(n == 1) { printf("%d\n" ,1 % M); continue; } a = quick_mat(a ,n-1); __int64 Ans = 1 * a.mat[1][1] + 2 * a.mat[2][1] + 1 * a.mat[3][1]; printf("%I64d\n" ,Ans % M); } return 0;}
0 0
- hdu4990 矩阵快速幂
- hdu4990---Reading comprehension(矩阵快速幂)
- HDU4990 Reading comprehension【矩阵快速幂】
- hdu4990——多解矩阵快速幂
- [HDU4990] 等比公式+取模运算公式 或矩阵求递推法~
- 快速矩阵快速幂
- 转移矩阵+矩阵快速幂
- 矩阵乘法 矩阵快速幂
- 构造矩阵+矩阵快速幂
- 矩阵快速幂,矩阵加法,矩阵乘法
- 快速幂||矩阵快速幂
- 快速幂&矩阵快速幂
- 快速幂,矩阵快速幂
- 快速幂 矩阵快速幂
- 快速幂&矩阵快速幂
- 【快速幂】【矩阵快速幂】
- 快速幂和快速矩阵
- poj3070--矩阵 快速幂
- Oracle rowid 详解
- 无需购买服务器,Android前端开发如何快速接入Bmob后端云
- 也谈暗时间
- 使用sp_addmessage添加自定义消息
- 找出数组中两个只出现一次的数字
- hdu4990 矩阵快速幂
- c++析构函数
- 使用动态性能视图调优实例
- MT6573_android_LCD_backlight_Driver
- fork与vfork的区别
- 测试
- Access restriction: The type TaskTopicResolver is not accessible due to restrict
- c# 动态加载和卸载C#DLL程序集 C++程序集,
- openIMSCore学习