题目不难 有个好的细节想着分享一下
一开始写的有点问题~需要特判掉一个...
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 1e5+10;
const ll mod = 998244353;
ll qmi(ll a,ll b){
ll ans = 1;
while(b){
if(b&1)ans = ans*a%mod;
a = a*a%mod;
b>>=1;
}
return (ans%mod+mod)%mod;
}
ll inv(ll t){
//if(t==998244353)return 1;
return qmi(t,mod-2);
}
void solve()
{
ll a,b;cin>>a>>b;
if(a==1&&b==1){
cout<<0;return;
}
if(a==998244353&&b==1){
cout<<998244352<<"\n";return;
}
ll tema = a;
ll reu = 1;
ll reu2 = 1;
for(int i=2;i<=a/i;++i){
if(a%i==0){
reu = (reu*(i-1)%mod+mod)%mod;
while(a%i==0)a/=i;
reu2 = reu2*i;
}
}
if(a>1)reu = (reu*(a-1)%mod+mod)%mod,reu2 = reu2*a;
//cout<<reu<<" "<<reu2<<"\n";
reu2 = inv(reu2);
// cout<<reu2<<"\n";
reu = reu*reu2%mod;
cout<<(qmi(tema,b)*reu%mod+mod)%mod;
}
int main()
{
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int _;
_ = 1;
while(_--)solve();
}
题目很好写,但是这个算的思路需要借鉴一下
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 1e5+10;
const ll mod = 998244353;
ll qmi(ll a,ll b){S
ll ans = 1;
while(b){
if(b&1)ans = ans*a%mod;
a = a*a%mod;
b>>=1;
}
return (ans%mod+mod)%mod;
}
void solve()
{
ll a,b;cin>>a>>b;
if(a==1){
cout<<0;return;
}
ll tema = a;
ll reu = a;
for(int i=2;i<=a/i;++i){
if(a%i==0){
reu = reu*(i-1)/i;
while(a%i==0)a/=i;
}
}
if(a>1)reu = reu*(a-1)/a;
cout<<qmi(tema,b-1)*reu%mod;
}
int main()
{
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int _;
_ = 1;
while(_--)solve();
}