acwing4986.互质数的个数

发布时间:2024年01月12日

题目不难 有个好的细节想着分享一下

一开始写的有点问题~需要特判掉一个...

#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();
}

文章来源:https://blog.csdn.net/m0_60921016/article/details/135562908
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。