dp math number theory sortings

Please click on ads to support us..

C++ Code:

// Problem: F. Gifts from Grandfather Ahmed
// Contest: Codeforces - Codeforces Round 860 (Div. 2)
// URL: https://codeforces.com/contest/1798/problem/F
// Memory Limit: 256 MB
// Time Limit: 1000 ms

#include<bits/stdc++.h>

using namespace std;
#define MP make_pair
#define debug(x) cerr<<#x<<" = "<<x<<" ";
#define debug_(x) cerr<<#x<<" = "<<x<<"\n";
typedef long long ll;
typedef unsigned long long ull;
typedef double db;

constexpr int P = 998244353;
int norm(int x) {
    if (x < 0) {
        x += P;
    }
    if (x >= P) {
        x -= P;
    }
    return x;
}
template<class T>
T power(T a, ll b) {
    T res = 1;
    for (; b; b /= 2, a *= a) {
        if (b % 2) {
            res *= a;
        }
    }
    return res;
}
struct Z {
    int x;
    Z(int x = 0) : x(norm(x)) {}
    int val() const {
        return x;
    }
    Z operator-() const {
        return Z(norm(P - x));
    }
    Z inv() const {
        // assert(x != 0);
        return power(*this, P - 2);
    }
    Z &operator*=(const Z &rhs) {
        x = ll(x) * rhs.x % P;
        return *this;
    }
    Z &operator+=(const Z &rhs) {
        x = norm(x + rhs.x);
        return *this;
    }
    Z &operator-=(const Z &rhs) {
        x = norm(x - rhs.x);
        return *this;
    }
    Z &operator/=(const Z &rhs) {
        return *this *= rhs.inv();
    }
    friend Z operator*(const Z &lhs, const Z &rhs) {
        Z res = lhs;
        res *= rhs;
        return res;
    }
    friend Z operator+(const Z &lhs, const Z &rhs) {
        Z res = lhs;
        res += rhs;
        return res;
    }
    friend Z operator-(const Z &lhs, const Z &rhs) {
        Z res = lhs;
        res -= rhs;
        return res;
    }
    friend Z operator/(const Z &lhs, const Z &rhs) {
        Z res = lhs;
        res /= rhs;
        return res;
    }
};
void solve(){
	int n, K;
	cin >> n >> K;
	vector<int>arr(n);
	for(auto &x : arr)cin >> x;
	vector<int>s(K);
	int mxid = 0;
	for(int i = 0; i < K; ++i){
		cin >> s[i];
		if(s[i] > s[mxid])mxid = i;
	}
	vector<bool>use(n, false);
	vector<vector<int>>ans(K);
	auto divbox = [&](int id){
		int len = s[id];
		// cerr << id << " " << len << "\n";
		vector<vector<int>>f(len, vector<int>(len, -1));
		for(int i = 0; i < n; ++i)if(!use[i]){
			for(int j = len - 2; ~j; --j){
				for(int k = 0; k < len; ++k)if(~f[j][k]){
					if(!~f[j + 1][(k + arr[i]) % len]){
						f[j + 1][(k + arr[i]) % len] = i;
					}
				}
			}
			if(!~f[0][arr[i] % len])f[0][arr[i] % len] = i;
		}
		for(int i = len - 1, v = 0; ~i; --i){
			int u = f[i][v];
			use[u] = true;
			ans[id].push_back(arr[u]);
			(v -= arr[u]) %= len;
			if(v < 0)v += len;
		}
	};
	for(int i = 0; i < K; ++i)if(i != mxid)divbox(i);
	int sum = 0;
	for(int i = 0; i < n; ++i){
		if(!use[i]){
			ans[mxid].push_back(arr[i]);
			(sum += arr[i]) %= s[mxid];
		}
	}
	cout << s[mxid] - sum << "\n";
	ans[mxid].push_back(s[mxid] - sum);
	for(int i = 0; i < K; ++i){
		sum = 0;
		for(int j = 0 ; j < (int)ans[i].size(); ++j){
			sum += ans[i][j];
			cout << ans[i][j] << " \n"[j == (int)ans[i].size() - 1];
		}
		assert(sum % s[i] == 0);
	}
}
int main(){
	ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
	solve();
	return 0;
}


Comments

Submit
0 Comments
More Questions

102. Binary Tree Level Order Traversal
96. Unique Binary Search Trees
75. Sort Colors
74. Search a 2D Matrix
71. Simplify Path
62. Unique Paths
50. Pow(x, n)
43. Multiply Strings
34. Find First and Last Position of Element in Sorted Array
33. Search in Rotated Sorted Array
17. Letter Combinations of a Phone Number
5. Longest Palindromic Substring
3. Longest Substring Without Repeating Characters
1312. Minimum Insertion Steps to Make a String Palindrome
1092. Shortest Common Supersequence
1044. Longest Duplicate Substring
1032. Stream of Characters
987. Vertical Order Traversal of a Binary Tree
952. Largest Component Size by Common Factor
212. Word Search II
174. Dungeon Game
127. Word Ladder
123. Best Time to Buy and Sell Stock III
85. Maximal Rectangle
84. Largest Rectangle in Histogram
60. Permutation Sequence
42. Trapping Rain Water
32. Longest Valid Parentheses
Cutting a material
Bubble Sort