原题地址

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. Android FrameWork 概述

    Framework是什么 Framework的中文意思是“框架”,在软件开发中通常指开发框架,在一个系统中处于内核层之上,为顶层应用提供接口,被设计用来帮助开发者快速开发顶层应用,而不必关心系统内核运 ...

  2. 【Max Points on a Line 】cpp

    题目: Given n points on a 2D plane, find the maximum number of points that lie on the same straight li ...

  3. 关于python中的 if __name__ == 'main'

    name 是内置变量,它表示的是当前所在模块的名字,同时还能反应一个包的结构. a ├── b │   ├── c.py │   └── __init__.py └── __init__.py 目录中 ...

  4. Python学习-django-ModelForm组件

    ModelForm a. class Meta: model, # 对应Model的 fields=None, # 字段 exclude=None, # 排除字段 labels=None, # 提示信 ...

  5. ssh-centos74.sh

    #!/bin/bash sed -i 's/#PermitRootLogin yes/PermitRootLogin yes/g' /etc/ssh/sshd_config sed -i 's/#Us ...

  6. 【志银】php5.6-Apache2.4-mysql5.6环境配置(win7_64位)

    ----------------------------------------------------- ★软件工具:(下载时注意下载相应版本,不同版本安装细节可能会有差异!!) 1>http ...

  7. J2EE的十三个技术——EJB之概述

    含义: 企业级的JavaBeans(Enterprise JavaBean),其设计目标是部署分布式应用程序. EJB是J2EE的一部分,称为Java企业Bean,它把使用Java开发的服务器组件的部 ...

  8. SystemTap 用法

    SystemTap需要内核符号表: http://ddebs.ubuntu.com/pool/main/l/linux/ 基本语法: next对应C中的return,中途返回: 今晚遗留了两个问题: ...

  9. Android M中 JNI的入门学习

    今年谷歌推出了Android 6.0,作为安卓开发人员,对其学习掌握肯定是必不可少的,今天小编和大家分享的就是Android 6.0中的 JNI相关知识,这是在一个安卓教程网上看到的内容,感觉很不错, ...

  10. [poj] 3057 Evacuation

    原题 题目大意 墙壁"X",空区域(都是人)".", 门"D". 人向门移动通过时视为逃脱,门每秒能出去一个人,人可以上下左右移动,墙阻止移 ...