HDU 3306 Another kind of Fibonacci(矩阵+ll超时必须用int&输入必须取模&M必须是int类型)
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类型)的更多相关文章
- 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] ...
- HDU 3306 Another kind of Fibonacci ---构造矩阵***
Another kind of Fibonacci Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Jav ...
- hdu 3306 Another kind of Fibonacci(矩阵高速幂)
Another kind of Fibonacci Time Limit: 3000/10 ...
- HDU 3306 Another kind of Fibonacci(快速幂矩阵)
题目链接 构造矩阵 看的题解,剩下的就是模板了,好久没写过了,注意取余. #include <cstring> #include <cstdio> #include <s ...
- 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+……+ ...
- hdu 1097 A hard puzzle 快速幂取模
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1097 分析:简单题,快速幂取模, 由于只要求输出最后一位,所以开始就可以直接mod10. /*A ha ...
- hdu 1588(Fibonacci矩阵求和)
题目的大意就是求等差数列对应的Fibonacci数值的和,容易知道Fibonacci对应的矩阵为[1,1,1,0],因为题目中f[0]=0,f[1]=1,所以推出最后结果f[n]=(A^n-1).a, ...
- POJ3070 Fibonacci[矩阵乘法]
Fibonacci Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13677 Accepted: 9697 Descri ...
- HDU 5895 Mathematician QSC(矩阵乘法+循环节降幂+除法取模小技巧+快速幂)
传送门:HDU 5895 Mathematician QSC 这是一篇很好的题解,我想讲的他基本都讲了http://blog.csdn.net/queuelovestack/article/detai ...
随机推荐
- [No0000EA]C# 可空类型(Nullable)
C# 可空类型(Nullable) C# 提供了一个特殊的数据类型,nullable 类型(可空类型),可空类型可以表示其基础值类型正常范围内的值,再加上一个 null 值. 例如,Nullable& ...
- spring boot拦截器WebMvcConfigurerAdapter,以及高版本的替换方案
Springboot中静态资源和拦截器处理(踩了坑) 背景: 在项目中我使用了自定义的Filter 这时候过滤了很多路径,当然对静态资源我是直接放过去的,但是,还是出现了静态资源没办法访问到spr ...
- 为什么实数系里不存在最小正数?(Why the smallest positive real number doesn't exist in the real number system ?)
We define the smallest positive real number as the number which is explicitly greater than zero and ...
- post/get in console of JSarray/js 数组详细操作方法及解析合集
https://juejin.im/post/5b0903b26fb9a07a9d70c7e0[ js 数组详细操作方法及解析合集 js array and for each https://blog ...
- IO密集型 计算密集型
参考:https://www.cnblogs.com/zhangyux/p/6195860.html 参考:廖雪峰 协程 gevent IO密集型任务指的是磁盘IO或者网络IO占主要的任务,计算量很小 ...
- 内部排序->插入排序->其它插入排序->表插入排序
文字描述 和之前的插入排序比,表插入排序可以保证排序过程中不移动记录:因此表插入排序所用的存储结构和之前的顺序存储不同,表插入排序采用静态链表类型作为待排记录序列的存储结构,设数组中下标0的分量为表头 ...
- SQL row_number() over(partition by函数
1)row_number() over(partition by 列名1 order by 列名2 desc)的使用 表示根据 列名1 分组,然后在分组内部根据 列名2 排序,而此函数计算的值就表示每 ...
- 遍历出文档内所有元素的tagName
//深度优先 function deepLogTagNames(parentNode){ console.log(parentNode.tagName); const childNodes=paren ...
- POJ2431 Expedition 贪心
正解:模拟费用流 解题报告: 先放个传送门鸭,题目大意可以点Descriptions的第二个切换成中文翻译 然后为了方便表述,这里强行改一下题意(问题是一样的只是表述不一样辣,,, 就是说现在在高速公 ...
- install python in wine
wget http://www.kegel.com/wine/winetricks chmod +x winetricks ./winetricks 钩选msxml3就可以了. ___________ ...