The third F question of the 2019-2020 University, Elementary and Secondary School Student Joint Training Competition exists

Add link description

Title description

Given a B array B[0...n-1]. You need to find another array A[0...n-1] so that the A array meets the following conditions at the same time:
1. For 0<=i<n, A[i] must be 2^j, where j is a positive integer. That is, A[i] must be 2, 4, 8, 16, 32, 64... etc.
2. For 0<=i<n, A[i]^B[i] must divide P evenly, where P=A[0] * A[1] * A[2] * …A[n-1].
Note: For this question, all means power, that is, how many powers, for example: 2 3 means 2 to the 3rd power, so 2^3=8.
If you can find the A array that meets the above requirements, then output "Possible" or output "Impossible" without outputting double quotes.

enter

Multiple sets of test data.
In the first line, an integer G indicates that there are G groups of test data. 1 <= G <= 13.
Each test data format: the
first line, an integer n. 1 <= n <= 50.
In the second line, there are n integers, and the i-th integer is B[i]. 1 <= B[i] <= 10.

Output

There are G lines, one string per line, "Possible" or "Impossible", double quotes are not output.
Sample input Copy
4
2
3 2
3
3 3 3
2
1 10
3
2 3 10

Sample output Copy

Possible
Possible
Impossible
Possible

prompt

The first set of test data: A[] = {2,2} The
second set of test data: A[] = {2,2,2} The
fourth set of test data: A[] = {8,4,2}

Problem analysis:

We assume that the initialization state of array A is all 2, because array B has n numbers, so the initialization state of array A is n 2, and
we take the last set of test examples to derive the change process of array A. The
Insert picture description here
Insert picture description here
above is a solution. ideas
but when no solution that is output impossible
I can not program their own end, and then I added a limit, when p is greater than the index number on the impossible

With AC code

在这里插入代#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int b[1000],a[1000];
int main() {
    
    
	int G;
	cin >> G;
	while(G--) {
    
    
		int n,i,j,num;
		cin>>n;
		num=n;
		for(i=1; i<=n; i++) {
    
    
			cin>>b[i];
			a[i]=b[i];
		}
		int temp=0,flag=1;


		for(i=1; i<=n; i++) {
    
    
			if(b[i]<num) {
    
    
				b[i]+=a[i];          //因为没有数组A ,只能把数组A 的变化量体现在数组B上 
				i=0;                  //只要不满足条件 就从头开始重新判断 
				num++;              // p的幂加一 
				temp=0;
				if(num>6666) {
    
                        // P的幂加一  ,设定上限666 
					goto loop;
					break;
				}
			} else
				temp++;            //每有一个 满足情况的数。计数器加一 
		}


		if(temp==n) {
    
                //当计数器的值与数组中个数相等时说明所有的数都符合条件,           
			printf("Possible\n");  // 即找到了符合条件的数组A 
			flag=0;                               
			goto loop;
		}

		else
			printf("Impossible\n");
loop:                                        //添加一个标记,幂超出范围时终止程序 
		if(flag)
			printf("Impossible\n");


	}
	return 0;
}
码片

Is there a better way for the big guys

Guess you like

Origin blog.csdn.net/wmy0536/article/details/103301540