constructive algorithms greedy implementation math *1400

Please click on ads to support us..

Python Code:

def expand(a , m):
    b = []
    for x in a:
        c = 1
        while x%m==0:
            c*=m
            x = x//m
        if len(b)!=0 and b[-1][0]==x:
            b[-1][1]+=c
        else:
            b.append([x,c])
    return b
            

t = int(input())
for i in range(t): 
    n , m = map(int , input().split())
    a = list(map(int , input().split()))
    k = int(input())
    b = list(map(int , input().split()))
                                                                                    A = []
    B = []
                                    arr1 = expand(a,m)
    arr2 = expand(b,m)
            if arr1==arr2:
        print("YES")
    else:
        print("NO")
    

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'

vector<pair<int, long long>> transform(vector<int> &vec, int m) {
	int n = (int) vec.size();
	vector<pair<int, long long>> result;
	for (int ele : vec) {
		long long c = 1;
		while (ele % m == 0) {
			ele /= m;
			c *= (1LL * m);
		}

		if ((int) result.size() == 0 || result.back().first != ele) {
			result.push_back({ele, c});
		} else {
			result.back().second += c;
		}
	}
	return result;
}



void runTestCase()
{
	int n, k, m;
	cin >> n >> m;

	vector<int> vec1(n);
	for (int &ele : vec1)
		cin >> ele;
	cin >> k;
	vector<int> vec2(k);
	for (int &ele : vec2)
		cin >> ele;

	vector<pair<int, long long>> result1 = transform(vec1, m);
	vector<pair<int, long long>> result2 = transform(vec2, m);
	if (result1 == result2)
		cout << "YES";
	else
		cout << "NO";
	cout << endl;
}


signed main()
{
#ifndef ONLINE_JUDGE
	freopen("inputf.in", "r", stdin);
	freopen("outputf.in", "w", stdout);
#endif

	int tc = 1;
	cin >> tc;

	for (int i = 1; i <= tc; i++)
		runTestCase();

	return 0;
}


Comments

Submit
0 Comments
More Questions

1656B - Subtract Operation
1656A - Good Pairs
1367A - Short Substrings
87A - Trains
664A - Complicated GCD
1635D - Infinite Set
1462A - Favorite Sequence
1445B - Elimination
1656C - Make Equal With Mod
567A - Lineland Mail
1553A - Digits Sum
1359B - New Theatre Square
766A - Mahmoud and Longest Uncommon Subsequence
701B - Cells Not Under Attack
702A - Maximum Increase
1656D - K-good
1426A - Floor Number
876A - Trip For Meal
1326B - Maximums
1635C - Differential Sorting
961A - Tetris
1635B - Avoid Local Maximums
20A - BerOS file system
1637A - Sorting Parts
509A - Maximum in Table
1647C - Madoka and Childish Pranks
689B - Mike and Shortcuts
379B - New Year Present
1498A - GCD Sum
1277C - As Simple as One and Two