BZOJ3240 NOI2013矩阵游戏(数论)

  必修五题。

// luogu-judger-enable-o2
#include<iostream> 
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
int read()
{
    int x=0,f=1;char c=getchar();
    while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
    while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
    return x*f;
}
#define N 1000010
#define P 1000000007
int n,m,a[N],b[N],c,d,u,v,p,q,ans,x,y,k,tmp1,tmp2;
int ksm(int a,int k)
{
    if (k==0) return 1;
    int tmp=ksm(a,k>>1);
    if (k&1) return 1ll*tmp*tmp%P*a%P;
    else return 1ll*tmp*tmp%P;
}
int inv(int x){return ksm((x+P)%P,P-2);}
int main()
{
    char ch=getchar();
    while (ch>='0'&&ch<='9') k=(10ll*k+(ch^48))%(P-1),tmp1=(10ll*tmp1+(ch^48))%P,ch=getchar();
    while (ch<'0'||ch>'9') ch=getchar();
    while (ch>='0'&&ch<='9') p=(10ll*p+(ch^48))%(P-1),tmp2=(10ll*tmp2+(ch^48))%P,ch=getchar();
    u=read(),v=read(),c=read(),d=read();
    if (u>1) p=(p+P-2)%(P-1),p=ksm(u,p),q=1ll*v*inv(u-1)%P*(p+P-1)%P;
    else tmp2=(tmp2+P-1)%P,q=1ll*tmp2*v%P,p=1;
    x=p,y=q;
    p=1ll*p*c%P;q=(d+1ll*c*q%P)%P;
    if (p>1) k=(k+P-2)%(P-1),q=1ll*q*inv(p-1)%P,p=ksm(p,k),q=1ll*q*(p+P-1)%P,ans=(p+q)%P;
    else tmp1=(tmp1+P-1)%P,ans=(1+1ll*tmp1*q)%P;
    ans=(1ll*ans*x%P+y+P)%P;
    cout<<ans;
    return 0;
}

 

posted @ 2018-09-17 16:13  Gloid  阅读(122)  评论(0编辑  收藏  举报