第十三届 ACM/CCPC 吉林省赛 A. Boring Game

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;
}
  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值