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 ...
随机推荐
- 6.python在windows下用批处理文件在运行中输入程序名直接运行的方法
最近由于平时自由时间比较多,在看一本python入门书籍,在里面学习了一种用windows下的批处理文件在电脑运行界面中直接输入程序名称就可运行的方法,现将其详细说明如下: 1.首先编写一个教程上的程 ...
- 基于Antd框架的通信与交互
基于Antd框架的通信与交互 1.与用户交互 对于input输入框,在于用户交互的过程中,用户在输入任何东西时,都会引起该组件的onChange事件(如果写有这个方法的话). <FormItem ...
- EXE和DLL调用关系,DLL制作,钩子
制作DLL时,在cpp种引入了头文件,但头文件里的全局变量在cpp种却不能用 参考大佬博客https://blog.csdn.net/speargod/article/details/88854344 ...
- Redis内存碎片清理
当Redis中清理了大量的Key之后原先Redis申请的内存(used_memory_rss)将继续持有而不会释放,此时查看内存信息将会看到存在大量的内存碎片.那么,Redis的内存碎片可以清理么,该 ...
- creator 2.0版本对于preloadScene函数获取加载进度
有时候,当我们场景上挂载的资源过多时,我们使用cc.director.loadScene切换场景时会等一段时间才会切换过去,这对游戏的体验是相当不好的.所以我们可以使用cc.director.prel ...
- 快到极致的Android模拟器——Genymotion
转载声明:Ryan的博客文章欢迎您的转载,但在转载的同时,请注明文章的来源出处,不胜感激! :-)http://my.oschina.net/ryanhoo/blog/141824 还在用Androi ...
- .NET 在云原生时代的蜕变,让我在云时代脱颖而出
.NET 生态系统是一个不断变化的生态圈,我相信它正在朝着一个伟大的方向发展.有了开源和跨平台这两个关键优先事项,我们就可以放心了.云原生对应用运行时的不同需求,说明一个.NET Core 在云原生时 ...
- 【DPDK】【ring】从DPDK的ring来看无锁队列的实现
[前言] 队列是众多数据结构中最常见的一种之一.曾经有人和我说过这么一句话,叫做“程序等于数据结构+算法”.因此在设计模块.写代码时,队列常常作为一个很常见的结构出现在模块设计中.DPDK不仅是一个加 ...
- 月薪30k的Java架构师JVM常见面试题解析
在做程序员的路上经常会遇到的JVM一些经典面试题,今天给大家分享出我自己的解题思路,希望对大家有帮助,后续有空会持续更新. 1.什么情况下会发生栈内存溢出. 思路: 描述栈定义,再描述为什么会溢出,再 ...
- Nginx模块讲解
Nginx模块分为:nginx官方模块.第三方模块 通过nginx -V查看编译参数,可以看到官方编译的模块 --with-compat --with-file-aio --with-threads ...