http://acm.hdu.edu.cn/showproblem.php?pid=1588

Problem Description
Without expecting, Angel replied quickly.She says: "I'v heard that you'r a very clever boy. So if you wanna me be your GF, you should solve the problem called GF~. " How good an opportunity that Gardon can not give up! The "Problem GF" told by Angel is actually "Gauss Fibonacci". As we know ,Gauss is the famous mathematician who worked out the sum from 1 to 100 very quickly, and Fibonacci is the crazy man who invented some numbers.
Arithmetic progression: g(i)=k*i+b; We assume k and b are both non-nagetive integers.
Fibonacci Numbers: f(0)=0 f(1)=1 f(n)=f(n-1)+f(n-2) (n>=2)
The Gauss Fibonacci problem is described as follows: Given k,b,n ,calculate the sum of every f(g(i)) for 0<=i<n The answer may be very large, so you should divide this answer by M and just output the remainder instead.
 
Input
The input contains serveral lines. For each line there are four non-nagetive integers: k,b,n,M Each of them will not exceed 1,000,000,000.
 
Output
For each line input, out the value described above.
 
Sample Input
2 1 4 100
2 0 4 100
 
Sample Output
21
12
 

题目解析:

用于构造斐波那契的矩阵为

0,1

1,1

设这个矩阵为A。

sum=f(b)+f(k+b)+f(2*k+b)+f(3*k+b)+........+f((n-1)*k+b)

<=>sum=A^b+A^(k+b)+A^(2*k+b)+A^(3*k+b)+........+A^((n-1)*k+b)

<=>sum=A^b+A^b*(A^k+A^2*k+A^3*k+.......+A^((n-1)*k))(1)

设矩阵B为A^k;

那么(1)式为

sum=A^b+A^b*(B+B^2+B^3+......+B^(n-1));

显然,这时候就可以用二分矩阵做了,括号内的就跟POJ 3233的形式一样了。

代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
#define inf 0x3f3f3f3f
#define LL __int64//int就WA了
using namespace std;
struct ma
{
LL a[][];
} init,res,B,C;
int mod,k,b,n,K;
void Init()
{
init.a[][]=;
init.a[][]=;
init.a[][]=;
init.a[][]=;
}
ma Mult(ma x,ma y)
{
ma tmp;
for(int i=; i<; i++)
{
for(int j=; j<; j++)
{
tmp.a[i][j]=;
for(int z=; z<; z++)
{
tmp.a[i][j]=(tmp.a[i][j]+x.a[i][z]*y.a[z][j])%mod;
}
}
}
return tmp;
}
ma Pow(ma x,int K)
{
ma tmp;
for(int i=; i<; i++)
{
for(int j=; j<; j++)
tmp.a[i][j]=(i==j);
}
while(K!=)
{
if(K&)
tmp=Mult(tmp,x);
K>>=;
x=Mult(x,x);
}
return tmp;
}
ma Add(ma x,ma y)
{
ma tmp;
for(int i=; i<; i++)
{
for(int j=; j<; j++)
{
tmp.a[i][j]=(x.a[i][j]+y.a[i][j])%mod;
}
}
return tmp;
}
ma Sum(ma x,int K)
{
ma tmp,y;
if(K==)
return x;
tmp=Sum(x,K/);
if(K&)
{
y=Pow(x,K/+);
tmp=Add(Mult(y,tmp),tmp);
tmp=Add(tmp,y);
}
else
{
y=Pow(x,K/);
tmp=Add(Mult(y,tmp),tmp);
}
return tmp;
}
/*另外一种写法
matrix Sum(matrix x, int k) 

    if(k==1) return x; 
    if(k&1) 
        return Add(Sum(x,k-1),Pow(x,k));  //如果k是奇数,求x^k+sum(x,k-1)
    matrix tmp; 
    tmp=Sum(x,k>>1); 
    return Add(tmp,Mult(tmp,Pow(x,k>>1))); 
}
*/
int main()
{
while(scanf("%d%d%d%d",&k,&b,&n,&mod)!=EOF)
{
Init();
B=Pow(init,k);
C=Pow(init,b);
res=Sum(B,n-);
res=Mult(C,res);
res=Add(C,res);
printf("%I64d\n",res.a[][]);
}
return ;
}

