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. [深入理解Android卷一全文-第四章]深入理解zygote

    由于<深入理解Android 卷一>和<深入理解Android卷二>不再出版,而知识的传播不应该由于纸质媒介的问题而中断,所以我将在CSDN博客中全文转发这两本书的所有内容. ...

  2. 超全面的JavaWeb笔记day14<用户注册登录>

    案例:用户注册登录 要求:3层框架,使用验证码 1 功能分析 l 注册 l 登录 1.1 JSP页面 l regist.jsp Ø 注册表单:用户输入注册信息: Ø 回显错误信息:当注册失败时,显示错 ...

  3. CCNet持续集成编译中SVN问题解决

    SVN问题 BUILD EXCEPTION Error Message: ThoughtWorks.CruiseControl.Core.CruiseControlException: Source ...

  4. Go基础---->go的基础学习(四)

    这里简单的介绍一下go中的关于多线程的知识. Go中的多线程 一.go中简单的并发例子 package main import ( "fmt" "time" ) ...

  5. LeetCode——Integer to Roman

    Description: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the r ...

  6. LeetCode——Submission Details

    Description: Given a string of numbers and operators, return all possible results from computing all ...

  7. str += "a" + "b" & str = str + "a" + "b"的性能比较

    str += "a" + "b"在浏览器中的执行过程: 1.创建临时字符串, 2.将临时字符串设置为“ab”, 3.将临时字符串和str进行连接, 4.将结果赋 ...

  8. VScode之JavaScript Snippet Pack

    一个片段包 使用例如: cl 回车或者tab键,就可以完整的打出console.log("") 还有很多快捷功能: 参考: https://marketplace.visualst ...

  9. JZOJ.5329【NOIP2017模拟8.22】时间机器

    Description

  10. Linux Netfilter注册钩子点

    注册钩子点首先要包含响应的头文件,因为这应该已经属于对kernel的编程了. #include <linux/module.h> #include <linux/kernel.h&g ...