Gauss Fibonacci

Time Limit: 3000/1000 MS (Java/Others)     Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 27    Accepted Submission(s): 5
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.
 
SampleInput
2 1 4 100
2 0 4 100
 
SampleOutput
21
12 by yxt

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

1,1

1,0

设这个矩阵为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 <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include <set>
#include <algorithm> using namespace std;
#define LL __int64
LL mod;
struct matrix
{
LL ma[][];
}init, res1, res2, ans;
matrix Mult(matrix x, matrix y)//矩阵相乘
{
matrix tmp;
int i, j, k;
for(i=;i<;i++)
{
for(j=;j<;j++)
{
tmp.ma[i][j]=;
for(k=;k<;k++)
{
tmp.ma[i][j]=(tmp.ma[i][j]+x.ma[i][k]*y.ma[k][j])%mod;
}
}
}
return tmp;
}
matrix Pow(matrix x, int k) //矩阵快速幂关键
{
matrix tmp;
int i, j;
for(i=;i<;i++) for(j=;j<;j++) tmp.ma[i][j]=(i==j);
while(k)
{
if(k&) tmp=Mult(tmp,x);
x=Mult(x,x);
k>>=;
}
return tmp;
}
matrix Add(matrix x, matrix y)//矩阵相加
{
int i, j;
matrix tmp;
for(i=;i<;i++)
{
for(j=;j<;j++)
{
tmp.ma[i][j]=(x.ma[i][j]+y.ma[i][j])%mod;
}
}
return tmp;
}
matrix Sum(matrix x, int k)//等比矩阵求和
{
if(k==) return x;
if(k&)
return Add(Sum(x,k-),Pow(x,k));
matrix tmp;
tmp=Sum(x,k>>);
return Add(tmp,Mult(tmp,Pow(x,k>>)));
}
int main()
{
int k, b, n;
while(scanf("%d%d%d%d",&k,&b,&n,&mod)!=EOF)
{
init.ma[][]=;
init.ma[][]=;
init.ma[][]=;
init.ma[][]=;
res1=Pow(init,b);
res2=Pow(init,k);
ans=Add(res1,Mult(res1,Sum(res2,n-)));
printf("%I64d\n",ans.ma[][]);
}
return ;
}

HDU 1588 Gauss Fibonacci(矩阵快速幂)的更多相关文章

  1. HDU 1588 Gauss Fibonacci(矩阵高速幂+二分等比序列求和)

    HDU 1588 Gauss Fibonacci(矩阵高速幂+二分等比序列求和) ACM 题目地址:HDU 1588 Gauss Fibonacci 题意:  g(i)=k*i+b;i为变量.  给出 ...

  2. HDU.1575 Tr A ( 矩阵快速幂)

    HDU.1575 Tr A ( 矩阵快速幂) 点我挑战题目 题意分析 直接求矩阵A^K的结果,然后计算正对角线,即左上到右下对角线的和,结果模9973后输出即可. 由于此题矩阵直接给出的,题目比较裸. ...

  3. HDU 2842 (递推+矩阵快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2842 题目大意:棒子上套环.第i个环能拿下的条件是:第i-1个环在棒子上,前i-2个环不在棒子上.每个 ...

  4. hdu 2604 Queuing(矩阵快速幂乘法)

    Problem Description Queues and Priority Queues are data structures which are known to most computer ...

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

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

  6. poj 3070 Fibonacci 矩阵快速幂

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

  7. HDU 5950 - Recursive sequence - [矩阵快速幂加速递推][2016ACM/ICPC亚洲区沈阳站 Problem C]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5950 Farmer John likes to play mathematics games with ...

  8. 2013长春网赛1009 hdu 4767 Bell(矩阵快速幂+中国剩余定理)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4767 题意:求集合{1, 2, 3, ..., n}有多少种划分情况bell[n],最后结果bell[ ...

  9. HDU 6470 Count 【矩阵快速幂】(广东工业大学第十四届程序设计竞赛 )

    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6470 Count Time Limit: 6000/3000 MS (Java/Others)    ...

随机推荐

  1. shell 基本操作小结

    1.echo和if else fi命令 #!/bin/bash echo hello;echo there filename=demo.sh if [ -e "$filename" ...

  2. Sublime Text 3.1.1 Build 3176 注册码破解

    在hosts(C:\Windows\System32\drivers\etc)加入如下内容: 127.0.0.1       www.sublimetext.com127.0.0.1       li ...

  3. Selenium UI 举例 getCssValue

    selenium jar包中,在WebElement的接口中, String getCssValue(String var1); 可以通过标签,获取对应的css值.具体要怎么用呢,如下: WebEle ...

  4. django查询集API

    本节将详细介绍查询集的API,它建立在下面的模型基础上,与上一节的模型相同: from django.db import models class Blog(models.Model): name = ...

  5. Qt5_qt.conf

    本站所有文章由本站和原作者保留一切权力,仅在保留本版权信息.原文链接.原 文作者的情况下允许转载,转载请勿删改原文内容, 并不得用于商业用途. 谢谢合作. 原文链接:用qt.conf和qtconfig ...

  6. cocos2dx 在windows下开启console

    cocos2dx 3.10 1.在main函数中写入代码 AllocConsole(); freopen("CONIN$", "r", stdin); freo ...

  7. Linux crontab定时执行任务 命令格式与详细例子(转)

    基本格式 : * * * * * command 分 时 日 月 周 命令 第1列表示分钟1-59 每分钟用*或者 */1表示 第2列表示小时1-23(0表示0点) 第3列表示日期1-31 第4列表示 ...

  8. C# WPF 利用NPOI读写Excel文件

    https://blog.csdn.net/a312024054/article/details/70139172 [各种样式] https://www.cnblogs.com/xwgli/archi ...

  9. (Gorails)vuejs系列视频: Webpacker/vue-resource(不再为官方推荐)。

    频:https://gorails.com/episodes/using-vuejs-for-nested-forms-part-1?autoplay=1 在嵌套表格上使用vue.js. 在appli ...

  10. node搭建本地服务器

    随着前端不断发展,node基本已经成为必备,在调试的时候经常需要服务器,在之前的做法通常是去下载一个phpstudy 或者 xampp等启动一个服务,作为一个前端人虽然可以借助各种工具,但是怎么能不懂 ...