codeforces gym #101161G - Binary Strings(矩阵快速幂)

Posted carcar

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了codeforces gym #101161G - Binary Strings(矩阵快速幂)相关的知识,希望对你有一定的参考价值。

题目链接:

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

题意:

 

数据范围:

 

分析: 

 

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;

  

以上是关于codeforces gym #101161G - Binary Strings(矩阵快速幂)的主要内容,如果未能解决你的问题,请参考以下文章

B - Average Gym - 101161B 组合数学

模拟NEERC15 G Generators(2015-2016 ACM-ICPC)(Codeforces GYM 100851)

codeforces gym101243 A C D E F G H J

Codeforces-GYM101873 G Water Testing 皮克定理

codeforces gym 100952 A B C D E F G H I J

codeforces Gym 101063 C