codeforces gym #101161G - Binary Strings(矩阵快速幂,前缀斐波那契)
题目链接:
http://codeforces.com/gym/101161/attachments
题意:
$T$组数据
每组数据包含$L,R,K$
计算$\sum_{k|n}^{}F(n)$
定义$F(n)$为斐波那契数列第$n$项
数据范围:
$1\leq T\leq 10000$
$1\leq L\leq 10^{18}$
$1\leq R\leq 10^{18}$
分析:
博客来源:https://blog.csdn.net/qq_41552508/article/details/97161365
ac代码:
#include<bits/stdc++.h> using namespace std; #define ll long long #define pii pair<int,int> const int maxn = 1e5+100; const int mod=1e9+7; struct Node{ ll num[4][4]; Node(){memset(num,0,sizeof(num));} }dd,ss,tt,gg,zz; ll dpk,dpk1; Node mul(Node a,Node b){ Node res; for(int i=1;i<=3;i++) for(int j=1;j<=3;j++) for(int k=1;k<=3;k++) res.num[i][j]=(res.num[i][j]+a.num[i][k]*b.num[k][j]%mod)%mod; return res; } Node qpow(Node x,ll n){ Node res=dd; while(n>0){ if(n&1) res=mul(res,x); x=mul(x,x); n/=2; } return res; } ll cal(ll x){ if(x==0)return 0; Node res=qpow(gg,x-1); return (dpk*res.num[3][1]%mod+dpk1*res.num[3][2]%mod+dpk*res.num[3][3]%mod)%mod; } int main() { dd.num[1][1]=dd.num[2][2]=dd.num[3][3]=1; ss.num[1][1]=ss.num[1][2]=ss.num[2][1]=ss.num[3][1] =ss.num[3][2]=ss.num[3][3]=1; tt.num[1][1]=tt.num[1][2]=tt.num[2][1]=tt.num[3][3]=1; int T; scanf("%d",&T); for(int cn=1;cn<=T;cn++){ ll L,R,k; scanf("%lld %lld %lld",&L,&R,&k); Node zz=qpow(ss,k-2); dpk=(zz.num[1][1]*3+zz.num[1][2]*2+zz.num[1][3]*5)%mod; dpk1=(zz.num[2][1]*3+zz.num[2][2]*2+zz.num[2][3]*5)%mod; if(k==1)dpk=2,dpk1=1; gg=mul(ss,qpow(tt,k-1)); printf("Case %d: %lld\n",cn,(cal(R/k)-cal((L-1)/k)+mod)%mod); } return 0; }