Another kind of Fibonacci

【题目链接】Another kind of Fibonacci

【题目类型】矩阵+ll超时必须用int&输入必须取模&M必须是int类型

&题解:

算出矩阵的每一行一定要和初始化的那个矩阵不一样,如果有一项是一样的,那么就推不出最后的答案,所以矩阵如下:



代码还有问题,T了,不知道为什么,明天在看吧

【时间复杂度】\(O(logn)\)

&超时代码:

#include <cstdio>
#include <bitset>
#include <iostream>
#include <set>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <map>
#include <queue>
#include <vector>
using namespace std;
#define INF 0x3f3f3f3f
typedef long long ll;
const int si= 6;
struct mat
{
ll m[si][si];
}A;
ll n,x,y,M=10007;
mat Mul(mat a,mat b)
{
mat c;
for(int i=0;i<si;i++)
for(int j=0;j<si;j++){
c.m[i][j]=0;
for(int k=0;k<si;k++)
c.m[i][j]=(c.m[i][j]+a.m[i][k]*b.m[k][j])%M;
}
return c;
}
mat bPow(mat a,ll z)
{
mat un;
for(int i=0;i<si;i++)for(int j=0;j<si;j++)
un.m[i][j]=(i==j);
while(z){
if(z&1)
un=Mul(un,a);
a=Mul(a,a);
z>>=1;
}
return un;
}
ll tb[si];
void Init()
{
for(int i=0;i<5;i++)tb[i]=1;
tb[5]=2;
memset(A.m,0,sizeof(A.m));
A.m[0][0]=x,A.m[0][1]=y;
A.m[1][0]=1;
A.m[2][2]=x*x,A.m[2][3]=y*y,A.m[2][4]=2*x*y;
A.m[3][2]=1;
A.m[4][2]=x,A.m[4][4]=y;
for(int i=0;i<si;i++) A.m[5][i]=A.m[2][i];
A.m[5][5]=1;
}
void DF(mat a)
{
for(int i=0;i<si;i++){
for(int j=0;j<si;j++)
cout<<a.m[i][j]<<" ";
cout<<endl;
}
}
int main()
{
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
freopen("E:1.txt","r",stdin);
while(cin>>n>>x>>y){
x%=M,y%=M;
Init();
// DF(A);
ll ans=0;
if(n==0){
cout<<1<<endl;
}
else if(n==1){
cout<<2<<endl;
}
else{
A=bPow(A,n-1);
// DF(A);
for(int i=0;i<si;i++){
ans=(ans+A.m[5][i]*tb[i])%M;
}
cout<<ans<<endl;
}
}
return 0;
}

&题解2:

妈的,这题真tm智障,花了我2个小时,最后终于对照别人ac代码找到了错误.

注意1:矩阵中是long long 类型 运算没有int快 所以要改为int型 还有取模的M也必须要是int型 如果M是ll的话也会超时.

注意2:相应地,上面由long long改为int了 所以输入数据必须要取模了

总结:好智障的烂题啊,居然卡在数据类型上.(当然,也应该注意一下了:能用int的就不要用long long,因为如果用了long long 就有可能超时;原来一直以为用ll不会爆范围,就总是用ll,现在发现了,一直用ll会爆时间,尤其是在这种矩阵快速幂的题里,绝对要注意!!!)

&AC代码:

#include <cstdio>
#include <bitset>
#include <iostream>
#include <set>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <map>
#include <queue>
#include <vector>
using namespace std;
#define INF 0x3f3f3f3f
typedef long long ll;
const int si= 6;
struct mat
{
int m[si][si];
}A;
ll n,x,y;
int M=10007;
mat Mul(mat a,mat b)
{
mat c;
for(int i=0;i<si;i++)
for(int j=0;j<si;j++){
c.m[i][j]=0;
for(int k=0;k<si;k++)
c.m[i][j]=(c.m[i][j]+a.m[i][k]*b.m[k][j])%M;
}
return c;
}
mat bPow(mat a,ll z)
{
mat un;
for(int i=0;i<si;i++)for(int j=0;j<si;j++)
un.m[i][j]=(i==j);
while(z){
if(z&1)
un=Mul(un,a);
a=Mul(a,a);
z>>=1;
}
return un;
}
ll tb[si];
void Init()
{
for(int i=0;i<5;i++)tb[i]=1;
tb[5]=2;
memset(A.m,0,sizeof(A.m));
A.m[0][0]=x,A.m[0][1]=y;
A.m[1][0]=1;
A.m[2][2]=x*x%M,A.m[2][3]=y*y%M,A.m[2][4]=2*x*y%M;
A.m[3][2]=1;
A.m[4][2]=x,A.m[4][4]=y;
for(int i=0;i<si;i++) A.m[5][i]=A.m[2][i];
A.m[5][5]=1;
}
void DF(mat a)
{
for(int i=0;i<si;i++){
for(int j=0;j<si;j++)
cout<<a.m[i][j]<<" ";
cout<<endl;
}
}
int main()
{
while(cin>>n>>x>>y){
x%=M,y%=M;
Init();
ll ans=0;
if(n==0){
cout<<1<<endl;
}
else if(n==1){
cout<<2<<endl;
}
else{
A=bPow(A,n-1);
for(int i=0;i<si;i++){
ans=(ans+A.m[5][i]*tb[i])%M;
}
cout<<ans<<endl;
}
}
return 0;
}

