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. tensorflow的assgin方法

    官网API是这么说的 This operation outputs a Tensor that holds the new value of 'ref' after the value has bee ...

  2. ==、===和Object.is()的区别

    ==.===和Object.is()的区别 一. 定义: ==:等同,比较运算符,两边值类型不同的时候,先进行类型转换,再比较: ===:恒等,严格比较运算符,不做类型转换,类型不同就是不等: Obj ...

  3. 【待补】splay 模板

    #define _CRT_SECURE_NO_WARNINGS #include<cmath> #include<iostream> #include<stdio.h&g ...

  4. ReactNative小笔记

    import React, { Component } from 'react'; import { AppRegistry, View } from 'react-native'; export d ...

  5. 超级有用的15个mysqlbinlog命令

    在MySQL或MariaDB中,任意时间对数据库所做的修改,都会被记录到日志文件中.例如,当你添加了一个新的表,或者更新了一条数据,这些事件都会被存储到二进制日志文件中.二进制日志文件在MySQL主从 ...

  6. typedef define typedef可以使程序参数化,提高程序的可移植性。

    小结: 1. typedef并没有创建一个新类型,它只是为某个已存在的类型增加了一个新的名称而已: 2. typedef声明也没有证据新的语义:通过这种方式声明的变量与通过普通方式声明的变量具有完全相 ...

  7. 可执行代码(Executable Code)目标代码(object code)

    小结: 1.可执行代码(Executable Code)是指将目标代码(object code)连接后形成的代码,简单来说是机器能够直接执行的代码. https://baike.baidu.com/i ...

  8. php之变量和常量

    PHP中的变量用一个美元符号后面跟变量来表示.变量名是区分大小写的. 变量与PHP中其它的标签一样遵循相同的规则.一个有效的变量名有字母或者下划线开头,后面跟上任意数量的字母,数字,或者下划线. 按照 ...

  9. [daily] 使用左右对比查看diff 格式的文件

    如题: Given your references to Vim in the question, I'm not sure if this is the answer you want :) but ...

  10. kafka实战读书笔记

    1.katka_2.12-l.0.0.tgz 上面两个文件中的 2.11 /2.12 分别表示编译 Kafka 的 Scala 语言版本,后面的 1.0 .0 是 Kafka的版本 . 2.kafka ...