HDU:Gauss Fibonacci(矩阵快速幂+二分)的更多相关文章

  1. HDU 1588 Gauss Fibonacci(矩阵快速幂)

    Gauss Fibonacci Time Limit: 3000/1000 MS (Java/Others)     Memory Limit: 32768/32768 K (Java/Others) ...

  2. HDU.2640 Queuing (矩阵快速幂)

    HDU.2640 Queuing (矩阵快速幂) 题意分析 不妨令f为1,m为0,那么题目的意思为,求长度为n的01序列,求其中不含111或者101这样串的个数对M取模的值. 用F(n)表示串长为n的 ...

  3. HDU 5667 构造矩阵快速幂

    HDU 5667 构造矩阵快速幂 题目描述 解析 我们根据递推公式 设 则可得到Q的指数关系式 求Q构造矩阵 同时有公式 其中φ为欧拉函数,且当p为质数时有 代码 #include <cstdi ...

  4. HDU 4549 (费马小定理+矩阵快速幂+二分快速幂)

    M斐波那契数列 Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Submit Statu ...

  5. POJ 3233 Matrix Power Series 矩阵快速幂+二分求和

    矩阵快速幂,请参照模板 http://www.cnblogs.com/pach/p/5978475.html 直接sum=A+A2+A3...+Ak这样累加肯定会超时,但是 sum=A+A2+...+ ...

  6. POJ 3233 Matrix Power Series (矩阵快速幂+二分求解)

    题意:求S=(A+A^2+A^3+...+A^k)%m的和 方法一:二分求解S=A+A^2+...+A^k若k为奇数:S=(A+A^2+...+A^(k/2))+A^(k/2)*(A+A^2+...+ ...

  7. poj 3070 Fibonacci (矩阵快速幂乘/模板)

    题意:给你一个n,输出Fibonacci (n)%10000的结果 思路:裸矩阵快速幂乘,直接套模板 代码: #include <cstdio> #include <cstring& ...

  8. HDU 6185 Covering 矩阵快速幂

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6185 题意:用 1 * 2 的小长方形完全覆盖 4 * n的矩形有多少方案. 解法:小范围是一个经典题 ...

  9. 2017 ECJTU ACM程序设计竞赛 矩阵快速幂+二分

    矩阵 Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other) Total Submission ...

  10. poj 3070 Fibonacci 矩阵快速幂

    Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. F ...

随机推荐

  1. 编写高性能的jQuery代码

    jQuery Optimization 现在jQuery已经出现在很多项目中,然而许多同学忽略了他的性能问题以及代码质量问题, 下面是我对jQuery的一些性能方面的学习. 选择器 选择器是jQuer ...

  2. Makefile--伪目标 (三)

    原创博文,转载请标明出处--周学伟http://www.cnblogs.com/zxouxuewei/ 一般情况下,Makefile都会有一个clean目标,用于清除编译过程中产生的二进制文件.我们在 ...

  3. 工作流JBPM_day01:4-管理流程定义

    工作流JBPM_day01:4-管理流程定义 管理流程(流程定义) 部署(添加) 查询 删除 查看流程图(xxx.png) -- 修改 --> 没有真正的修改,而是使用“再次部署+使用最新版本启 ...

  4. swift - UIImageView 的使用

    1.创建 var imageView = UIImageView()//初始化 2.图片的显示及图片的改变 imageView = UIImageView(image: UIImage(named: ...

  5. python2.0 s12 day4

    python2.0 s12 day404 python s12 day4 TengLan回顾上节内容 05 python s12 day4 迭代器原理及使用 本节大纲介绍: 1.迭代器&生成器 ...

  6. docker学习-docker容器

  7. jquery ajax返回html乱码解决

    情景: index.html 使用如下代码访问另外一个html片段 1 2 3 4 5 6 7 8 9 10 11 12 13 function rewritedata(id){     var uu ...

  8. x86 体系指令

    FASM 第二章 - 2.1 x86 体系指令 Author: 徐艺波  From: xuyibo.org  Updated: 2008-04-17   官方论坛   本站软件反馈.软件开发交流.   ...

  9. MUI 图标筛选切换(父页面传值子页面)

      1 父页面: index.html <li class="tab_layout"> <a href="javascript:;" clas ...

  10. struts2漏洞原理

    一.struts2简介: 目前web框架中非常流行的都是mvc设计模式.经典例子例如:python的Django.Flask:java的ssm等.因为使用MVC设计模式,所以在框架内部处理用户数据流参 ...