# bzoj2242，洛谷2485----SDOI2011计算器（exgcd,qsm,bsgs模板）

BSGS

$t=\lfloor \sqrt p \rfloor$

$(a^t)^c=ba^d \pmod p$

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define ll long long
#include<map>

using namespace std;

{
ll x=0,f=1;char ch=getchar();
while (!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
while (isdigit(ch)) {x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
return x*f;
}

ll n,m;
ll mod;
map<ll,ll> mp;

ll qsm(ll i,ll j)
{
ll ans=1;
while (j)
{
if (j&1) ans=ans*i%mod;
i=i*i%mod;
j>>=1;
}
return ans;
}

ll exgcd(ll &x,ll &y,ll a,ll b)
{
if (b==0)
{
x=1;
y=0;
return a;
}
ll cnt=exgcd(x,y,b,a%b);
ll tmp =x;
x=y;
y=tmp-a/b*y;
return cnt;
}

ll bsgs(ll a,ll b)
{
mp.clear();
if (a%mod==0 && b==0) return 0;
if (a%mod==0 && b!=0) return -1;
//if (a==1 && b!=1) return -1;
//if (a==1 && b==1) return 0;
//==0) return -1;
ll t = ceil(sqrt(mod));
for (ll i=0;i<=t;i++)
{
ll tmp = qsm(a,i)*b%mod;
if (!mp[tmp]) mp[tmp]=i;
}
for (ll c=1;c<=t;c++)
{
ll cnt = qsm(a,c*t)%mod;
if (mp[cnt])
{
//cout<<c*t<<endl;
return c*t-mp[cnt];
}
}
return -1;
}

int main()
{
scanf("%d%d",&n,&m);
if (m==1)
{
for (int i=1;i<=n;i++)
{
ll x,y;
printf("%lld\n",qsm(x,y));
}
}

if (m==2)
{
for (int i=1;i<=n;i++)
{
ll a,b,c;
ll x=0,y=0;
ll gcd=exgcd(x,y,a,b);
if (c%gcd!=0)
{
printf("Orz, I cannot find x!\n");
continue;
}
ll tmp = b/gcd;
x=x*c/gcd%tmp;
x=(x%tmp+tmp)%tmp;
printf("%lld\n",x);
}
}
//return 0;
if (m==3)
{
for (int i=1;i<=n;i++)
{
ll a,b;