欧拉函数与辗转相除法结合 8.1@亿万星辰 AcWing 3999

#include <bits/stdc++.h>
using namespace std;
typedef long long ll; 
ll gcd(ll a,ll m)//辗转相除法求最大公约数 
{
	ll b;
	while(m%a){
		m%=a;
		b=m,m=a,a=b;
	}
	return a;
}
ll Get_phi(ll n)//欧拉定义法求phi(n) 
{
	ll phi=n;
	for(ll i=2;i*i<=n;i++){
		if(n%i==0) {
			phi*=(i-1.0)/i;
			while(n%i==0) n/=i*1.0;
		}
	}
	if(n>1) phi*=(n-1.0)/n;
	return phi;
}
int main()
{
	#ifndef ONLINE_JUDGE
	freopen("3999.txt","r",stdin);
	freopen("out.txt","w",stdout);
	#endif
	ll a,m,b;
	int t;
	scanf("%d",&t);
	for(int i=1;i<=t;i++){
		scanf("%lld%lld",&a,&m);
		b=gcd(a,m);
		b=Get_phi(m/b);//题意为求(m/最大公约数)的欧拉值 
		printf("%lld\n",b);
	}
	return 0;
}
AcWing 3999. 最大公约数代码实现,今日份学习

全部评论

相关推荐

uu们,拒offer时hr很生气怎么办我哭死
爱睡觉的冰箱哥:人家回收你的offer,或者oc后没给你发offer的时候可不会愧疚你,所以你拒了也没必要愧疚他。
点赞 评论 收藏
分享
小叮当411:应该是1-3个月吧
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
昨天 17:10
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务