题解:
很多人都用矩乘来
这道题,但据说矩乘可能会超时
。
当
时:
当
时:
两式整理一下,发现同样的关系:
,计算方法同上。
算出
,减去
再除
即为
。
由于
、
的范围很大,需要使用费马小定理。
#include<bits/stdc++.h>
#define N 1000005
#define mod 1000000007
using namespace std;
typedef long long ll;
char s1[N],s2[N];
struct arr{ll uni,ord;}n,m;
ll a,b,c,d,p,k,t;
void give(char *s,arr &n)
{
int p=strlen(s);
for (int i=0;i<p;++i)
{
n.uni=(n.uni*10+s[i]-48)%mod;
n.ord=(n.ord*10+s[i]-48)%(mod-1);
}
}
ll power(ll x,ll y)
{
ll res=1ll;
while (y)
{
if(y&1ll)res=res*x%mod;
y=y/2ll;x=x*x%mod;
}
return res;
}
int main()
{
scanf("%s%s",s1,s2);
scanf("%lld%lld%lld%lld",&a,&b,&c,&d);
give(s1,n);give(s2,m);
if(a==1)
{
b=((m.uni-1)*b%mod*c+d)%mod;
a=c;
}else
{
k=b*power(a-1,mod-2)%mod;
t=power(a,m.ord-1);
a=c*t%mod;
b=((t-1)*k%mod*c+d)%mod;
}
if(a==1)p=(1+n.uni*b)%mod;else
{
k=b*power(a-1,mod-2)%mod;
t=power(a,n.ord);
p=(t+(t-1)*k)%mod;
}
printf("%lld",((p-d)*power(c,mod-2)%mod+mod)%mod);
return 0;
}