用 k 种颜色对大小为 n 的环进行染色,要求相邻点上的颜色不同,旋转同构的方案视作相同,求本质不同的方案数
先不考虑同构
考虑容斥有几对颜色相同
得到
考虑怎么计算同构
考虑对于旋转位的置换,一个状态会遍历个不同状态
总共就有个循环
一个不动点就是每个循环的颜色相同的情况
也就是给
由于内的数因数只有的规模
写个就可以了
#include<bits/stdc++.h>
using namespace std;
const int RLEN=1<<20|1;
inline char gc(){
static char ibuf[RLEN],*ib,*ob;
(ob==ib)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
return (ob==ib)?EOF:*ib++;
}
#define gc getchar
inline int read(){
char ch=gc();
int res=0,f=1;
while(!isdigit(ch))f^=ch=='-',ch=gc();
while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
return f?res:-res;
}
#define ll long long
#define re register
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
#define cs const
#define bg begin
#define poly vector<int>
namespace Get{
inline ll mul(ll a,ll b,cs ll &mod){
return (a*b-(ll)((long double)a/mod*b)*mod+mod)%mod;
}
inline ll gcd(ll a,ll b){
return b?gcd(b,a%b):a;
}
inline ll ksm(ll a,ll b,cs ll &mod){
ll res=1;
for(;b;b>>=1,a=mul(a,a,mod))if(b&1)res=mul(res,a,mod);
return res;
}
inline bool isprime(ll x){
static int pr[17]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59};
for(int i=0;i<17;i++)if(x%pr[i]==0)return x==pr[i];
if(x<pr[16])return false;
ll t=x-1,s=0;
while(!(t&1))t>>=1,s++;
for(int i=0;i<17;i++){
ll a=pr[i],b=ksm(a,t,x);
for(int j=1;j<=s;j++){
ll k=mul(b,b,x);
if(k==1&&b!=1&&b!=x-1)return false;
b=k;
}
if(b!=1)return false;
}
return true;
}
inline ll f(ll x,ll c,cs ll &mod){
return (mul(x,x,mod)+c)%mod;
}
inline ll pro(ll x){
ll s=0,t=0,c=rand()%(x-1)+1;
for(int goal=1;;goal<<=1,s=t){
ll val=1;
for(int step=1;step<=goal;step++){
t=f(t,c,x);
val=mul(val,abs(t-s),x);
if(step%127==0){
ll d=gcd(val,x);
if(d>1)return d;
}
}
ll d=gcd(val,x);
if(d>1)return d;
}
}
}
cs int mod=998244353;
inline int add(int a,int b){return (a+=b)>=mod?a-mod:a;}
inline int dec(int a,int b){return (a-=b)<0?a+mod:a;}
inline int mul(int a,int b){return 1ll*a*b%mod;}
inline void Add(int &a,int b){(a+=b)>=mod?a-=mod:0;}
inline void Dec(int &a,int b){(a-=b)<0?a+=mod:0;}
inline void Mul(int &a,int b){a=1ll*a*b%mod;}
inline int ksm(int a,int b,int res=1){for(;b;b>>=1,Mul(a,a))(b&1)&&(Mul(res,a),1);return res;}
inline int Inv(int x){return ksm(x,mod-2);}
inline void chemx(int &a,int b){a<b?a=b:0;}
inline void chemn(int &a,int b){a>b?a=b:0;}
ll pr[100],n;
int cnt[100],tot,k;
inline void getfac(ll x){
if(x==1)return;
if(Get::isprime(x)){pr[++tot]=x;return;}
ll p=x;
while(p>=x)p=Get::pro(x);
while(x%p==0)x/=p;
getfac(x),getfac(p);
}
int ans;
inline void calc(ll d,ll phi){
d=n/d;
int res=ksm(k,d%(mod-1));
(d&1)?Dec(res,k):Add(res,k);
Add(ans,mul(phi%mod,res));
}
void dfs(int pos,ll prod,ll phi){
if(pos>tot)return calc(prod,phi);
dfs(pos+1,prod,phi);
for(int i=1;i<=cnt[pos];i++){
prod*=pr[pos],phi*=(pr[pos]-(i==1));
dfs(pos+1,prod,phi);
}
}
signed main(){
int T=read();
while(T--){
scanf("%lld",&n),k=(read()-1)%mod;
tot=ans=0;
getfac(n);
memset(cnt,0,sizeof(cnt));
sort(pr+1,pr+tot+1);
tot=unique(pr+1,pr+tot+1)-pr-1;
for(int i=1;i<=tot;i++){
ll x=n;
while(x%pr[i]==0)x/=pr[i],cnt[i]++;
}
dfs(1,1,1);
cout<<mul(ans,Inv(n%mod))<<'
';
}
}