Sequence

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 52    Accepted Submission(s): 13

Problem Description
Let us define a sequence as below

⎧⎩⎨⎪⎪⎪⎪⎪⎪F1F2Fn===ABC⋅Fn−2+D⋅Fn−1+⌊Pn⌋

Your job is simple, for each task, you should output Fn module 109+7.

 
Input
The first line has only one integer T, indicates the number of tasks.

Then, for the next T lines, each line consists of 6 integers, A , B, C, D, P, n.

1≤T≤200≤A,B,C,D≤1091≤P,n≤109

 
Sample Input
2
3 3 2 1 3 5
3 2 2 2 1 4
 
Sample Output
36
24
 
Source
 
  对n进行分段 ,每一段内的 floor(P/ni) 都是一定的,这样就可以对每一段直接跑矩阵快速幂了。分段的时候应该可以O(1)的,比赛时候
没想到直接上的二分= =。
  (把第78行求边界换成这句代码就是O(1)了:          LL j=P/i==0?N:min(N,P/(P/i));
  

 #include <bits/stdc++.h>
using namespace std;
#define LL long long
LL mod=1e9+;
LL f[],T,A,B,C,D,P,N;
struct matrix{
LL a[][];
matrix(){
memset(a,,sizeof(a));
}
matrix operator*(matrix &tmp){
matrix ans;
for(int i=;i<;++i){
for(int j=;j<;++j){
for(int k=;k<;++k){
ans.a[i][j]=(ans.a[i][j]+a[i][k]*tmp.a[k][j]);
// ans.a[i][j]%=mod;
}
ans.a[i][j]%=mod;
}
}
return ans;
} void show(){
puts("---");
for(int i=;i<;++i){
for(int j=;j<;++j){
cout<<a[i][j]<<' ';
}cout<<endl;
}
puts("---");
}
}X,U;
matrix qpow(matrix A,int b){
matrix ans=U;
while(b){
if(b&)ans=ans*A;
A=A*A;
b>>=;
}
return ans;
}
LL solve(LL i){
LL key=P/i;
LL l=i,r=N;
while(l<r){
int mid=r-(r-l)/;
//cout<<mid<<' '<<P/mid<<endl;
if(P/mid==key) l=mid;
else if(P/mid<key){
r=mid-;
}
else{
l=mid+;
}
}
return l;
}
int main(){
U.a[][]=U.a[][]=U.a[][]=;
scanf("%lld",&T);
while(T--){
scanf("%lld%lld%lld%lld%lld%lld",&A,&B,&C,&D,&P,&N); f[]=A%mod;
f[]=B%mod;
if(N<=){
cout<<f[N]<<endl;
continue;
}
//for(int i=3;i<=N;++i) f[i]=(C*f[i-2]%mod+D*f[i-1]%mod+(P/i))%mod;
memset(X.a,,sizeof(X.a));
X.a[][]=D,X.a[][]=;
X.a[][]=C,X.a[][]=;
LL f1=f[],f2=f[];
for(LL i=;i<=N;){
LL j=solve(i);
//cout<<i<<' '<<j<<endl;
X.a[][]=P/i;
matrix ans=qpow(X,j-i+);
LL _f1=(f1*ans.a[][]%mod+f2*ans.a[][]%mod+ans.a[][])%mod;
LL _f2=(f1*ans.a[][]+f2*ans.a[][]+ans.a[][])%mod; f1=_f1;
f2=_f2;
//cout<<j<<' '<<f1<<' '<<f2<<' '<<f[j]<<' '<<f[j-1]<<endl;
i=j+;
}
cout<<f1<<endl;
//printf("%lld %lld\n",f1,f[N]); }
return ;
}
/*
2
3 3 2 1 3 5
3 2 2 2 1 4 */
 

HDU-6395-矩阵快速幂的更多相关文章

  1. HDU 2855 (矩阵快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2855 题目大意:求$S(n)=\sum_{k=0}^{n}C_{n}^{k}Fibonacci(k)$ ...

  2. HDU 4471 矩阵快速幂 Homework

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4471 解题思路,矩阵快速幂····特殊点特殊处理····· 令h为计算某个数最多须知前h个数,于是写 ...

  3. HDU - 1575——矩阵快速幂问题

    HDU - 1575 题目: A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973.  Input数据的第一行是一个T,表示有T组数据. 每组数据的第一行有n( ...

  4. hdu 1757 (矩阵快速幂) 一个简单的问题 一个简单的开始

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1757 题意不难理解,当x小于10的时候,数列f(x)=x,当x大于等于10的时候f(x) = a0 * ...

  5. 随手练——HDU 5015 矩阵快速幂

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5015 看到这个限时,我就知道这题不简单~~矩阵快速幂,找递推关系 我们假设第一列为: 23 a1 a2 ...

  6. HDU 3802 矩阵快速幂 化简递推式子 加一点点二次剩余知识

    求$G(a,b,n,p) = (a^{\frac {p-1}{2}}+1)(b^{\frac{p-1}{2}}+1)[(\sqrt{a} + \sqrt{b})^{2F_n} + (\sqrt{a} ...

  7. How many ways?? HDU - 2157 矩阵快速幂

    题目描述 春天到了, HDU校园里开满了花, 姹紫嫣红, 非常美丽. 葱头是个爱花的人, 看着校花校草竞相开放, 漫步校园, 心情也变得舒畅. 为了多看看这迷人的校园, 葱头决定, 每次上课都走不同的 ...

  8. HDU 5950 矩阵快速幂

    Recursive sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  9. hdu 1757 矩阵快速幂 **

    一看正确率这么高,以为是水题可以爽一发,结果是没怎么用过的矩阵快速幂,233 题解链接:点我 #include<iostream> #include<cstring> ; us ...

  10. HDU 4686 矩阵快速幂 Arc of Dream

    由式子的性质发现都是线性的,考虑构造矩阵,先有式子,a[i] = ax * a[i-1] + ay; b[i] = bx*b[i-1] +by; a[i]*b[i] = ax*bx*a[i-1]*b[ ...

随机推荐

  1. Images之multi-stage builds

    原文链接 Use multi-stage builds Multi-stage builds are a new feature requiring Docker 17.05 or higher on ...

  2. Linux 下上手 STC89C52RC

    第一次接触单片机,自然选择了简单的51单片机.然而我的操作系统是 Linux .在 Windows 下上手51似乎很容易.但是 Linux 上搭建 51 开发环境不是很顺. 那么谈谈 Linux 我如 ...

  3. 洛谷p1732 活蹦乱跳的香穗子 二维DP

    今天不BB了,直接帖原题吧  地址>>https://www.luogu.org/problem/show?pid=1732<< 题目描述 香穗子在田野上调蘑菇!她跳啊跳,发现 ...

  4. [Err] 1449 - The user specified as a definer ('rybhe'@'%') does not exist

    转载: 最近在做一个项目,由于服务器切换,所以需要将原有服务器的mysql数据表以及存储过程导入到另一个服务器的mysql数据库中.导入完成之后以为一切是那么的简单,却没有想到总还是出现了一些莫名其妙 ...

  5. [转载]Linux中的网络接口及LO回环接口

    转自:https://blog.csdn.net/weixin_39863747/article/details/80564358 Linux中的网络接口及LO回环接口 2018年06月04日 10: ...

  6. HDU 5724 Chess(SG函数+状态压缩)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=5724 题意: 现在有一个n*20的棋盘,上面有一些棋子,双方每次可以选择一个棋子把它移动到其右边第一 ...

  7. vue中click阻止事件冒泡,防止触发另一个事件

    在使用el-upload组件时,在其中放置了一个删除按钮的图片. 当点击图片,本想只删除上传的视频,但是意外触发了el-upload中的事件 解决办法:用stop,结果只删除当前预览,不触发上传事件. ...

  8. 转 这种方法可以免去自己计算大文件md5 的麻烦

    using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEditor;using ...

  9. python 删除文件夹

    只能删除空文件夹,删除非空文件夹会报错 >>> import os >>> os.rmdir("/tmp/ssh-GyoPWOFZ47") Tr ...

  10. Python 函数也是一种对象

    函数也是一种对象 func = lambda x, y: x+y print func(3, 4) def func(x, y): return x+y print func(3, 4) 效果相同. ...