Group decision SDUT discrete mathematics OJ4171

Group decision

Time Limit: 1000 ms Memory Limit: 65536 KiB

Submit Statistic

Problem Description

Let d be a set of nonnegative subnumbers, and let binary operation + be modular M addition. Given D and M, we ask if the algebraic system v = < D, + > constitutes a group. If so, then we find the inverse element of the given element.

Input

For each group of test data, the first row has three numbers n (1 < = n < = 100), m (1 < = m < = 100) and Q (1 < = 100 < = q). Where n is the number of elements in S (the elements may be repeated, please remove the duplicates by yourself), M is as mentioned above, and Q is the number of queries. Next, n numbers x (0 < = x < 100) represent the elements in S. Next row Q, a number y (0 < = y < 100) in each row, represents the inverse element of the query y.

Output

If V does not form a group, just output a - 1.
Otherwise, output Q lines, one number per line, corresponding to the inverse element of a query.

Sample Input

8 7 1
0 6 3 6 1 2 4 5
2
3 4 2
0 2 3
0
3

Sample Output

5
-1

Hint

The example of modular M addition is as follows: for example, modular 7 addition, 3 + 6 = 2, 5 + 2 = 0, that is, the answer is the remainder with division by remainder.

#include <bits/stdc++.h>
#define ll long long
using namespace std;
int arr[110];
int vis[110];
int num[110];
int b[110];
int main()
{
    int n, m, q;
    while(cin >> n >> m >> q)
    {
        int flag = 1;
        memset(b,0,sizeof(b));
        memset(vis,0,sizeof(vis));
        memset(num,-1,sizeof(num));
        memset(arr,-1,sizeof(arr));
        for(int i = 1; i <= n; i++)
        {
            cin>>arr[i];
            vis[arr[i]] = 1;
        }
        for(int i = 1; i <= q; i++)
        {
            cin>>b[i];
        }
        if(vis[0]==0)
        {
            flag = 0;
        }
        num[0] = 0;
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= n; j++)
            {
                if(vis[(arr[i] + arr[j]) % m] && (arr[i] + arr[j]) % m == 0)
                {
                    num[arr[i]] = arr[j];
                    num[arr[j]] = arr[i];
                    break;
                }
            }
        }
        for(int i = 1; i <= n; i++)
        {
            if(num[arr[i]] == -1)
            {
                flag = 0;
            }
        }
        if(!flag)
        {
            cout << "-1" << endl;
        }
        else
        {
            for(int i = 1; i <= q; i++)
            {
                cout << num[b[i]] << endl;
            }
        }
    }
    return 0;
}

 

 

Tags: Programming

Posted on Thu, 07 Nov 2019 16:32:50 -0500 by aiwebs