Link
每次在图上走动一条边,要么乘以一个素数,要么除以一个素数
如果 u ∣ v u|v u∣v的话,显然我按照每次乘以一个素数的走法,走出来的边权和就是 v u \frac{v}{u} uv的约数个数再减去 1 1 1,而且这个肯定是最短的;如果 u ∤ v u\not |v u∣v而且 v ∤ u v\not |u v∣u,那我可以先让 u u u走到 g c d ( u , v ) gcd(u,v) gcd(u,v),然后再从 g c d ( u , v ) gcd(u,v) gcd(u,v)走到 v v v
从 a a a走到其约数 b b b的最短路条数和从 a b \frac{a}{b} ba走到 1 1 1的最短路条数是相同的,都是 a b \frac{a}{b} ba的素约数的排列数,而且这是一个带有重复元素的排列组合。
#include
#include
#include
#define iinf 0x3f3f3f3f
#define linf (1ll<<60)
#define maxn 100010
#define eps 1e-8
#define cl(x) memset(x,0,sizeof(x))
#define rep(i,a,b) for(i=a;i<=b;i++)
#define drep(i,a,b) for(i=a;i>=b;i--)
#define em(x) emplace(x)
#define emb(x) emplace_back(x)
#define emf(x) emplace_front(x)
#define fi first
#define se second
#define de(x) cerr<<#x<<" = "<
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll read(ll x=0)
{
ll c, f(1);
for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-f;
for(;isdigit(c);c=getchar())x=x*10+c-0x30;
return f*x;
}
#define mod 998244353ll
struct EasyMath
{
ll prime[maxn], phi[maxn], mu[maxn];
bool mark[maxn];
ll fastpow(ll a, ll b, ll c)
{
ll t(a%c), ans(1ll);
for(;b;b>>=1,t=t*t%c)if(b&1)ans=ans*t%c;
return ans;
}
void exgcd(ll a, ll b, ll &x, ll &y)
{
if(!b){x=1,y=0;return;}
ll xx, yy;
exgcd(b,a%b,xx,yy);
x=yy, y=xx-a/b*yy;
}
ll inv(ll x, ll p) //p是素数
{return fastpow(x%p,p-2,p);}
ll inv2(ll a, ll p)
{
ll x, y;
exgcd(a,p,x,y);
return (x+p)%p;
}
void shai(ll N)
{
ll i, j;
for(i=2;i<=N;i++)mark[i]=false;
*prime=0;
phi[1]=mu[1]=1;
for(i=2;i<=N;i++)
{
if(!mark[i])prime[++*prime]=i, mu[i]=-1, phi[i]=i-1;
for(j=1;j<=*prime and i*prime[j]<=N;j++)
{
mark[i*prime[j]]=true;
if(i%prime[j]==0)
{
phi[i*prime[j]]=phi[i]*prime[j];
break;
}
mu[i*prime[j]]=-mu[i];
phi[i*prime[j]]=phi[i]*(prime[j]-1);
}
}
}
ll CRT(vector<ll> a, vector<ll> m) //要求模数两两互质
{
ll M=1, ans=0, n=a.size(), i;
for(i=0;i<n;i++)M*=m[i];
for(i=0;i<n;i++)(ans+=a[i]*(M/m[i])%M*inv2(M/m[i],m[i]))%=M;
return ans;
}
}em;
vector<ll> p, fac, plis[maxn];
map<ll,ll> tb;
ll fact[maxn], _fact[maxn], ans[maxn];
int main()
{
ll D = read(), i, u, v;
fact[0]=_fact[0]=1;
rep(i,1,maxn-1)fact[i]=fact[i-1]*i%mod, _fact[i]=em.inv(fact[i],mod);
for(i=1;i*i<=D;i++)
{
if(D%i==0)
{
fac.emb(i);
if(D/i!=i)fac.emb(D/i);
}
}
ll d = D;
for(i=2;i*i<=d;i++)
{
if(d%i==0)
{
p.emb(i);
while(d%i==0)d/=i;
}
}
if(d>1)p.emb(d);
sort(p.begin(),p.end());
sort(fac.begin(),fac.end());
rep(i,0,fac.size()-1)tb[ fac[i] ] = i;
for(auto x:fac)
{
for(auto pr:p)
{
if((D/x)%pr==0)
{
ll to = tb[x*pr];
if(plis[to].empty())
{
for(auto d:plis[tb[x]])plis[to].emb(d);
plis[to].emb(pr);
}
}
}
}
rep(i,0,fac.size())sort(plis[i].begin(),plis[i].end());
rep(i,0,fac.size()-1)
{
auto& vec = plis[i];
auto& Ans = ans[i];
Ans = fact[vec.size()];
ll last = -1, cnt=0;
for(auto x:vec)
{
if(x==last)cnt++;
else
{
Ans = (Ans * _fact[cnt]) %mod;
cnt = 1;
}
last = x;
}
Ans = (Ans * _fact[cnt]) %mod;
}
ll Q = read();
while(Q--)
{
ll u = read(), v = read(), g = __gcd(u,v);
printf("%lld\n",ans[tb[u/g]]*ans[tb[v/g]]%mod);
}
return 0;
}