题目:http://acm.hdu.edu.cn/showproblem.php?pid=6395

给你一个式子,给出你A,B,C,D,P,n,让你求出第n项的式子Fn。(其中ABCDPn均在1e9的范围内)

分析: 如果Fn=C*F(n-2) + D*F(n-1) + num ; 我们就可以直接构造出这个斐波那契的矩阵快速幂 :写出相似的矩阵

1.f(n)=a*f(n-1)+b*f(n-2)+c;(a,b,c是常数)

但是这里的P/n 是变化的 , 我们无法转化出来 , 但是这里 P/n 是向下取整 也 就是说会有一段一段的区间里面的 p/i 是相等的 ; 所以我们现在找到这些一段一段然后用矩阵快速幂

#include <bits/stdc++.h>
#define maxn 100005
using namespace std;
const int mod=1e9+;
typedef long long ll;
struct Marix{
ll mo[][];
Marix(){
memset(mo,,sizeof(mo));
}
};
Marix mul(Marix a,Marix b){
Marix c;
for(int i=;i<;i++){
for(int j=;j<;j++){
for(int k=;k<;k++){
c.mo[i][j]=(c.mo[i][j]+a.mo[i][k]*b.mo[k][j])%mod;
}
}
}
return c;
}
Marix powmod(Marix a,ll n){//矩阵快速幂模板
Marix tmp;
for(int i=;i<;i++){
tmp.mo[i][i]=;
}
while(n){
if(n&) tmp=mul(tmp,a);
n>>=;
a=mul(a,a);
}
return tmp;
}
int main()
{
int t;
scanf("%d",&t);
while(t--){
ll a,b,c,d,p,n;
scanf("%lld%lld%lld%lld%lld%lld",&a,&b,&c,&d,&p,&n);
if(n==){
printf("%lld\n",a);
continue;
}
if(n==){
printf("%lld\n",b);
continue;
}
Marix m;
m.mo[][]=d,m.mo[][]=c,m.mo[][]=,m.mo[][]=;
bool vis=;
for(ll i=;i<=n;){
if(p/i==){//倘若当前项大于p了,则直接用矩阵快速幂求解剩下的项
Marix tmp;
tmp=m;
tmp=powmod(tmp,n-i+);
ll res=(tmp.mo[][]*b+tmp.mo[][]*a+tmp.mo[][])%mod;
printf("%lld\n",res);
vis=;
break;
}//否则,不断的分段求解矩阵的值,并将矩阵的值进行修改
ll j=min(n,p/(p/i));
Marix tmp;
tmp=m;
tmp.mo[][]=p/i;
tmp=powmod(tmp,j-i+);
ll A=(tmp.mo[][]*b+tmp.mo[][]*a+tmp.mo[][])%mod;
ll B=(tmp.mo[][]*b+tmp.mo[][]*a+tmp.mo[][])%mod;
a=A,b=B;
i=j+;
}
if(!vis) printf("%lld\n",b);
}
return ;
}

新感受!

做的时候大概的想法是想到的了 , 但是无法解决F1=A, F2=B , 的情况  , 之后看的别人的代码才大悟出来 ;

只要求出最后的答案后,在成系数

fn=mo[0][0]*(f2) + mo[0][1]*(f1) 就好拉

例如

ll ans=(tmp.mo[0][0]*b+tmp.mo[0][1]*a+tmp.mo[0][2])%mod;

参考代码

/*
* Author: windystreet
* Date : 2018-08-14 09:46:10
* Motto : Think twice, code once.
*/
#include<bits/stdc++.h> using namespace std; #define X first
#define Y second
#define eps 1e-5
#define gcd __gcd
#define pb push_back
#define PI acos(-1.0)
#define lowbit(x) (x)&(-x)
#define bug printf("!!!!!\n");
#define mem(x,y) memset(x,y,sizeof(x)) typedef long long LL;
typedef long double LD;
typedef pair<int,int> pii;
typedef unsigned long long uLL; const int maxn = 1e5+;
const int INF = <<;
const int mod = 1e9+; struct Matrix
{
int n,m;
LL ma[][];
Matrix (int x,int y):n(x),m(y){clear();}
void set(int n_,int m_){n = n_,m = m_;}
LL *operator[](int x){return ma[x];}
Matrix operator*(Matrix x){
Matrix res(n,x.m);
for(int i=;i<=n;i++)
for(int j=;j<=x.m;j++)
for(int k=;k<=m;k++)
(res[i][j]+=ma[i][k]*x[k][j]%mod+mod)%=mod;
return res;
}
Matrix operator ^(int y){
Matrix x(n,m);
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
x[i][j]=ma[i][j];
Matrix res(x.n,x.n);
for(int i=;i<=x.n;i++)
res[i][i]=;
for(;y;y>>=,x=x*x)
if(y&)res=res*x;
return res;
}
void print(){
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
printf("%lld%c",ma[i][j]," \n"[j==m]);
}
void clear(){mem(ma,);}
};
int a ,b,c,d ,p,n;
pii f(int x,int y){
if(!x)return make_pair(y,n);
int l = max(y,(p+x+)/(x+)),r = min(n,p/x);
return make_pair(l,r);
} void solve(){
scanf("%d%d%d%d%d%d",&a,&b,&c,&d,&p,&n);
if(n==)printf("%d\n",a);
if(n==)printf("%d\n",b);
else{
Matrix x(,),y(,);
x[][] = b,x[][]=a,x[][]=;
for(int i=;i<=n;){
pii seg = f(p/i,i);
y[][]=d,y[][] = ,y[][] = ;
y[][]=c,y[][] = ,y[][] = ;
y[][]=p/i,y[][]=,y[][] = ;
x = x*(y^(seg.Y - seg.X + ));
i = seg.Y+;
}
printf("%lld\n",x[][] );
} return;
} int main()
{
// freopen("F:\\in.txt","r",stdin);
// freopen("out.txt","w",stdout);
// ios::sync_with_stdio(false);
int t = ;
scanf("%d",&t);
while(t--){
// printf("Case %d: ",cas++);
solve();
}
return ;
}

