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 ;
}

最新文章

  1. 【夯实PHP基础系列】linux下yum安装PHP APC
  2. 六、Android学习第五天——Handler的使用(转)
  3. squid安装配置
  4. android架构
  5. VS2013中Python学习笔记[Django Web的第一个网页]
  6. geotools
  7. hdu 3948 Portal (kusral+离线)
  8. node-sqlserver :微软发布的 SQL Server 的 Node.js 驱动
  9. zoj 3537 Cake(区间dp)
  10. android EditText内嵌图片
  11. HDU 4725 The Shortest Path in Nya Graph-【SPFA最短路】
  12. Android与JNI(三) ---- c++调用java(转载)
  13. Accord.NET_Naive Bayes Classifier
  14. 安卓4.0以上系统怎么不用root激活XPOSED框架的方法
  15. ELK简单安装
  16. VS调试快捷键配置更改
  17. CSS 定位 (Positioning) 实例
  18. 读取磁盘:CHS方式
  19. 洛谷 P4389 付公主的背包 解题报告
  20. Spark部署

热门文章

  1. 加L“”
  2. 使用tensorflow深度学习识别验证码
  3. MySQL------Navicat安装与激活
  4. 在linux本地下载ftp中的文件
  5. cocos2d-x游戏引擎核心之八——多线程
  6. @ResponseBody将集合数据转换为json格式并返回给客户端
  7. c++11——std::function和bind绑定器
  8. LeetCode——N-Queens
  9. SenchaTouch调用纯数字键盘
  10. 删除lv