快速矩阵幂,系数矩阵由多个二项分布组成。
第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. iOS开发——企业证书(免审核 299$)

    (最近准备考试……空闲截图整理成博客)

  2. js事件冒泡和事件委托

    js事件冒泡 js所谓的事件冒泡就是子级元素的某个事件被触发,它的上级元素的该事件也被递归执行 html: <ul class="clearfix" data-type=&q ...

  3. 修改虚机IP

    同网段的话,直接修改,不同网段的话,使用以下方法: 步骤一:nova list --all-tenant 找到相应虚拟机+--------------------------------------+ ...

  4. 2016ACM竞赛训练暑期课期末考试 a题

    描述 给出n个正整数,任取两个数分别作为分子和分母组成最简真分数,编程求共有几个这样的组合. 输入 第一行是一个正整数n(n<=600).第二行是n个不同的整数,相邻两个整数之间用单个空格隔开. ...

  5. PHPstrom 增加emmet插件

    之前记得使用Eclipse的时候有一个插件叫 emmet 可以实现快速开发前端,简直就是前端开发秒杀神器: 输入对应的代码一个table键就搞定了一堆代码: 在emmet的官网上 看到其实是支持的PH ...

  6. Java知识总结--数据库

    1 薪水排序后薪水排名在第3-5的员工 1)select * from(select ename,sal,rownum rn from (select ename,sal from emp_44 wh ...

  7. Win7 钩子 超时 失效

    这段时间在程序中,使用了全局钩子,但是在测试时发现,会偶尔失效. 在网上搜索到了这两篇文章: VC底层钩子程序在Win7/Vista下无效 Hooking problem in Windows 7 这 ...

  8. 用PHP实现单向链表

    供参考,代码还可继续打磨 同时放在了我的github上:https://github.com/hheedat/demo/blob/master/learn_php/58_linked_list.php ...

  9. 【4】创建一个自己的Bootstrap模板

    什么也不说了,直接贴上代码吧,哈哈 <!DOCTYPE html> <html lang="zh-cn"> <head> <meta ch ...

  10. PHP提取身份证号码中的生日并验证是否成年的函数

    php 提取身份证号码中的生日日期以及确定是否成年的一个函数.可以同时确定15位和18位的身份证,经本人亲测,非常好用,分享函数代码如下: <?php //用php从身份证中提取生日,包括15位 ...