HDU1588 Gauss Fibonacci (矩阵快速幂+等比数列二分求和)
2016-07-21 09:47
363 查看
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,MEach 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
1221
对于fibonacci列的求法,是利用矩阵A的n次方;
但这道题要求的是i从0到n-1时f(ki+b)的和,有如下的演算
对于等比数列和的求法,有二分求等比数列
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#define mem(a,x) memset(a,x,sizeof(a))
#define
4000
Rep(i,x,n) for(int i=x;i<=n;i++)
using namespace std;
const int NUM = 2;
int maxn,mod;
typedef long long ll;
struct Matrix
{
ll a[NUM][NUM];
void init()
{
mem(a,0);
for(int i=0;i<maxn;i++){
a[i][i] = 1;
}
}
};
Matrix add(Matrix a,Matrix b)
{
Matrix ans;
mem(ans.a,0);
for(int i=0;i<maxn;i++){
for(int j=0;j<maxn;j++){
ans.a[i][j] = a.a[i][j]+b.a[i][j];
ans.a[i][j] %= mod;
}
}
return ans;
}
Matrix mul(Matrix a,Matrix b)
{
Matrix ans;
for(int i=0;i<maxn;i++){
for(int j=0;j<maxn;j++){
ans.a[i][j] = 0;
for(int k=0;k<maxn;k++){
ans.a[i][j] += a.a[i][k]*b.a[k][j];
ans.a[i][j] %= mod;
}
}
}
return ans;
}
Matrix pow(Matrix a,ll n)
{
Matrix ans;
ans.init();
while(n){
if(n&1)
ans = mul(ans,a);
n >>= 1;
a = mul(a,a);
}
return ans;
}
Matrix sum(Matrix a,ll k)
{
if(k==1) return a;
Matrix t = sum(a,k/2);
if(k&1){
Matrix cur = pow(a,k/2+1);
t = add(t,mul(t,cur));
t = add(t,cur);
}
else{
Matrix cur = pow(a,k/2);
t = add(t,mul(t,cur));
}
return t;
}
int main()
{
int m;
ll k,b,n;
while(scanf("%lld %lld %lld %d",&k,&b,&n,&m)!=EOF){
if(k==0&&b==0){
printf("0\n");
continue;
}
mod = m; maxn = 2;
Matrix P ={1,1,1,0},A,B,S1,S2,S3;
A = pow(P,k);
B={0,0,0,0};
if(b>0){
B = pow(P,b-1);
S1 = sum(A,n-1);
S2 = mul(B,S1);
S3 = add(B,S2);
}
else{
B = pow(P,k-1);
S1 = sum(A,n-2);
S2 = mul(B,S1);
S3 = add(B,S2);
}
printf("%lld\n",S3.a[0][0]);
}
return 0;
}
相关文章推荐
- 列式数据库和行式数据库
- 百练 4116 拯救行动
- 杭电 Problem - 1997 汉诺塔VII 【递归&规律】
- thinkPHP多域名情况下使用memcache方式共享session数据的实现方法
- reuqest接收参数的几种方式
- Windows下常见的SQLite管理工具
- 构建高性能web之路------mysql读写分离实战
- 大数据与数据中心
- Myeclipse学习总结(7)——Eclipse插件之Maven配置及问题解析
- Myeclipse学习总结(7)——Eclipse插件之Maven配置及问题解析
- Hive通过查询语句向表中插入数据过程中发现的坑
- AsyncTask类
- cf #363 a
- (转载)hibernate缓存
- 设计模式之策略
- Mac OS X中配置Apache
- Fragment从Activity里获取数据(Activity向Fragment传递数据)
- System.exit(0) 退出软件后重启问题
- 设置UIImage的渲染模式:UIImage.renderingMode
- javascript