### 小紅帽的畫筆(NOIP模擬賽Round 7)

【數據范圍】

————————————————我是分割線————————————————————

TOP1：找規律

a=a+b-b;a+b=a+2b-b;2a+2b=2a+3b-b;3a+4b=3a+5b-b.....

TOP2：構造矩陣

———————————————我是分割線—————————————————

`#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int n;struct matrix{    unsigned long long mat[2][2];};unsigned long long l,r,mod;long long mul(long long x,long long y){    if (x<y) swap(x,y);    register long long z=0;    for (;y;y>>=1,x<<=1,x=x>=mod?x-mod:x) if (y&1) z+=x,z=z>=mod?z-mod:z;    return z;}matrix multiply(matrix a,matrix b){    unsigned long long sum=0;    matrix c;    memset(c.mat,0,sizeof(c.mat));    for(int i=0;i<=1;i++)        for(int j=0;j<=1;j++)        {            sum=0;            for(int k=0;k<=1;k++)            {                long long numqq=mul(a.mat[i][k],b.mat[k][j]);                if(mod>1000000000)sum+=numqq,sum%=mod;                else sum+=(a.mat[i][k]*b.mat[k][j])%mod;            }            c.mat[i][j]=sum;        }    return c;    } matrix matmod(matrix a,unsigned long long k){    matrix res;    memset(res.mat,0,sizeof(res.mat));    for(int i=0;i<=1;i++)res.mat[i][i]=1;    while(k)    {        if(k&1)res=multiply(res,a);        k>>=1;        a=multiply(a,a);    }    return res;}unsigned long long work(unsigned long long x){    if(x==0)return 0;    if(x==1)return 1;    if(x==2)return 2;    matrix a,b;    memset(a.mat,0,sizeof(a.mat));    memset(b.mat,0,sizeof(b.mat));    for(int i=0;i<=1;i++)a.mat[i][1]=1;    for(int i=0;i<=1;i++)b.mat[i][0]=1;    a.mat[1][0]=1;     a=matmod(a,x);    a=multiply(a,b);    return (a.mat[1][0]-1+mod)%mod;} int main(){    freopen("artist.in","r",stdin);    freopen("artist.out","w",stdout);    scanf("%d%lld",&n,&mod);    for(int i=1;i<=n;i++)    {        scanf("%lld%lld",&l,&r);        unsigned long long num1=0,num2=0;        num1=work(l-1);        num2=work(r);        unsigned long long ans=num2-num1+mod;        printf("%lld\n",ans%mod);     }    return 0;    fclose(stdin);    fclose(stdout);} `