标签:x
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define N 140345
#define INF 0x7fffffffffffffffll
using namespace std;
typedef long long ll;
int T,cnt,tot,totc;
int head[N+10];
ll a,b,c;
ll prime[N];
ll the_prime_c[N];
ll cnt_num_c[N];
struct node
{
ll from,to,val;
int next;
}edge[N+10];
void cutit(ll x)
{
totc=0;
for(ll i=2;i*i<=x;i++)
{
if(x%i==0)
{
the_prime_c[++totc]=i;
cnt_num_c[totc]=1;
x/=i;
while(x%i==0){x/=i;cnt_num_c[totc]++;}
}
}
if(x!=1)the_prime_c[++totc]=x,cnt_num_c[totc]=1;
}
void init()
{
memset(head,-1,sizeof(head));
cnt=1;
}
void edgeadd(ll from,ll to,ll val)
{
edge[cnt].from=from,edge[cnt].to=to,edge[cnt].val=val;
edge[cnt].next=head[from];
head[from]=cnt++;
}
void exgcd(ll a,ll b,ll &x,ll &y,ll &gcd)
{
if(!b)
{
x=1,y=0,gcd=a;
return;
}
exgcd(b,a%b,y,x,gcd);
y=y-a/b*x;
}
void get_factor(ll x)
{
tot=0;
for(int i=2;i*i<=x;i++)
{
if(x%i==0)
{
prime[++tot]=i;
while(x%i==0)x/=i;
}
}
if(x!=1)prime[++tot]=x;
}
ll quick_my(ll x,ll y,ll mod)
{
ll ret=1;
while(y)
{
if(y&1)ret=(ret*x)%mod;
x=(x*x)%mod;
y>>=1;
}
return ret;
}
ll get_inv(ll x,ll mod)
{
ll X,Y,GCD;
exgcd(x,mod,X,Y,GCD);
return (X%mod+mod)%mod;
}
ll get_phi(ll x)
{
ll ret=x,tmp=x;
for(ll i=2;i*i<=tmp;i++)
{
if(tmp%i==0)
{
ret=ret*(i-1)/i;
while(tmp%i==0){tmp/=i;}
}
}
if(tmp!=1)ret=ret*(tmp-1)/tmp;
return ret;
}
int check(ll x,ll MOD,ll PHI)
{
for(int i=1;i<=tot;i++)
{
if(quick_my(x,PHI/prime[i],MOD)==1)return 0;
}
return 1;
}
ll find_primitive_root(ll x)
{
ll tmp=get_phi(x);
get_factor(tmp);
for(int i=2;;i++)
{
if(check(i,x,tmp))return i;
}
}
ll Baby_Step_Giant_Step(ll A,ll B,ll C)
{
init();
ll m=(int)ceil(sqrt(C));
ll k=1;
for(int i=0;i<m;i++)
{
int flag=1;
for(int j=head[k%N];j!=-1;j=edge[j].next)
{
if(edge[j].val==k){flag=0;break;}
}
if(flag)edgeadd(k%N,i,k);
k=k*A%C;
}
ll invk=get_inv(k,C);
ll invD=1;
for(int i=0;i<=m;i++)
{
ll tmpB=B*invD%C;
for(int j=head[tmpB%N];j!=-1;j=edge[j].next)
{
if(edge[j].val==tmpB)return edge[j].to+i*m;
}
invD=invD*invk%C;
}
return -1;
}
ll work(ll a,ll b,ll pri,ll num)
{
ll B=quick_my(pri,num,INF);b%=B;
if(b==0)return quick_my(pri,num-((num-1)/a+1),INF);
ll cnt_b=0;
while(b%pri==0)
{
cnt_b++,b/=pri,num--;
}
if(cnt_b%a!=0)return 0;
ll g=find_primitive_root(B);
ll indB=Baby_Step_Giant_Step(g,b,B);
ll X,Y,GCD;
exgcd(a,get_phi(B),X,Y,GCD);
// A*ind_g(A)=ind_g(B)(mod φ(C1));
if(indB%GCD!=0)return 0;
return GCD*quick_my(pri,cnt_b-cnt_b/a,INF);
}
int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%lld%lld%lld",&a,&b,&c);
c=c*2+1;
cutit(c);
ll ans=1;
for(int i=1;i<=totc;i++)
{
ans*=work(a,b,the_prime_c[i],cnt_num_c[i]);
}
printf("%lld\n",ans);
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:x
原文地址:http://blog.csdn.net/wzq_qwq/article/details/47813629