Hdu

Stella981
• 阅读 572

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

2018 Multi-University Training Contest 7

对n进行分段 ,每一段内的 floor(P/ni) 都是一定的,这样就可以对每一段直接跑矩阵快速幂了。分段的时候应该可以O(1)的,比赛时候

没想到直接上的二分= =。

(把第78行求边界换成这句代码就是O(1)了:          LL j=P/i==0?N:min(N,P/(P/i));

  1 #include <bits/stdc++.h>
  2 using namespace std;
  3 #define LL long long 
  4 LL mod=1e9+7;
  5 LL f[41000],T,A,B,C,D,P,N;
  6 struct matrix{
  7     LL a[3][3];
  8     matrix(){
  9         memset(a,0,sizeof(a));
 10     }
 11     matrix operator*(matrix &tmp){
 12         matrix ans;
 13         for(int i=0;i<3;++i){
 14             for(int j=0;j<3;++j){
 15                 for(int k=0;k<3;++k){
 16                     ans.a[i][j]=(ans.a[i][j]+a[i][k]*tmp.a[k][j]);
 17                 //    ans.a[i][j]%=mod;
 18                 }
 19                 ans.a[i][j]%=mod;
 20             }
 21         }
 22         return ans;
 23     }
 24     
 25     void show(){
 26         puts("---");
 27         for(int i=0;i<3;++i){
 28             for(int j=0;j<3;++j){
 29                 cout<<a[i][j]<<' ';
 30             }cout<<endl;
 31         }
 32         puts("---");
 33     }
 34 }X,U;
 35 matrix qpow(matrix A,int b){
 36     matrix ans=U;
 37     while(b){
 38         if(b&1)ans=ans*A;
 39         A=A*A;
 40         b>>=1;
 41     }
 42     return ans;
 43 }
 44 LL solve(LL i){
 45     LL key=P/i;
 46     LL l=i,r=N;
 47     while(l<r){
 48         int mid=r-(r-l)/2;
 49         //cout<<mid<<' '<<P/mid<<endl;
 50         if(P/mid==key) l=mid;
 51         else if(P/mid<key){
 52             r=mid-1;
 53         }
 54         else{
 55             l=mid+1;
 56         }
 57     }
 58     return l;
 59 }
 60 int main(){
 61     U.a[0][0]=U.a[1][1]=U.a[2][2]=1;
 62     scanf("%lld",&T);
 63     while(T--){
 64         scanf("%lld%lld%lld%lld%lld%lld",&A,&B,&C,&D,&P,&N);
 65         
 66         f[1]=A%mod;
 67         f[2]=B%mod;
 68         if(N<=2){
 69             cout<<f[N]<<endl;
 70             continue;
 71         }
 72         //for(int i=3;i<=N;++i) f[i]=(C*f[i-2]%mod+D*f[i-1]%mod+(P/i))%mod;
 73         memset(X.a,0,sizeof(X.a));
 74         X.a[0][0]=D,X.a[0][1]=1;
 75         X.a[1][0]=C,X.a[2][2]=1;
 76         LL f1=f[2],f2=f[1];
 77         for(LL i=3;i<=N;){
 78             LL j=solve(i);
 79             //cout<<i<<' '<<j<<endl;
 80             X.a[2][0]=P/i;
 81             matrix ans=qpow(X,j-i+1);
 82             LL _f1=(f1*ans.a[0][0]%mod+f2*ans.a[1][0]%mod+ans.a[2][0])%mod;
 83             LL _f2=(f1*ans.a[0][1]+f2*ans.a[1][1]+ans.a[2][1])%mod;
 84             
 85             f1=_f1;
 86             f2=_f2;
 87             //cout<<j<<' '<<f1<<' '<<f2<<' '<<f[j]<<' '<<f[j-1]<<endl;
 88             i=j+1;
 89         }
 90         cout<<f1<<endl;
 91         //printf("%lld %lld\n",f1,f[N]);
 92         
 93     }    
 94     return 0;
 95 }
 96 /*
 97 2
 98 3 3 2 1 3 5
 99 3 2 2 2 1 4
100 
101 */
点赞
收藏
评论区
推荐文章
待兔 待兔
4个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Wesley13 Wesley13
3年前
hdu2204 Eddy's爱好
原题:点击打开链接(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Facm.hdu.edu.cn%2Fshowproblem.php%3Fpid%3D2204)题意很明确了,即:给你一个正整数N,确定在1到N之间有多少个可以表示成M^K(K1)的数。本题目为容斥原
Stella981 Stella981
3年前
2019 HDOJ Multi
服务器时不时爆炸,有点难受。题目链接:http://acm.hdu.edu.cn/userloginex.php?cid849(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Facm.hdu.edu.cn%2Fuserloginex.php%3Fcid%3D849)
Stella981 Stella981
3年前
Jenkins+Ansible+Gitlab自动化部署三剑客
JenkinsAnsibleGitlab自动化部署三剑客小中大showerlee2016031113:00Ansible(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fwww.
Wesley13 Wesley13
3年前
P2P技术揭秘.P2P网络技术原理与典型系统开发
Modular.Java(2009.06)\.Craig.Walls.文字版.pdf:http://www.t00y.com/file/59501950(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fwww.t00y.com%2Ffile%2F59501950)\More.E
Easter79 Easter79
3年前
Swift项目兼容Objective
!456.jpg(http://static.oschina.net/uploads/img/201509/13172704_1KcG.jpg"1433497731426906.jpg")本文是投稿文章,作者:一叶(博客(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2F00red
可莉 可莉
3年前
2019 HDOJ Multi
服务器时不时爆炸,有点难受。题目链接:http://acm.hdu.edu.cn/userloginex.php?cid849(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Facm.hdu.edu.cn%2Fuserloginex.php%3Fcid%3D849)
Stella981 Stella981
3年前
HDU1005 Number Sequence
HDU1005NumberSequence(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Facm.hdu.edu.cn%2Fshowproblem.php%3Fpid%3D1005)对于公式f\n\A\f\n1\B\f\n2\,因为对于f\n1\
Easter79 Easter79
3年前
The Complete Guide To Rooting Any Android Phone
PhoneWhitsonGordon(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fwww.lifehacker.com.au%2Fauthor%2Fwhitsongordon%2F)7April,20118:00AMShare(https://ww
Wesley13 Wesley13
3年前
Java大数统计
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid1316(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Facm.hdu.edu.cn%2Fshowproblem.php%3Fpid%3D1316)题目描述:!(https://o