HDU 3306 Another kind of Fibonacci(矩阵+ll超时必须用int&输入必须取模&M必须是int类型)的更多相关文章

  1. hdu 3306 Another kind of Fibonacci 矩阵快速幂

    参考了某大佬的 我们可以根据(s[n-2], a[n-1]^2, a[n-1]*a[n-2], a[n-2]^2) * A = (s[n-1], a[n]^2, a[n]*a[n-1], a[n-1] ...

  2. HDU 3306 Another kind of Fibonacci ---构造矩阵***

    Another kind of Fibonacci Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Jav ...

  3. hdu 3306 Another kind of Fibonacci(矩阵高速幂)

    Another kind of Fibonacci                                                        Time Limit: 3000/10 ...

  4. HDU 3306 Another kind of Fibonacci(快速幂矩阵)

    题目链接 构造矩阵 看的题解,剩下的就是模板了,好久没写过了,注意取余. #include <cstring> #include <cstdio> #include <s ...

  5. HDU 3306 - Another kind of Fibonacci

    给你 A(0) = 1 , A(1) = 1 , A(N) = X * A(N - 1) + Y * A(N - 2) (N >= 2). 求 S(N) = A(0) 2 +A(1) 2+……+ ...

  6. hdu 1097 A hard puzzle 快速幂取模

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1097 分析:简单题,快速幂取模, 由于只要求输出最后一位,所以开始就可以直接mod10. /*A ha ...

  7. hdu 1588(Fibonacci矩阵求和)

    题目的大意就是求等差数列对应的Fibonacci数值的和,容易知道Fibonacci对应的矩阵为[1,1,1,0],因为题目中f[0]=0,f[1]=1,所以推出最后结果f[n]=(A^n-1).a, ...

  8. POJ3070 Fibonacci[矩阵乘法]

    Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13677   Accepted: 9697 Descri ...

  9. HDU 5895 Mathematician QSC(矩阵乘法+循环节降幂+除法取模小技巧+快速幂)

    传送门:HDU 5895 Mathematician QSC 这是一篇很好的题解,我想讲的他基本都讲了http://blog.csdn.net/queuelovestack/article/detai ...

随机推荐

  1. [No0000D0] 让你效率“猛增十倍”,沉浸工作法到底是什么?

    一位编剧在三天内完成两万字的剧本,而在此之前,他曾拖延了足足半年.一名大四学生用一天半写了8000多字,一鼓作气拿下毕业论文. 有人说:“用了这个方法,我的效率猛增十倍.只用短短两小时,就摧枯拉朽地完 ...

  2. python中OrderedDict的使用

    很多人认为python中的字典是无序的,因为它是按照hash来存储的,但是python中有个模块collections(英文,收集.集合),里面自带了一个子类 OrderedDict,实现了对字典对象 ...

  3. 深入理解无穷级数和的定义(the sum of the series)

    Given an infinite sequence (a1, a2, a3, ...), a series is informally the form of adding all those te ...

  4. Apache Sharding-Sphere

    Sharding-Sphere 正式步入 Apache 基金会孵化器 - 开源中国 https://www.oschina.net/news/101691/sharding-sphere-enter- ...

  5. Xcode 编辑器之Workspace,Project,Scheme,Target

    一,前言 最近老是突然对Workspace,Project,Scheme,Target四者的关系有些疑惑,所以查阅资料总结一下. 二,Workspace,Project,Scheme,Target四者 ...

  6. [knowledge][模式匹配] 字符匹配/模式匹配 正则表达式 自动机

    字符串 T = abcabaabcabac,字符串 P = abaa,判断P是否是T的子串,就是字符串匹配问题了,T 叫做文本(Text) ,P 叫做模式(Pattern),所以正确描述是,找出所有在 ...

  7. 那些年读过的书《Java并发编程实战》二、如何设计线程安全类

    1.设计线程安全类的过程 设计线程安全类的过程就是设计对象状态并发访问下线程间的协同机制(在不破坏对象状态变量的不变性条件的前提下). (1)构建线程安全类的三个基本要素: 1)找出构成对象状态的所有 ...

  8. python基础数据类型考试题

    Python基础数据类型考试题 考试时间:两个半小时                      满分100分(80分以上包含80分及格) 一,基础题. 1,简述变量命名规范(3分) 2,字节和位的关系 ...

  9. springmvc拦截器实现用户登录权限验证

    实现用户登录权限验证 先看一下我的项目的目录,我是在intellij idea 上开发的 1.先创建一个User类 package cn.lzc.po; public class User { pri ...

  10. Python3学习之路~2.7 文件操作

    对文件操作流程 打开文件,得到文件句柄并赋值给一个变量 通过句柄对文件进行操作 关闭文件 现有文件如下 Somehow, it seems the love I knew was always the ...