暑假训练day2 : zoj选练

3844
A. Easy Task

Time Limit: 2 Seconds       Memory Limit: 65536 KB

You are given n integers. Your task is very easy. You should find the maximum integer a and the minimum integer b among these n integers. And then you should replace both a and b with a-b. Your task will not be finished unless all the integers are equal.

Now the problem come, you want to know whether you can finish you task. And if you can finish the task, you want to know the final result.

Input

The first line of the input contain an integer T(T≤ 20) indicates the number of test cases.

Then T cases come. Each case consists of two lines. The first line is an integer n(2≤ n≤ 10) as the problem described. The second line contains n integers, all of them are no less than -100000 and no more than 100000.

Output

For each case you should print one line. If you can finish your task, you should print one of the n integers. Otherwise, you should print "Nooooooo!"(without quotes).

Sample Input
2
3
1 2 3
2
5 5
Sample Output
2
5

水,直接模拟。

#include <iostream>
#include <string.h>
#include <queue>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
typedef long long ll;
int a[20],b[20];

int main() {
	int t,q,n,i,j,flag;
	cin >> q;
	for (t=1;t<=q;t++) {
		cin >> n;
		flag=1;
		for (i=1;i<=n;i++) {
			cin >> a[i];
			if (a[i]!=a[i-1]&&i!=1) flag=0;
		}
		int k=-0x3f3f3f3f,l=0x3f3f3f3f;
		int kk=0,ll=0;
		i=0;
		if (!flag)
		while (i<1000000) {
			i++;
			flag=0;
			k=-0x3f3f3f3f,l=0x3f3f3f3f;
			for (j=1;j<=n;j++) {
				if (a[j]>k) {
					k=max(k,a[j]);
					kk=j;
				}
				if (a[j]<l) {
					l=min(l,a[j]);
					ll=j;
				}
			}
			if (l==k) 
				break;
			int num=0;
			for (j=1;j<=n;j++) {
				if (j!=ll&&j!=kk) {
					b[++num]=a[j];
				}
			}
			b[n-1]=b[n]=k-l;
			for (j=1;j<=n;j++) a[j]=b[j];
		}
		else l=a[1];
		if (i<1000000) cout << l << endl; else cout << "Nooooooo!" << endl;
	}
	return 0;
}

B. GCD Reduce

Time Limit: 2 Seconds       Memory Limit: 65536 KB       Special Judge

You are given a sequence { A1A2, ..., AN}. You task is to change all the element of the sequence to 1 with the following operations (you may need to apply it multiple times):

  • choose two indexes i and j (1 ≤ i < j ≤ N);
  • change both Ai and Aj to gcd(AiAj), where gcd(AiAj) is the greatest common divisor of Ai and Aj.

You do not need to minimize the number of used operations. However, you need to make sure that there are at most 5N operations.

Input

Input will consist of multiple test cases.

The first line of each case contains one integer N (1 ≤ N ≤ 105), indicating the length of the sequence. The second line contains N integers, A1A2, ..., AN (1 ≤ Ai ≤ 109).

Output

For each test case, print a line containing the test case number (beginning with 1) followed by one integer M, indicating the number of operations needed. You must assure that M is no larger than 5N. If you cannot find a solution, make M equal to -1 and ignore the following output.

In the next M lines, each contains two integers i and j (1 ≤ i < j ≤ N), indicating an operation, separated by one space.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值