题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1537

思路:一开始用二项式拆了一下,发现这个式子的形式总能变成a+b*sqrt(2)的形式。然后就列了几个。

1+1*sqrt(2)

3+2*sqrt(2)

7+5*sqrt(2)

17+12*sqrt(2)

挺明显的:ai=ai-1+2*bi-1,bi=ai-1+bi-1。

转移的矩阵:

1 2

1 1

起初我是怀疑这个题是有bug的,因为输出的结果要取模,但是我的代码是求出了未平方的值,但是这个时候如果平方溢出了呢?再取模会不会有问题?不过仔细一想,平方后对1e9+7取模结果一定小于它,所以它们的相对位置是统一的。推算出结果,判断一发a和b的顺序就行了。

 #include <bits/stdc++.h>
using namespace std; typedef long long LL;
const LL mod = (LL)1e9+;
const int maxn = ;
LL n; typedef struct Matrix {
LL m[maxn][maxn];
int r;
int c;
Matrix(){
r = c = ;
memset(m, , sizeof(m));
}
} Matrix; Matrix mul(Matrix m1, Matrix m2) {
Matrix ans = Matrix();
ans.r = m1.r;
ans.c = m2.c;
for(int i = ; i <= m1.r; i++) {
for(int j = ; j <= m2.r; j++) {
for(int k = ; k <= m2.c; k++) {
if(m2.m[j][k] == ) continue;
ans.m[i][k] = ((ans.m[i][k] + m1.m[i][j] * m2.m[j][k] % mod) % mod) % mod;
}
}
}
return ans;
} Matrix quickmul(Matrix m, LL n) {
Matrix ans = Matrix();
for(int i = ; i <= m.r; i++) {
ans.m[i][i] = ;
}
ans.r = m.r;
ans.c = m.c;
while(n) {
if(n & ) ans = mul(m, ans);
m = mul(m, m);
n >>= ;
}
return ans;
} int main() {
//freopen("in", "r", stdin);
while(~scanf("%lld", &n)) {
Matrix p; p.c = ; p.r = ;
p.m[][] = ; p.m[][] = ;
p.m[][] = ; p.m[][] = ;
Matrix q = quickmul(p, n);
LL a = q.m[][], b = q.m[][];
a = (a * a) % mod;
b = (((b * b) % mod) * ) % mod;
if(a == b + ) printf("%lld\n", a);
else if(a + == b) printf("%lld\n", b);
else puts("no");
}
return ;
}

[51NOD1537] 分解(递推,矩阵快速幂)的更多相关文章

  1. HDU 5950 Recursive sequence 【递推+矩阵快速幂】 (2016ACM/ICPC亚洲区沈阳站)

    Recursive sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  2. hdu 2604 递推 矩阵快速幂

    HDU 2604 Queuing (递推+矩阵快速幂) 这位作者讲的不错,可以看看他的 #include <cstdio> #include <iostream> #inclu ...

  3. HDU 2842 (递推+矩阵快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2842 题目大意:棒子上套环.第i个环能拿下的条件是:第i-1个环在棒子上,前i-2个环不在棒子上.每个 ...

  4. Recursive sequence HDU - 5950 (递推 矩阵快速幂优化)

    题目链接 F[1] = a, F[2] = b, F[i] = 2 * F[i-2] + F[i-1] + i ^ 4, (i >= 3) 现在要求F[N] 类似于斐波那契数列的递推式子吧, 但 ...

  5. HDU6030 Happy Necklace(递推+矩阵快速幂)

    传送门:点我 Little Q wants to buy a necklace for his girlfriend. Necklaces are single strings composed of ...

  6. 五校联考R1 Day1T3 平面图planar(递推 矩阵快速幂)

    题目链接 我们可以把棱柱拆成有\(n\)条高的矩形,尝试递推. 在计算的过程中,第\(i\)列(\(i\neq n\))只与\(i-1\)列有关,称\(i-1\)列的上面/下面为左上/左下,第\(i\ ...

  7. LightOJ 1244 - Tiles 猜递推+矩阵快速幂

    http://www.lightoj.com/volume_showproblem.php?problem=1244 题意:给出六种积木,不能旋转,翻转,问填充2XN的格子有几种方法.\(N < ...

  8. [递推+矩阵快速幂]Codeforces 1117D - Magic Gems

    传送门:Educational Codeforces Round 60 – D   题意: 给定N,M(n <1e18,m <= 100) 一个magic gem可以分裂成M个普通的gem ...

  9. 2017中国大学生程序设计竞赛 - 女生专场 Happy Necklace(递推+矩阵快速幂)

    Happy Necklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) ...

  10. [hdu 2604] Queuing 递推 矩阵快速幂

    Problem Description Queues and Priority Queues are data structures which are known to most computer ...

随机推荐

  1. Server编解码 解决Response.Redirect方法传递汉字丢失或乱码

  2. 如何通过类找到对应的jar包

    ctrl+shift+T 然后输入对应类  

  3. javascript 正则表达式(二)

    /* 正则表达式方法:test(),exec(),String对象方法:match(),search(),replace(),split() 1.test()方法: 用法:  regexp对象实例.t ...

  4. 为 Macbook 安装 wget 命令

    没想到装个这个命令那么麻烦, 还要装 xcode?..... 好吧,按步骤来成功装上 http://www.arefly.com/mac-wget/ 其實Mac也自帶了一個Curl同樣也可以下載網頁, ...

  5. jython学习笔记2

    1.%是求余,//是整除的商,**是乘方 abs(var) Absolute value pow(x, y) Can be used in place of ** operator pow(x,y,m ...

  6. 各种drawable

    BitmapDrawable   可以把小图片平铺布满布局来设置背景. PictureDrawable    记录绘制过程 ClipDrawable     裁剪   进度条 InsetDrawabl ...

  7. [BIM]BIM中IFD介绍

    第三大支柱IFD - 确定交换的信息和你要的信息是同一个东西 IFD的全称是International Framework for Dictionaries,中文可以叫“国际字典框架”,和前两者IFC ...

  8. ACM学习之路————一个大整数与一个小整数不得不说得的秘密

    这个相对于两个大整数的运算来说,只能说是,low爆了. 只要利用好除法的性质,这类题便迎刃而解.O(∩_∩)O哈哈~ //大整数除一个int数 #include<iostream> #in ...

  9. android内存优化相关1

    第一种策略,是释放显示相关的内存.这是我们针对系统APP采用的一种调优策略. 图形内容,俗称位图是非常占用内存的,针对位图,我们采用异步加载的方法,将位图内容信息和位图的状态信息分别进行存储,将内容信 ...

  10. c#对话框

    OpenFileDialog open = new OpenFileDialog();//创建对话框 open.InitialDirectory = @"C:\Documents and S ...