最小公倍数(HDU 1108)
Problem Description
给定两个正整数,计算这两个数的最小公倍数。
Input
输入包含多组测试数据,每组只有一行,包括两个不大于1000的正整数.
Output
对于每个测试用例,给出这两个数的最小公倍数,每个实例输出一行。
Sample Input
10 14
Sample Output
70
解题思路:模板题,LCM(A,B) = A * B / GCD(A,B) = A / GCD(A,B) * B
AC代码:
#include <iostream>
using namespace std;
int gcd(int a,int b)
{
int tmp;
while (b != 0)
{
tmp = a % b;
a = b;
b = tmp;
}
return a;
}
int main()
{
int a,b;
while (cin >> a >> b)
{
int ans = a / gcd(a,b) * b;
cout << ans << endl;
}
return 0;
}
还是最小公倍数(HDU 1019)
Problem Description
2个正整数的最小公倍数,大家应该都知道怎么求了,比如,10和14的最小公倍数就是70。现在增加点难度,如果给你多个正整数,你还会计算最小公倍数吗?比如,5、7、15这三个数的最小公倍数就是105.
Input
输入数据第一行是一个正整数C(C<10),表示有C组测试用例。
接下来C行,每行是一组测试数据。每组数据首先是一个正整数N(1<N<30),表示本组数据有N个正整数,然后接着是N个正整数。
Output
请输出每组数据的最小公倍数,每组数据输出一行。
题目保证所有的结果都在32位整数范围内。
Sample Input
2
3 5 7 15
6 4 10296 936 1287 792 1
Sample Output
105
10296
解题思路:对于第一组样例,输入三个数,分别是5、7、15,我们先求出前两个数的最大公约数gcd(5,7) = 1
,从而得到前两个数的最小公倍数lcm(5,7) = 35
。再求gcd(35,15) = 5
,最后lcm(35,15) = 35 * 15 / gcd(35,15) = 105
AC代码:
#include <iostream>
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b)//gcd模板
{
ll tmp;
while (b != 0)
{
tmp = a % b;
a = b;
b = tmp;
}
return a;
}
ll lcm(ll a,ll b) { return a / gcd(a,b) * b; }//lcm模板
int main()
{
ll c; cin >> c;//c组数据
while (c --)
{
ll n; cin >> n;//每组数据有n个数
ll tmp1,tmp2;
ll a; ll b;
cin >> a;//第1个数
for (int i = 2;i <= n;i++)//第2 ~ n个数
{
cin >> b;
tmp1 = gcd(a,b);//前两个数的最大公约数
tmp2 = lcm(a,b);//前两个数的最小公倍数
a = tmp2;//更新a
}
//cout << tmp2 <<endl;如果只有一个数,输出tmp就会发生错误
cout << a << endl;
}
return 0;
}
一个新的斐波那契数列(HDU 1021)
Problem Description
现在,有一个新的斐波那契数列,定义如下:
F(0) = 7,
F(1) = 11,
F(n) = F(n-1) + F(n-2) (n>=2).
Input
输入包含多组测试样例,每组测试样例包含一个整数n(n < 1,000,000).
Output
如果F(n)能够被3整除,请输出"yes",否则请输出"no"。
Sample Input
0
1
2
3
4
5
Sample Output
no
no
yes
no
no
no
相信很多同学看到这题会使用如下的做法解题。但其实这么写是会WA
掉的…原因是会爆long long
,也就是说dp[1e6]
非常大,long long
不可能存储得下。
#include <iostream>
using namespace std;
const int N = 1e6 + 10;
long long dp[N];
int main()
{
dp[0] = 7;
dp[1] = 11;
for (int i = 2;i <= N;i++) dp[i] = dp[i - 1] + dp[i - 2];
int n;
while (cin >> n)
{
if (dp[n] % 3 == 0) cout << "yes" << endl;
else cout << "no" << endl;
}
return 0;
}
正确思路
#include <iostream>
using namespace std;
const int N = 1e6 + 10;
long long dp[N];
int main()
{
dp[0] = 7;
dp[1] = 11;
for (int i = 2;i <= N;i++) dp[i] = dp[i - 1] + dp[i - 2];
for (int i = 0;i <= 15;i++) cout << dp[i] % 3 << " ";
return 0;
}
dp[i] % 3
等价于(dp[i - 1] + dp[i - 2]) % 3
等价于(dp[i - 1] % 3 + dp[i - 2] % 3) % 3
。这种写法可以避免爆long long
的问题。所以我们可以直接将dp[i]
赋值为(dp[i - 1] % 3 + dp[i - 2] % 3) % 3
。dp[i]
为0则表示其可以被3整除,否则表示不能被3整除。通过以上代码得到结果:
1 2 0 2 2 1 0 1 1 2 0 2 2 1 0 1
不难发现,模3的结果以1 2 0 2 2 1 0 1
做为循环节。
0 1 2 3 4 5 6 7
8 9 10 11 12 13 14 15
16 17 18 19 20 21 22 23
通过观察不难发现,dp数组对3求模余0的项为2、6、10、14、18、22…其都满足规律:**n % 4 == 2**
。
通过以上分析,得出下面两种AC代码:
AC代码1:
#include <iostream>
using namespace std;
const int N = 1e6 + 10;
long long dp[N];
int main()
{
dp[0] = 7;
dp[1] = 11;
for (int i = 2;i <= N;i++) dp[i] = (dp[i - 1] % 3 + dp[i - 2] % 3) % 3;
int n;
while (cin >> n)
{
if (dp[n]) cout << "no" << endl;
else cout << "yes" << endl;
}
return 0;
}
AC代码2:
#include <iostream>
using namespace std;
int main()
{
int n;
while (cin >> n)
{
if (n % 4 == 2) cout << "yes" << endl;
else cout << "no" << endl;
}
return 0;
}