---
id: 2627
lang: en
translation_of: index.md
source_sha256: 49e2a07782ef72f9d716ef0022d2738ce18f1ac86843da2afc3b86b9d16774be
title: "[Codeforces 653G] Move by Prime — Solution"
description: "Split the median cost into per-number contributions for each prime; a generating function turns them into prefix sums of binomial coefficients."
tags: ["OI","Binomial Coefficients","Combinatorics","Generating Function"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes: []
---

## Main Idea

Of these 3000+ problems, this is the most <span class="slang" data-zh="小清新" data-gloss="xiǎo qīngxīn, “small and fresh”. Internet slang for something light, clean and pleasant; here: neat and simple.">refreshingly clean</span> one I've done.

The first thing you can surely think of: after prime factorization, for a $p^c$, take every element's exponent of that prime and take the median, and that gives this prime's contribution. That's roughly where <span class="slang" data-zh="断片" data-gloss="duànpiàn, “the film snaps”. Slang for a blackout or memory lapse; here: the train of thought just stopped.">my mind went blank</span>.

<!--more-->

However, this problem can perfectly well be solved by splitting out each number's contribution. For the $i$-th number, its contribution is $-c_i$ on the left of the median and $c_i$ on the right. The generating function is clearly:

$$
(1+{1\over x})^{i-1}(1+x)^{n-i}={(1+x)^{n-1}\over x^{i-1}}
$$

Written as binomial coefficients, this is:

$$
\sum_{j = i}^{n - 1} {n - 1 \choose j} - \sum_{j = 0}^{i - 2} {n - 1 \choose j}
$$

Just maintain that and you're done.

## Code

```cpp
// CF653G.cpp
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 3e5 + 200, mod = 1e9 + 7;

int n, ai[MAX_N], ptot, pid[MAX_N], fac[MAX_N], fac_inv[MAX_N], inv[MAX_N], pre[MAX_N];
vector<int> pidx[MAX_N];

void initialize()
{
	for (int i = fac[0] = 1; i < MAX_N; i++)
		fac[i] = 1LL * fac[i - 1] * i % mod;
	inv[0] = inv[1] = fac_inv[0] = 1;
	for (int i = 2; i < MAX_N; i++)
		inv[i] = 1LL * (mod - mod / i) * inv[mod % i] % mod;
	for (int i = 1; i < MAX_N; i++)
		fac_inv[i] = 1LL * fac_inv[i - 1] * inv[i] % mod;
}

int binomial(int n_, int k_) { return 1LL * fac[n_] * fac_inv[k_] % mod * fac_inv[n_ - k_] % mod; }

void factorize(int x)
{
	int acc = x;
	for (int i = 2; 1LL * i * i <= acc; i++)
		if (acc % i == 0)
		{
			if (pid[i] == 0)
				pid[i] = ++ptot;
			int idxcnt = 0;
			while (acc % i == 0)
				idxcnt++, acc /= i;
			pidx[pid[i]].push_back(idxcnt);
		}
	if (acc > 1)
	{
		if (pid[acc] == 0)
			pid[acc] = ++ptot;
		pidx[pid[acc]].push_back(1);
	}
}

int main()
{
	initialize(), scanf("%d", &n);
	for (int i = 1; i <= n; i++)
		scanf("%d", &ai[i]), factorize(ai[i]);
	pre[0] = binomial(n - 1, 0);
	for (int i = 1; i <= n - 1; i++)
		pre[i] = (0LL + pre[i - 1] + binomial(n - 1, i)) % mod;
	int ans = 0;
	for (int ptr = 1; ptr <= ptot; ptr++)
	{
		sort(pidx[ptr].begin(), pidx[ptr].end());
		int rest0 = n - pidx[ptr].size();
		for (int i = 0, siz = pidx[ptr].size(); i < siz; i++)
		{
			rest0++, ans = (0LL + ans + 1LL * pidx[ptr][i] * ((0LL + (rest0 == 1 ? 0 : pre[rest0 - 2])	+ pre[rest0 - 1] + mod - pre[n - 1]) % mod) % mod) % mod;
		}
	}
	printf("%d\n", ans);
	return 0;
}
```
