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. mongodb启动时报错ERROR: child process failed, exited with error number 1

    不多说,直接上干货! 前期博客 Ubuntu14.04下Mongodb安装部署步骤(图文详解) Ubuntu16.04下Mongodb安装部署步骤(图文详解) root@zhouls-virtual- ...

  2. 第四章 Spring.Net 如何管理您的类___自定义对象行为

    Spring.Net 一直讲求 ” 配置应用程序“,有一种需求在实际开发中不太常用,却非常有用 -> 配置对象的行为,Spring.Net 也能够完美的实现.Spring.Net 通过几个专门的 ...

  3. 使用select多选标签笔记

    之前一直用checkbox做多选,其实 select也可以多选,只要多给一个属性即可.标签属性 http://www.w3school.com.cn/tags/att_select_multiple. ...

  4. ZooKeeper(八)-- Curator实现分布式锁

    1.pom.xml <dependencies> <dependency> <groupId>junit</groupId> <artifactI ...

  5. C++新式转型

    本文对四种标准C++的类型转换符:static_cast.dynamic_cast.reinterpret_cast.和const_cast进行了介绍,通过本文应当能够理解这四个类型转换操作符的含义. ...

  6. piblog企划

    今天开始准备根据廖雪峰的博客中的教程来完成一个python项目. 心想是以后用来作自己的博客的,那就取一个(自己感觉)好听的名字吧,然后就取名叫“piblog”,中文名就是“派博客”. 心想首先是给自 ...

  7. 拼图的几个网上找到的Demo

    东西就直接放到云盘里了 https://yunpan.cn/ck8eCzJe9Pknm  访问密码 ee53

  8. activemq 实战 一

    This chapter covers  Introduction to the use case for each of the book examples  Use of Maven for ...

  9. JavaWeb温习之HttpServletResponse对象

    以下内容均根据"方立勋JavaWeb视频教程"进行总结 1. HttpServletResponse常见应用——设置响应头控制浏览器的行为 1.1 设置http响应头控制浏览器禁止 ...

  10. Sun公司的产品AnswerBook存在多种漏洞

    一.未授权管理脚本访问漏洞: 1.受影响版本: Sun-AnswerBook2     1.2-1.4.2 2.攻击测试手段 http://a.b.c.d:8888/ab2/@AdminViewErr ...