ACM/CCPC 历届真题 题解目录
Problem A. Boring Game
Time Limit: 1000ms Memory Limit: 512MB
Description
Luna wants to play a boring game.
She has three integers a, b, c in the beginning.
She will play the game k rounds.
Each round she will do exactly 3 steps in order:
1. If a > b then a = a − b.
2. If b > c then b = b − c.
3. If c > a then c = c − a.
She wants you to help her calculate a, b, c after k rounds.
Input
First line contains an integer T (1 ≤ T ≤ 10) represents the number of test cases.
For each test case:
One line contains four integers a, b, c, k (1 ≤ a, b, c ≤ 1 0 6 10^6 106, 1 ≤ k ≤ 1 0 9 10^9 109) represents the integers in the beginning and the rounds of the game.
Output
For each test case, output a line contains three integers represents the integers a, b, c after k rounds.
Sample
Input
2
1 10 100 2
100 10 1 2
Output
1 10 98
81 8 1
题目大意:
输入整数T代表测试用例的数量,每个测试用例输入a, b, c, k四个整数,求经过k轮上述三个操作后,a,b,c的值是多少?
分析:
这道题可以直接暴力。但是K的上限是
1
0
9
10^9
109,不做任何处理的话,是一定会超时的。
思路:
当
a
=
b
=
c
a=b=c
a=b=c时,每轮操作的三个if都不会被执行,这个时候,之后不论经过几轮操作,a,b,c的值都不会发生改变。
所以加个if(a == b && b == c) break;即可。这时大概最多也就循环
1
0
6
10^6
106次,就不会再超时了。
代码如下:
#include <iostream>
using namespace std;
int t;
int a, b, c, k;
int main()
{
scanf("%d", &t);
while(t--) //t个样例
{
scanf("%d%d%d%d", &a, &b, &c, &k);
while(k--) //k轮操作
{
if(a > b) a -= b;
if(b > c) b -= c;
if(c > a) c -= a;
//重点优化,不加会超时。k最大为1e9,加了此条件,大约最多循环1e6次
if(a == b && b == c) break;
}
printf("%d %d %d\n", a, b, c);
}
return 0;
}