2020.1.4 大一寒假训练五(GCD&&快速幂)

Problem A:NEFU1077 最大公约数和最小公倍数

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int a, b;
	while(cin>>a>>b)
	{
		int c = __gcd(a, b);  //cpp库函数
		int d = a/c*b;
		cout<<c<<" "<<d<<endl;
	}
	return 0;
}

Problem B:NEFU992 又见GCD

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int a, b;
	while(cin>>a>>b)
	{
		for(int i=b+1; ; i++)
			if(__gcd(a, i) == b)
			{
				cout<<i<<endl;
				break;
			}
	}
	return 0;
}

Problem C:NEFU764 多个数的最大公约数

依次两两做最大公约数的计算

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n, a, b;
	while(cin>>n)
	{
		cin>>a;
		for(int i=1; i<n; i++)
		{
			cin>>b;
			a = __gcd(a, b);
		}
		cout<<a<<endl;
	}
	return 0;
}

Problem D:NEFU765 多个数的最小公倍数

依次两两做最小公倍数的计算

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n;
	long long a, b;
	while(cin>>n)
	{
		cin>>a;
		for(int i=1; i<n; i++)
		{
			cin>>b;
			a = a/__gcd(a, b)*b;
		}
		cout<<a<<endl;
	}
	return 0;
}

Problem E:NEFU1411 LCM&GCD

#include<bits/stdc++.h>
using namespace std;
int t;
long long x, y;
int main()
{
	cin>>t;
	while(t--)
	{
		int cnt=0;
		cin>>x>>y;
		long long tmp = x*y;
		for(long long i=x; i<=y; i++)
			if(tmp%i==0)
				if(tmp/i>=x && tmp/i<=y && __gcd(i, tmp/i)==x)
					cnt++;
		cout<<cnt<<endl;
	}
	return 0;
}

Problem F:NEFU1221 人见人爱gcd

推导出gcd(x,y) = gcd(a,b)
从而得到 x² + y² = a² - 2 * b * gcd(a,b)
不过。。
cin cout会超时。

#include<bits/stdc++.h>
using namespace std;
int t, a, b;
int main()
{
	//ios::sync_with_stdio(0);
	while(scanf("%d", &t) != EOF)
	{
		while(t--)
		{
			scanf("%d%d", &a, &b);
			printf("%d\n", a*a-2*__gcd(a, b)*b);
		}
	}
	return 0;
}

Problem G:NEFU1669 高木同学的因子

// 在相互学习中成长 
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
	ll x, y;
	cin>>x>>y;
	int m = __gcd(x, y);
	int cnt=0;
	for(int i=1; i*i<=m; i++)
		if(m%i==0) cnt++;
	if(sqrt(m)*sqrt(m) == m) cout<<cnt*2-1<<endl; //judge the perfect square number
	else cout<<cnt*2<<endl;
	return 0;
}

Problem H:NEFU601 快速幂取模

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll quick_pow(ll a, ll b, ll c)
{
	int rslt = 1;
	while(b)
	{
		if(b&1) rslt = rslt*a%c;
		a = a*a%c;
		b >>= 1;
	}
	return rslt;
}
int main()
{
	ll A, B, C;
	while(cin>>A>>B>>C)
	{
		cout<<quick_pow(A, B, C)<<endl;
	}
	return 0;
}

Problem I:NEFU1666 库特的数学题

看上去像是道递推题?勇敢去做,等待着你的,只有TLE。
多推几组,你就该发现,T掉的话就换用快速幂做吧。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll quick_pow(ll a, ll b)
{
	int rslt = 1;
	while(b)
	{
		if(b&1) rslt = rslt*a%(1000000007);
		a = a*a%(1000000007);
		b >>= 1;
	}
	return rslt;
}
int main()
{
	long long n;
	while(cin>>n)
	{
		cout<<(quick_pow(3,n)*2)%1000000007<<endl;
	}
	return 0;
}

Problem J:NEFU1834 异或方程解的个数

这道题算是二进制枚举的题,ljw好哥哥为了玩弄锻炼我们(防ak?),特地加了这么一道题。

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n;
	while(cin>>n)
	{
		int cnt=0;
		while(n)
		{
			if(1&n)
				cnt++;
			n >>= 1;
		}
		cout<<(1<<cnt)<<endl;
	}
	return 0;
}

感谢各位大佬鼎力相助。
ranklist

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值