HDU6395 Sequence(矩阵快速幂+数论分块)
题意:
F(1)=A,F(2)=B,F(n)=C*F(n-2)+D*F(n-1)+P/n
给定ABCDPn,求F(n) mod 1e9+7
思路:
P/n在一段n里是不变的,可以数论分块,再在每一段里用矩阵快速幂
debug了一下午。。
坑点:
1.数论分块的写法要注意,已更新
2.矩阵乘法在赋值回去的时候记得模一下
3.矩阵相乘不可逆,注意看一下
代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<vector>
#include<map>
#include<functional> #define fst first
#define sc second
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define lc root<<1
#define rc root<<1|1
#define lowbit(x) ((x)&(-x)) using namespace std; typedef double db;
typedef long double ldb;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PI;
typedef pair<ll,ll> PLL; const db eps = 1e-;
const int mod = 1e9+;
const int maxn = 2e6+;
const int maxm = 2e6+;
const int inf = 0x3f3f3f3f;
const db pi = acos(-1.0);
void prt(ll b[][]){
for(int i = ; i <= ; i++){
for(int j = ; j <= ; j++){
printf("%3I64d ", b[i][j]);
}
printf("\n");
}
return;
}
void mtpl(ll a[][], ll b[][], ll s[][]){
ll tmp[][];
mem(tmp, );
for(int i = ; i <= ; i++){
for(int j = ; j <= ; j++){ for(int k = ; k <= ; k++){
tmp[i][j] += (a[i][k]*b[k][j])%mod;
}
}
}
for(int i = ; i <= ; i++){
for(int j = ; j <= ; j++){
s[i][j] = tmp[i][j]%mod;
}
}
return;
}ll a, b, c, d, p, n;
ll B[][];
void fp(ll n, ll tmp){
ll A[][];
mem(A, );
A[][] = d;
A[][] = c;
A[][] = ;
A[][] = tmp;
A[][] = ;
//prt(A);
while(n){
if(n&) mtpl(A,B,B);
mtpl(A,A,A);
n>>=;
}
return;
}
int main() {
int T;
scanf("%d",&T);
while(T--){ scanf("%I64d %I64d %I64d %I64d %I64d %I64d", &a, &b, &c, &d, &p, &n); if(n==){
printf("%I64d\n", a);
continue;
}
if(n==){
printf("%I64d\n", b);
continue;
} mem(B, );
for(int i = ; i <= ; i++)B[i][i]=;
B[][] = b;
B[][] = a;
B[][] = ;
ll l , r;
for(l = , r = ; l <= n; l = r + ){
if(p/l) r = min(p, p/(p/l));
else r = n;
ll tmp = p/l;
fp(r-l+,tmp);
} printf("%I64d\n", B[][]);
}
return ;
}
/*
4
1 2 1 2 5 3
1 2 1 2 3 5
1 2 3 4 7 4
2 3 3 3 3 4 */
HDU6395 Sequence(矩阵快速幂+数论分块)的更多相关文章
- hdu6395 (矩阵快速幂+分块)
Online Judge Online Exercise Online Teaching Online Contests Exercise Author F.A.Q Hand In Hand Onli ...
- HDU5950 Recursive sequence (矩阵快速幂加速递推) (2016ACM/ICPC亚洲赛区沈阳站 Problem C)
题目链接:传送门 题目: Recursive sequence Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total ...
- HDU5950 Recursive sequence —— 矩阵快速幂
题目链接:https://vjudge.net/problem/HDU-5950 Recursive sequence Time Limit: 2000/1000 MS (Java/Others) ...
- hdu-5667 Sequence(矩阵快速幂+费马小定理+快速幂)
题目链接: Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- UVA - 10689 Yet another Number Sequence 矩阵快速幂
Yet another Number Sequence Let’s define another number sequence, given by the foll ...
- HDU6395-Sequence 矩阵快速幂+除法分块 矩阵快速幂模板
目录 Catalog Solution: (有任何问题欢迎留言或私聊 && 欢迎交流讨论哦 Catalog Problem:Portal传送门 原题目描述在最下面. Solution ...
- Yet Another Number Sequence——[矩阵快速幂]
Description Everyone knows what the Fibonacci sequence is. This sequence can be defined by the recur ...
- HDU 1005 Number Sequence(矩阵快速幂,快速幂模板)
Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1 ...
- HDU - 1005 Number Sequence 矩阵快速幂
HDU - 1005 Number Sequence Problem Description A number sequence is defined as follows:f(1) = 1, f(2 ...
随机推荐
- 2020 年 Java 程序员应该学习什么?
大家好,我相信大家在新的一年都有一个良好的开端,并准备好制定一个提升自我技术的目标.作为 Java 开发人员,我还制定了一些目标,希望在今年成为一名更好的 Java 开发人员. 如果你尚未制定目标,这 ...
- 搭建nginx
Nginx ("engine x") 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器.Nginx是由Igor Sysoev为俄罗斯访问量第二的R ...
- 《【面试突击】— Redis篇》--Redis都有哪些数据类型?分别在哪些场景下使用比较合适?
能坚持别人不能坚持的,才能拥有别人不能拥有的.关注编程大道公众号,让我们一同坚持心中所想,一起成长!! <[面试突击]— Redis篇>--Redis都有哪些数据类型?分别在哪些场景下使用 ...
- 如何应用threejs实现立方体每个面用图片替换
var geometry = new THREE.BoxGeometry(200, 200, 200);var materialsbg = []; for (var i = 0; i < geo ...
- ubuntu16.04 docker kubernetes(k8s) istio 安装
版本: docker: 19.03.5 kubernetes: 1.17.0 istio: 1.4.3 步骤一:给ubuntu换源 https://www.cnblogs.com/lfri/p/106 ...
- max_element( )
直接用这个函数 , 会比自己写个for 判断快的多了 . position=max_element(a,a+n)-a; position 代表找到最大元素的位置 , max_element( ) 的 ...
- C#中的委托是什么
1.什么是委托?(方法作另一个方法的参数)delegate void MyDel(int value); //声明委托类型和类一样,委托是用户自定义的类型,但是类是数据和方法的集合,而委托是持有 ...
- python简易计算器
import re """ 过程:(最内部的括号->先乘除,替换->整理表达式->加减)->替换 """ def m ...
- restframework 分页组件、响应器
一.分页组件 1.PageNumberPagination a.全局配置 导入模块 from rest_framework.pagination import PageNumberPagination ...
- 图解 Kubernetes
容器 在了解 Kubernetes 之前,让我们先了解一个容器. 因为如果不了解容器就没法聊容器编排. 容器就是...一个你塞入所有材料的容器. "材料"是指你的应用代码.依赖库, ...