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. Catch That Cow POJ - 3278 bfs map超时,短路判断顺序。

    题意:可以把n边为n+1,n-1,n*2问从n到k的最少变化次数. 坑:标题写了.有点不会写bfs了... ac代码 #define _CRT_SECURE_NO_WARNINGS #include& ...

  2. 为什么“how to say”是错的?

    2018-04-26 15:53 英语口语 吉米老师前言:如果让老外评选十大Chinglish之最,老师猜"how to say"一定榜上有名.几乎每一位学习英语的童鞋,都曾有过脱 ...

  3. MyBati__mapper 中取值(#{} 或${}) 以及 parameterType为(基本类型 或复杂类型)

    参考资料: MyBatis学习笔记(三)——parameterType为基本类型时的使用方法 MyBatis的传入参数parameterType类型 1. MyBatis的传入参数parameterT ...

  4. 创建日志表记录DML操作和DDL操作

    创建一个日志表,记录dept表的DML操作 create table dept_log(logid number,type varchar2(50),logdate date,deptno numbe ...

  5. spring mvc 资源映射配置

    在springmvc配置文件中添加 <mvc:resources location="/css/" mapping="/css/**"/> < ...

  6. Docker 镜像(五)

    我们都知道,操作系统分为内核和用户空间.对于 Linux 而言,内核启动后,会挂载 root 文件系统为其提供用户空间支持.而 Docker 镜像(Image),就相当于是一个 root 文件系统.比 ...

  7. 类的copy和deepcopy

    - (instancetype)initWithName:(NSString *)name age:(NSUInteger)age sex:(CYLSex)sex { if(self = [super ...

  8. java执行字符串数学表达式【记录】

    https://stackoverflow.com/questions/3422673/evaluating-a-math-expression-given-in-string-form 1. goo ...

  9. Tcpdump MySQL Query

    在MySQL线上环境我们一般只打开了binary log,slow log,有时我们需要查看general log呢?因为该log记录所有的请求,打开该日志肯定给磁盘造成很大压力,IO能力有所下降,所 ...

  10. MySQL常用SQL语句优化

    推荐阅读这篇博文,索引说的非常详细到位:http://blog.linezing.com/?p=798#nav-3-2 在数据库日常维护中,最常做的事情就是SQL语句优化,因为这个才是影响性能的最主要 ...