蓝桥杯4968. 互质数的个数
作者:
闪回
,
2024-03-12 16:04:19
,
所有人可见
,
阅读 33
暴力法,30%
//先写一个暴力
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
#define int long long
int a,b;
const int mod = 998244353;
int qmi(int a,int b)
{
int res = 1;
while(b)
{
if(b & 1)res *= a;
b >>= 1;
a *= a;
}
return res;
}
int gcd(int a,int b)
{
return b?gcd(b,a%b):a;
}
signed main()
{
cin>>a>>b;
int x = qmi(a,b);
int res = 0;
for(int i = 1;i<x;i++)
{
if(gcd(i,x) == 1)res++;
}
cout<<res<<endl;
return 0;
}
正解:分解质因数 + 欧拉函数
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
#define int long long
int a,b;
const int mod = 998244353;
int qmi(int a,int b)
{
int res = 1;
while(b)
{
if(b&1)res = res * a % mod;
b >>= 1;
a = a * a % mod;
}
return res;
}
signed main()
{
cin>>a>>b;
int ans = qmi(a,b),x = a;
for(int i = 2;i<=x/i;i++)
{
if(x % i == 0)
{
ans = ans * qmi(i,mod-2) % mod * (i-1) % mod;
while(x % i == 0) x /= i;
}
}
if(x > 1)ans = ans * qmi(x,mod-2) % mod * (x-1) % mod;
if(a == 1)ans = 0;
if(a == 998244353 && b == 1)ans = 998244352;
cout<<ans<<endl;
return 0;
}