codeforces gym # 101161G - Binary Strings (matrix fast power)

Topic links:

http://codeforces.com/gym/101161/attachments

Meaning of the questions:

 

data range:

 

analysis: 

 

ac Code:

#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;
}

  

Guess you like

Origin www.cnblogs.com/carcar/p/11493374.html