HDU6395(分段+矩阵快速幂)的更多相关文章

  1. BZOJ 2326 数学作业(分段矩阵快速幂)

    实际上,对于位数相同的连续段,可以用矩阵快速幂求出最后的ans,那么题目中一共只有18个连续段. 分段矩阵快速幂即可. #include<cstdio> #include<iostr ...

  2. HDU 6395 分段矩阵快速幂 HDU 6386 建虚点+dij

    http://acm.hdu.edu.cn/showproblem.php?pid=6395 Sequence Time Limit: 4000/2000 MS (Java/Others)    Me ...

  3. HDU 6395 Sequence(分段矩阵快速幂)题解

    题意: 已知\(A,B,C,D,P,n\)以及 \[\left\{ \begin{aligned} & F_1 = A \\ & F_2 = B\\ & F_n = C*F_{ ...

  4. hdu6395 (矩阵快速幂+分块)

    Online Judge Online Exercise Online Teaching Online Contests Exercise Author F.A.Q Hand In Hand Onli ...

  5. 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里是不变的,可以数论分块,再在每一段里 ...

  6. 数学--数论--HDU - 6395 Let us define a sequence as below 分段矩阵快速幂

    Your job is simple, for each task, you should output Fn module 109+7. Input The first line has only ...

  7. hdu6395 Sequence(分段矩阵快速幂)

    Sequence 题目传送门 解题思路 可以比较容易的推出矩阵方程,但是由于p/i向下取整的值在变,所以要根据p/i的变化将矩阵分段快速幂.p/i一共有sqrt(p)种结果,所以最多可以分为sqrt( ...

  8. hdu6395 /// 分块矩阵快速幂

    题目大意: F(1)=A, F(2)=B,  F(i)=C*F(i-2)+D*F(i-1)+p/i(向下取整) 给定A B C D p n 求F(n) 构造 矩阵A *   矩阵B        =  ...

  9. [hdu-6395]Sequence 分块+矩阵快速幂

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6395 因为题目数据范围太大,又存在递推关系,用矩阵快速幂来加快递推. 每一项递推时  加的下取整的数随 ...

随机推荐

  1. 728. Self Dividing Numbers可以自己除以自己的数字

    [抄题]: A self-dividing number is a number that is divisible by every digit it contains. For example, ...

  2. 566. Reshape the Matrix矩阵重排

    [抄题]: In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a ...

  3. ubuntu16.04 ARM平台移植xmlrpc-c1.39.12

    1. xmlrpc-c依赖与libcurl 参考另外一篇随笔:https://www.cnblogs.com/flyinggod/p/10148228.html 2. 下载源代码 http://xml ...

  4. adb shell unauthorized问题

    出现unauthorized 一般插上usb后,手机会弹出一个要求你授权debugging的对话框,如果没有的话,就是rsa_key有问题: /adb_keys. User-installed key ...

  5. jQuery基础教程-第8章-001Adding new global functions

    一. 1.To add a function to the jQuery namespace, we can just assign the new function asa property of ...

  6. matplotlib的颜色和控制条

    为了方便记忆,收藏备用 一 linestyle '-' solid line style '--' dashed line style '-.' dash-dot line style ':' dot ...

  7. Luogu 4284 [SHOI2014]概率充电器

    BZOJ 3566 树形$dp$ + 概率期望. 每一个点的贡献都是$1$,在本题中期望就等于概率. 发现每一个点要通电会在下面三件事中至少发生一件: 1.它自己通电了. 2.它的父亲给它通电了. 3 ...

  8. python,itertools模块

    itertools模块的使用: # coding=utf-8 """ itertools模块 """ import itertools im ...

  9. WCF把书读薄(2)——消息交换、服务实例、会话与并发

    上一篇:WCF把书读薄(1)——终结点与服务寄宿 八.消息交换模式 WCF服务的实现是基于消息交换的,消息交换模式一共有三种:请求回复模式.单向模式与双工模式. 请求回复模式很好理解,比如int Ad ...

  10. oracle数据库单表查询

    今天给大家分享的是关于数据库的单表查询,像单表查询/多表查询/分组查询/子查询,这些方法的使用在实际项目过程中会经常用到,作为一名合格的测试人员如果不会数据库那肯定是不行的,行走江湖可能随时会面临被侮 ...