【HDOJ】3509 Buge's Fibonacci Number Problem
快速矩阵幂,系数矩阵由多个二项分布组成。
第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的更多相关文章
- Buge's Fibonacci Number Problem
Buge's Fibonacci Number Problem Description snowingsea is having Buge’s discrete mathematics lesson, ...
- 【LEETCODE】44、509. Fibonacci Number
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
- 【HDOJ】4403 A very hard Aoshu problem
HASH+暴力. /* 4403 */ #include <iostream> #include <cstdio> #include <cstring> #incl ...
- 【HDOJ】4972 A simple dynamic programming problem
水题. #include <cstdio> #include <cstring> #include <cstdlib> int abs(int x) { ? -x: ...
- 【BZOJ4922】[Lydsy六月月赛]Karp-de-Chant Number 贪心+动态规划
[BZOJ4922][Lydsy六月月赛]Karp-de-Chant Number Description 卡常数被称为计算机算法竞赛之中最神奇的一类数字,主要特点集中于令人捉摸不透,有时候会让水平很 ...
- 【LeetCode】287. Find the Duplicate Number
Difficulty:medium More:[目录]LeetCode Java实现 Description Given an array nums containing n + 1 integer ...
- 【LeetCode】1150. Check If a Number Is Majority Element in a Sorted Array 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 二分查找 日期 题目地址:https://lee ...
- 【bzoj4922】[Lydsy六月月赛]Karp-de-Chant Number 贪心+背包dp
题目描述 给出 $n$ 个括号序列,从中选出任意个并将它们按照任意顺序连接起来,求以这种方式得到匹配括号序列的最大长度. 输入 第一行包含一个正整数n(1<=n<=300),表示括号序列的 ...
- 【数论】FOJ 2238 Daxia & Wzc's problem
题目链接: http://acm.fzu.edu.cn/problem.php?pid=2238 题目大意: 已知等差数列A(0)的首项a和公差d,求出数列A(0)前n项和,得到新数列A(1);以此类 ...
随机推荐
- CentOS配置java运行环境
CentOS_配置_docker CentOS_6.5 1.CentOS_6.5在安装docker-io之前需要首先卸载docker包(没下载过可以省略) $ sudo yum -y remove d ...
- nodejs-fs使用
(1)读取文本文件时须添加上'encoding'才能输出可读的内容. 02.txt hello,world! nodejs_readfile.js var fs = require('fs'); fs ...
- Asp.net自带导出方法
///datatable数据源 filename绝对路径 如:E:\\***.xls DataTable.WriteXml(fileName)
- JSP Ajax
html代码: <!DOCTYPE html> <html> <script> function display() { var div=document.getE ...
- call_user_func_array
call_user_func_array — 调用回调函数,并把一个数组参数作为回调函数的参数 参数 callback 被调用的回调函数. param_arr 要被传入回调函数的数组,这个数组得是索引 ...
- Universal-Image-Loader(UIL)使用方法&流程图&源码分析 ----- 未完
GitHub源码: Android-Universal-Image-Loader Features Multithread image loading (async or sync) 多线程加载(同步 ...
- MVC+EF 随笔小计——NuGet程序包管理
安装EF 打开 工具-库程序包管理器-程序包管理器控制台 输入 install-package entityframework 去MSDN上查看下EF的架构图:http://msdn.microsof ...
- 2016032901 - ubuntu安装jdk
在ubuntu上安装jdk,然后网上大部分相同的教程配置,结果运行java,javac,java -version总是出现莫名奇妙的问题. 原先配置完之后,运行java -version后出现下面内容 ...
- 关于ASE日志空间示数不正常的解决办法
最近某系统的ASE数据库出现了异常,经过各种努力,终于把数据库正常又起起来了.但是经过检查,发现在查看剩余日志空间的时候(sp_helpsegment 'logsegment'),发现显示出来 ...
- C语言小知识
1.数组初始化 a[5] = {1}; //1,0,0,0,0 a[5] = {0}; //0,0,0,0,0 a[3][3] = {1}; //1,0,0;0,0,0;0,0,0 a[3][ ...