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. VC++ 使用MSSOAP访问WebService天气服务(客户端开发)

    绪论 本文介绍使用VC++编程实现访问天气Web服务的简单实例(例子来源于网络). Web天气服务 http://www.webxml.com.cn/WebServices/WeatherWebSer ...

  2. shell脚本中,将所有的参数值否赋给一个变量或者说将所有的参数合成一个字符串,获取所有参数

    需求描述: 在写脚本的过程中,遇到这样的一个需求,将脚本执行过程中,传递给 脚本的所有的参数,都赋值给一个变量然后在对这个变量进行处理. 测试过程: 通过以下的脚本将所有传递给脚本的变量都赋值一个变量 ...

  3. 超全面的JavaWeb笔记day21<过滤器>

    1.过滤器的原理 2.实现过滤器 写一个类实现javax.servlet.Filter接口 在web.xml中对Filter进行配置 3.Filter接口 void init(FilterConfig ...

  4. 进程 vs. 线程(python的协程)(转廖雪峰老师python教程)

    我们介绍了多进程和多线程,这是实现多任务最常用的两种方式.现在,我们来讨论一下这两种方式的优缺点. 首先,要实现多任务,通常我们会设计Master-Worker模式,Master负责分配任务,Work ...

  5. raw_input()

    raw_input() 用于接收标准输入,并把标准输入当成字符串类型来处理,只能在 Python2 中使用,Python3 中没有这个函数 #!/usr/bin/env python #-*- cod ...

  6. Binary XML file line #17<vector> tag requires viewportWidth > 0

    Android高版本对比低版本 在我的项目中更改成 //buildToolsVersion '21.1.2'buildToolsVersion '24.0.1' // 24.0.1 必须用这个否则报B ...

  7. Ubuntu13.10:[3]如何开启SSH SERVER服务

    作为最新版本的UBUNTU系统而言,开源,升级全部都不在话下.传说XP已经停止补丁更新了,使用UBUNTU也是一个很好的选择.ubuntu默认安装完成后只有ssh-agent(客户端模式),宾哥百度经 ...

  8. 《C++ Primer Plus》15.5 类型转换运算符 学习笔记

    C++相对C更严格地限制允许的类型转换,并添加4个类型转换运算符,是转换过程更规范:* dynamic_cast:* const_cast:* static_cast:* reinterpret_ca ...

  9. mysql concat

    CONCAT_WS() 代表 CONCAT With Separator ,是CONCAT()的特殊形式. 第一个参数是其它参数的分隔符.分隔符的位置放在要连接的两个字符串之间. 分隔符可以是一个字符 ...

  10. android基础组件---->Button的使用

    按钮由文本或图标(或文本和一个图标)组成,当用户触摸到它时,会发生一些动作.今天我们开始Button的学习.少年的爱情永远不够用,一杯酒足以了却一件心事. Button的简要说明 根据你是否想要一个带 ...