快速矩阵幂,系数矩阵由多个二项分布组成。
第1列是(0,(a+b)^k)
第2列是(0,(a+b)^(k-1),0)
第3列是(0,(a+b)^(k-2),0,0)
以此类推。

 /* 3509 */
#include <iostream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") // #define DEBUG
#define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 typedef struct mat_t {
__int64 m[][]; mat_t() {
memset(m, , sizeof(m));
}
} mat_t; const int maxn = ;
__int64 A[maxn], B[maxn], F1[maxn], F2[maxn];
int mod, L; mat_t matMult(mat_t a, mat_t b) {
mat_t c; rep(k, , L) {
rep(i, , L) {
if (a.m[i][k]) {
rep(j, , L) {
if (b.m[k][j]) {
c.m[i][j] = (c.m[i][j] + a.m[i][k]*b.m[k][j]%mod)%mod;
}
}
}
}
} return c;
} mat_t matPow(mat_t a, int n) {
mat_t ret; rep(i, , L) ret.m[i][i] = ; while (n) {
if (n & )
ret = matMult(ret, a);
a = matMult(a, a);
n >>= ;
} return ret;
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif int t;
int f1, f2, a, b;
int n, k_;
__int64 ans;
mat_t e, tmp;
int i, j, k;
__int64 c;
int l, r, nc; scanf("%d", &t);
while (t--) {
scanf("%d %d %d %d %d %d %d", &f1,&f2, &a,&b, &k_, &n, &mod);
L = k_ + ;
A[] = B[] = F1[] = F2[] = ;
for (i=; i<L; ++i) {
A[i] = A[i-] * a % mod;
B[i] = B[i-] * b % mod;
F1[i] = F1[i-] * f1 % mod;
F2[i] = F2[i-] * f2 % mod;
} memset(e.m, , sizeof(e.m));
e.m[][] = e.m[][] = ;
for (j=,k=k_; j<L; ++j,--k) {
for (i=,c=,nc=k+,r=k,l=; nc; ++i,--nc,c=c*r/l,--r,++l) {
e.m[i][j] = (c % mod) * A[k+-i] % mod * B[i-] % mod;
}
}
#ifdef DEBUG
for (i=; i<L; ++i) {
for (j=; j<L; ++j)
printf("%I64d ", e.m[i][j]);
putchar('\n');
}
#endif tmp = matPow(e, n-); ans = F1[k_] * tmp.m[][] % mod;
for (j=; j<L; ++j) {
ans = (ans + F2[k_+-j]*F1[j-]%mod*tmp.m[j][]%mod)%mod;
} printf("%I64d\n", ans);
} #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}

【HDOJ】3509 Buge's Fibonacci Number Problem的更多相关文章

  1. Buge's Fibonacci Number Problem

    Buge's Fibonacci Number Problem Description snowingsea is having Buge’s discrete mathematics lesson, ...

  2. 【LEETCODE】44、509. Fibonacci Number

    package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...

  3. 【HDOJ】4403 A very hard Aoshu problem

    HASH+暴力. /* 4403 */ #include <iostream> #include <cstdio> #include <cstring> #incl ...

  4. 【HDOJ】4972 A simple dynamic programming problem

    水题. #include <cstdio> #include <cstring> #include <cstdlib> int abs(int x) { ? -x: ...

  5. 【BZOJ4922】[Lydsy六月月赛]Karp-de-Chant Number 贪心+动态规划

    [BZOJ4922][Lydsy六月月赛]Karp-de-Chant Number Description 卡常数被称为计算机算法竞赛之中最神奇的一类数字,主要特点集中于令人捉摸不透,有时候会让水平很 ...

  6. 【LeetCode】287. Find the Duplicate Number

    Difficulty:medium  More:[目录]LeetCode Java实现 Description Given an array nums containing n + 1 integer ...

  7. 【LeetCode】1150. Check If a Number Is Majority Element in a Sorted Array 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 二分查找 日期 题目地址:https://lee ...

  8. 【bzoj4922】[Lydsy六月月赛]Karp-de-Chant Number 贪心+背包dp

    题目描述 给出 $n$ 个括号序列,从中选出任意个并将它们按照任意顺序连接起来,求以这种方式得到匹配括号序列的最大长度. 输入 第一行包含一个正整数n(1<=n<=300),表示括号序列的 ...

  9. 【数论】FOJ 2238 Daxia & Wzc's problem

    题目链接: http://acm.fzu.edu.cn/problem.php?pid=2238 题目大意: 已知等差数列A(0)的首项a和公差d,求出数列A(0)前n项和,得到新数列A(1);以此类 ...

随机推荐

  1. UITableViewCell 高度计算从混沌初始到天地交泰

    [原创]UITableViewCell 高度计算从混沌初始到天地交泰 本文主要基予iOS UITableViewCell 高度自适应计算问题展开陈述,废话少说直入正题: UITableView控件可能 ...

  2. centos 7下配置mysql+php(ThinkPHP)+nginx

    最近在Linux平台上配置服务器部署网站(说多了都是泪!),记个笔记! 一.首先是在centos下安装mysql (参考博客) mysql yum库提供了一个简单的和方便的方法来安装和更新MySQL相 ...

  3. tomcat maxConnections和maxThreads区别

    maxConnections:与tomcat建立的最大socket连接数,默认10000(很多网上说200,实际上通过tomcat7.0.55源码查看可以知道是10000),AbstractEndpo ...

  4. Oracle PL/SQL 找出100以内是3和5的倍数的数 循环语句

    循环: loop --执行代码 exit when 表达式;--当表达式为真退出循环.(注意,其编写位置决定循环为先判断还是先执行,相当于java的while或do-while) end loop; ...

  5. PHPStrom 使用技巧以及基本设置教程【更新完结】

    本博文由北京乐学一百在线教育科技有限公司平台开发组成员提供,在此表示感谢,截图来源于Tower,热力推荐的一款不错的办公工具. 1.SVN链接上的条件下,修改子文件,父文件夹以及祖辈文件夹变色设置:​ ...

  6. memcached全面剖析--2

    理解memcached的内存存储 下面是<memcached全面剖析>的第二部分. 发表日:2008/7/9 作者:前坂徹(Toru Maesaka) 原文链接:http://gihyo. ...

  7. 使用curl获取网站的http的状态码

    发布:thebaby   来源:net     [大 中 小] 本文分享一例shell脚本,一个使用curl命令获取网站的httpd状态码的例子,有需要的朋友参考下.本文转自:http://www.j ...

  8. PHPCMS 核心代码与 www 分离部署

    为了满足更多用户二次开发的兴趣与爱好,同时,为了更加安全.可以通过修改入口代码的包含方式来让主程序和www程序分开. 先看下面目录结构: (图1) 我们需要将 phpcms 目录和 index.php ...

  9. 【InstallShield】 为什么卸载后有的文件没有删掉

    1.Component的属性Permanent设置为Yes. 2.Component的ID为空. 3.Component被其他应用程序或者Feature使用. 4.设置了一个Condition,在安装 ...

  10. 八,WPF 命令

    WPF命令模型 ICommand接口 WPF命令模型的核心是System.Windows.Input.ICommand接口,该接口定义了命令的工作原理,它包含了两个方法和一个事件: public in ...