T321187 题解
这个题卡逆元!!!!!!!!
Solution#1
直接爆算!快速幂er
得分:
Solution#2
我们发现
Solution#3
因为
后面直接暴力即可,时间复杂度
得分
Solution#4
考虑一个性质
得分
Solution#5
考虑一件事情,如果把
仍然利用上述性质,考虑一个子问题:
令
显然有:
考虑
我们便可倍增,细节见代码。
#include<iostream>
#include<cmath>
using namespace std;
typedef long long ll;
char ts[1005],tp[1005];
const ll m=1e9+6;
ll f[65],n,a,r,t,as,sp,S,g[65];
ll pre(ll x,ll s,ll k){
ll ans=1;
while(s){
if(s&1){
ans=(ans*x)%k;
}
x=(x*x)%k;
s>>=1;
}
return ans;
}
ll gos(ll a,ll bs){
ll cf=1,ans=0,ggs;
f[0]=a%m;g[1]=a%m;
for(ll k=1;k<=40;k++){
if(k>=2){
g[k]=(g[k-1]*g[k-1])%m;
}
f[k]=(f[k-1]*(g[k]+1))%m;
}
while(bs){
ggs=ll(log2(bs));
bs-=(1ll<<ggs);
ans=(ans+(cf*f[ggs])%m)%m;
cf=(cf*g[ggs+1])%m;
}
return ans;
}
int main(){
cin>>t;
while(t--){
cin>>n>>S>>r;
as=0;sp=1;
for(ll j=1;j<=n;j++){
sp=(sp*S)%m;
as=(as+gos(sp,n*n)*pre(j,r,m))%m;
}
cout<<as<<endl;
}
return 0;
}