原题地址

Sequence

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

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
 
Recommend
 
矩阵快速幂,但是中间带着一个跟着N变化的值;一开始想直接硬构造出一个矩阵;发现行不通就和队友搞其他题了;
后来发现这P/n在每个sqrt(q)范围内都是一定的;所以可以试着查找p/n这个值最大到哪一项,然后分别快速幂;很神奇的就是为什么p/(p/i)就是这个p/i的最大项;到现在还不明白‘
我的矩阵
 
Fn    D C p/i     Fn-1
Fn-1   1  0  0      Fn-2
1      0  0  1        1
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=1e9+7;
const int maxn=3e6+50;
const ll inf=0x3f3f3f3f3f3f;
ll a,b,c,d,p,n;
ll k,kk;
ll fun[100000005];
struct node{
ll a[3][3];
void init(){
memset(a,0,sizeof(a));
for(int i=0;i<3;i++){
a[i][i]=1;
}
}
};
node mul(node a,node b)
{
node ans;
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
ans.a[i][j]=0;
for(int k=0;k<3;k++){
ans.a[i][j]+=a.a[i][k]*b.a[k][j];
ans.a[i][j]%=mod;
}
}
}return ans;
}
void output(node a){
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
cout<<a.a[i][j]<<" ";
}cout<<endl;
}
}
node qpow(node a,ll n){
node ans;
ans.init();
while(n){
if(n&1)ans=mul(ans,a);
a=mul(a,a);
n/=2;
}
return ans;
}
node ac;
void init(ll d,ll c,ll x){
ac.a[0][0]=d;ac.a[0][1]=c;ac.a[0][2]=x; /*ac D C P/I */
ac.a[1][0]=1;ac.a[1][1]=0;ac.a[1][2]=0; /* 1 0 0 */
ac.a[2][0]=0;ac.a[2][1]=0;ac.a[2][2]=1; /* 0 0 1 */
}
node anw;
void init2(ll a,ll b){
anw.a[0][0]=b;anw.a[1][0]=a;anw.a[2][0]=1; /*fn*/
} /*fn-1*/
node init3(node one,node two){ /*1*/
node retu;
retu.a[0][0]=(one.a[0][0]*two.a[0][0]+one.a[0][1]*two.a[1][0]+one.a[0][2]*two.a[2][0])%mod;
retu.a[1][0]=(one.a[1][0]*two.a[0][0]+one.a[1][1]*two.a[1][0]+one.a[1][2]*two.a[2][0])%mod;
retu.a[2][0]=(one.a[2][0]*two.a[0][0]+one.a[2][1]*two.a[1][0]+one.a[2][2]*two.a[2][0])%mod;
return retu;
} int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);
int t;
cin>>t;
while(t--){
cin>>a>>b>>c>>d>>p>>n;
k=p/n;kk=p%n;
if(n==1){cout<<a<<endl;continue;}
if(n==2){cout<<b<<endl;continue;}
else{
init(d,c,1);
init2(a,b);
//output(ac);
// output(anw);
for(ll i=3;i<=n;){
if(p/i==0){
ll num=n-i+1;
init(d,c,0);
node mid=qpow(ac,num);
anw=init3(mid,anw);
break;
}
else{
ll k=min(n,p/(p/i));//num表示p/i这个值最大到哪一个
ll num=k-i+1;
init(d,c,p/i);
node mid=qpow(ac,num);
anw=init3(mid,anw);
i=k+1;
}
} cout<<anw.a[0][0]<<endl;
}
}
return 0;
}

  

HDU 6395 2018 Multi-University Training Contest 7 (快速幂+分块)的更多相关文章

  1. hdu 4915 Parenthese sequence--2014 Multi-University Training Contest 5

    主题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4915 Parenthese sequence Time Limit: 2000/1000 MS (Ja ...

  2. hdu 4902 Nice boat--2014 Multi-University Training Contest 4

    题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=4902 Nice boat Time Limit: 30000/15000 MS (Java/Othe ...

  3. hdu 4925 Apple Tree--2014 Multi-University Training Contest 6

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4925 Apple Tree Time Limit: 2000/1000 MS (Java/Others ...

  4. HDU校赛 | 2019 Multi-University Training Contest 6

    2019 Multi-University Training Contest 6 http://acm.hdu.edu.cn/contests/contest_show.php?cid=853 100 ...

  5. HDU校赛 | 2019 Multi-University Training Contest 5

    2019 Multi-University Training Contest 5 http://acm.hdu.edu.cn/contests/contest_show.php?cid=852 100 ...

  6. HDU校赛 | 2019 Multi-University Training Contest 4

    2019 Multi-University Training Contest 4 http://acm.hdu.edu.cn/contests/contest_show.php?cid=851 100 ...

  7. HDU校赛 | 2019 Multi-University Training Contest 3

    2019 Multi-University Training Contest 3 http://acm.hdu.edu.cn/contests/contest_show.php?cid=850 100 ...

  8. HDU校赛 | 2019 Multi-University Training Contest 2

    2019 Multi-University Training Contest 2 http://acm.hdu.edu.cn/contests/contest_show.php?cid=849 100 ...

  9. HDU校赛 | 2019 Multi-University Training Contest 1

    2019 Multi-University Training Contest 1 http://acm.hdu.edu.cn/contests/contest_show.php?cid=848 100 ...

随机推荐

  1. 《Cracking the Coding Interview》——第5章:位操作——题目2

    2014-03-19 05:47 题目:给定一个double型浮点数,输出其二进制表示,如果不能在32个字符内完成输出,则输出“ERROR”. 解法:如果你熟悉IEEE754标准,应该知道double ...

  2. TIDB介绍

    TiDB 是什么? TiDB 是一个分布式 NewSQL 数据库.它支持水平弹性扩展.ACID 事务.标准 SQL.MySQL 语法和 MySQL 协议,具有数据强一致的高可用特性,是一个不仅适合 O ...

  3. JS——BOM、DOM

    BOM.DOM BOM window对象 history对象 location对象 screen对象 DOM DOM对HTML元素访问操作 HTML DOM树 DOM 节点 DOM访问HTML元素 D ...

  4. pychram 的一些小技巧

    1.如何添加头部注释代码 进入设置 File->Settings->Editor->File and Code Templeates -> Python Script 添加以下 ...

  5. 如何将自己写的代码上传到github上

    忙活了一下午终于成功把代码上传到github上. 接下来就是我上传代码的步骤: ①首先注册github账号,登录,创建新仓库 ,点击+,点击new repository 得到如下页面,填写仓库名,自己 ...

  6. heat launch an instance

    在包含Orchestration服务的环境中,可以创建启动实例的堆栈 创建yam文件 heat_template_version: 2015-10-15 description: Launch a b ...

  7. Mini-MBA记录

    最近学完了Mini-MBA的课程,对课程讲述的人力资源,创新,财务,战略,领导力等方面有了更深一些的了解,在此之上也做了一些笔记,如果课程信息披露是被允许的,后续把这些笔记贴出来,作为自己以后的参考.

  8. 爬虫:Scrapy7 - Scrapy终端(Scrapy shell)

    Scrapy 终端是一个交互终端,可以在未启动 spider 的情况下尝试及调试你的爬取代码.其本意是用来测试提取数据的代码,不过可以将其作为正常的 Python 终端,在上面测试任何 Python ...

  9. webpack 基础

    1.安装: npm install --save-dev webpack         npm install --save-dev webpack@<version> 如果是webpa ...

  10. 团队Alpha版本(七)冲刺

    目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:凯琳 组员6:翟丹丹 组员7:何家伟 组员8:政演 组员9:黄鸿杰 组员10:刘一好 组员11:何宇恒 展示 ...