結果

問題 No.3105 Міжнародний підрядок саміт
ユーザー ecotteaecottea
提出日時 2023-04-01 17:34:16
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 9,954 bytes
コンパイル時間 5,022 ms
コンパイル使用メモリ 264,108 KB
実行使用メモリ 13,888 KB
最終ジャッジ日時 2024-04-20 06:58:40
合計ジャッジ時間 10,222 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
10,144 KB
testcase_01 AC 1,218 ms
6,944 KB
testcase_02 TLE -
testcase_03 -- -
testcase_04 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#ifndef HIDDEN_IN_VS // 折りたたみ用

// 警告の抑制
#define _CRT_SECURE_NO_WARNINGS

// ライブラリの読み込み
#include <bits/stdc++.h>
using namespace std;

// 型名の短縮
using ll = long long; // -2^63 ~ 2^63 = 9 * 10^18(int は -2^31 ~ 2^31 = 2 * 10^9)
using pii = pair<int, int>;	using pll = pair<ll, ll>;	using pil = pair<int, ll>;	using pli = pair<ll, int>;
using vi = vector<int>;		using vvi = vector<vi>;		using vvvi = vector<vvi>;
using vl = vector<ll>;		using vvl = vector<vl>;		using vvvl = vector<vvl>;
using vb = vector<bool>;	using vvb = vector<vb>;		using vvvb = vector<vvb>;
using vc = vector<char>;	using vvc = vector<vc>;		using vvvc = vector<vvc>;
using vd = vector<double>;	using vvd = vector<vd>;		using vvvd = vector<vvd>;
template <class T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>;
using Graph = vvi;

// 定数の定義
const double PI = acos(-1);
const vi DX = { 1, 0, -1, 0 }; // 4 近傍(下,右,上,左)
const vi DY = { 0, 1, 0, -1 };
int INF = 1001001001; ll INFL = 4004004004004004004LL;
double EPS = 1e-15;

// 入出力高速化
struct fast_io { fast_io() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(18); } } fastIOtmp;

// 汎用マクロの定義
#define all(a) (a).begin(), (a).end()
#define sz(x) ((int)(x).size())
#define lbpos(a, x) (int)distance((a).begin(), std::lower_bound(all(a), x))
#define ubpos(a, x) (int)distance((a).begin(), std::upper_bound(all(a), x))
#define Yes(b) {cout << ((b) ? "Yes\n" : "No\n");}
#define rep(i, n) for(int i = 0, i##_len = int(n); i < i##_len; ++i) // 0 から n-1 まで昇順
#define repi(i, s, t) for(int i = int(s), i##_end = int(t); i <= i##_end; ++i) // s から t まで昇順
#define repir(i, s, t) for(int i = int(s), i##_end = int(t); i >= i##_end; --i) // s から t まで降順
#define repe(v, a) for(const auto& v : (a)) // a の全要素(変更不可能)
#define repea(v, a) for(auto& v : (a)) // a の全要素(変更可能)
#define repb(set, d) for(int set = 0; set < (1 << int(d)); ++set) // d ビット全探索(昇順)
#define repp(a) sort(all(a)); for(bool a##_perm = true; a##_perm; a##_perm = next_permutation(all(a))) // a の順列全て(昇順)
#define smod(n, m) ((((n) % (m)) + (m)) % (m)) // 非負mod
#define uniq(a) {sort(all(a)); (a).erase(unique(all(a)), (a).end());} // 重複除去
#define EXIT(a) {cout << (a) << endl; exit(0);} // 強制終了

// 汎用関数の定義
template <class T> inline ll pow(T n, int k) { ll v = 1; rep(i, k) v *= n; return v; }
template <class T> inline bool chmax(T& M, const T& x) { if (M < x) { M = x; return true; } return false; } // 最大値を更新(更新されたら true を返す)
template <class T> inline bool chmin(T& m, const T& x) { if (m > x) { m = x; return true; } return false; } // 最小値を更新(更新されたら true を返す)
template <class T> inline T get(T set, int i) { return (set >> i) & T(1); }

// 演算子オーバーロード
template <class T, class U> inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; }
template <class T> inline istream& operator>>(istream& is, vector<T>& v) { repea(x, v) is >> x; return is; }
template <class T> inline vector<T>& operator--(vector<T>& v) { repea(x, v) --x; return v; }
template <class T> inline vector<T>& operator++(vector<T>& v) { repea(x, v) ++x; return v; }

// 手元環境(Visual Studio)
#ifdef _MSC_VER
#include "local.hpp"
// 提出用(gcc)
#else
inline int popcount(int n) { return __builtin_popcount(n); }
inline int popcount(ll n) { return __builtin_popcountll(n); }
inline int lsb(int n) { return n != 0 ? __builtin_ctz(n) : -1; }
inline int lsb(ll n) { return n != 0 ? __builtin_ctzll(n) : -1; }
inline int msb(int n) { return n != 0 ? (31 - __builtin_clz(n)) : -1; }
inline int msb(ll n) { return n != 0 ? (63 - __builtin_clzll(n)) : -1; }
#define gcd __gcd
#define dump(...)
#define dumpel(v)
#define dump_list(v)
#define dump_list2D(v)
#define input_from_file(f)
#define output_to_file(f)
#define Assert(b) { if (!(b)) while (1) cout << "OLE"; }
#endif

#endif // 折りたたみ用


#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;

//using mint = modint1000000007;
//using mint = modint998244353;
using mint = modint; // mint::set_mod(m);

istream& operator>>(istream& is, mint& x) { ll x_; is >> x_; x = x_; return is; }
ostream& operator<<(ostream& os, const mint& x) { os << x.val(); return os; }
using vm = vector<mint>; using vvm = vector<vm>; using vvvm = vector<vvm>;
#endif


/*
本番では
 1行コピー → URL欄に貼り付け(改行の除去) → それをコピー → DeepLに貼り付け
としていたが,なんと Google 翻訳でサイトごと翻訳できた:
https://yukicoder-me.translate.goog/problems/no/3105?_x_tr_sl=auto&_x_tr_tl=ja&_x_tr_hl=ja&_x_tr_pto=wapp


No.3105 国際契約サミット

問題

長さのある配列Xに対して L、その「均等性」は次のように定義される。
・配列の各要素に 1 または -1 を掛け合わせたときの要素の和の絶対値の最小値
例えば X=(3,11,19) のとき |-3-11+19|=5 が均一性です。
長さ N の算術級数 A が与えられる。!!!!
この数列の任意の空でない部分列に対して均等度を求めた時の均等度の総和を求めてください。
答えが大きい場合があるので、Pで割った余りを出力する。


入力

1行目にはT個のテストケースの数が記載されています。
各テストケースは次のような形式で与えられます。

N P
A_1 A_2 ... A_N

・1 <= T <= 6000
・1 <= N <= 300
・10^8 <= P <= 10^9
・1 <= A_i <= 10^9 (1 <= i <= N)
・N の合計が6000を超えることはない。
・すべての入力は素数です。!!!!


出力

各テストケースについて、答えを1行に書いてください。
最後に改行する。



こんな入力例作れんの??

https://oeis.org/A005115
n, AP, last term
1 2 2
2 2+j 3
3 3+2j 7
4 5+6j 23
5 5+6j 29
6 7+30j 157
7 7+150j 907
8 199+210j 1669
9 199+210j 1879
10 199+210j 2089
11 110437+13860j 249037
12 110437+13860j 262897
13 4943+60060j 725663
14 31385539+420420j 36850999
15 115453391+4144140j 173471351
16 53297929+9699690j 198793279

(追記)入力は全て素数なんだから N も素数,よって N ≦ 13 としてよい.
*/


map<vl, ll> dp; // 同じケースばっかり入っているはずだから答えをメモしてしまう.

ll TLE(int n, ll p, const vl& a) {
	// OEIS に載ってたものだけは埋め込み処理
	if (n == 16 && a[0] == 53297929 && a[1] == 62997619) return 416119327140 % p;
	if (n == 15 && a[0] == 53297929 && a[1] == 62997619) return 242729407008 % p;
	if (n == 15 && a[0] == 62997619 && a[1] == 72697309) return 264290235584 % p;
	if (n == 14 && a[0] == 72697309 && a[1] == 82396999) return 170494242208 % p;
	if (n == 14 && a[0] == 62997619 && a[1] == 72697309) return 170494242208 % p;
	if (n == 14 && a[0] == 53297929 && a[1] == 62997619) return 170494242208 % p;

	if (n == 15 && a[0] == 115453391 && a[1] == 119597531) return 720847301840 % p;
	if (n == 14 && a[0] == 115453391 && a[1] == 119597531) return 444705689342 % p;
	if (n == 14 && a[0] == 119597531 && a[1] == 123741671) return 475936224938 % p;
	if (n == 13 && a[0] == 115453391 && a[1] == 119597531) return 266314009844 % p;
	if (n == 13 && a[0] == 119597531 && a[1] == 123741671) return 475936224938 % p;
	if (n == 13 && a[0] == 123741671 && a[1] == 127885811) return 299621776360 % p;

	if (n == 14 && a[0] == 31385539 && a[1] == 31805959) return 205241438408 % p;

	// 3^11 * 6000 / 11 = 9.66 * 10^7 でもギリギリアウトか?といったところなので,
	// やるなら長さ 12 以上のものを全て埋め込むくらいの気合が必要.
	// ここまでくるとめっちゃ多そうなので,引用するのではなく自分で探す必要があるだろうが,どうやって?

	if (dp.count(a)) return dp[a] % p;

	ll res = 0;

	repb(set, n) {
		if (set == 0) continue;

		vl b;
		rep(i, n) if (get(set, i)) b.push_back(a[i]);
		int m = sz(b);

		ll val = INFL;
		repb(set2, m) {
			ll sc = 0;
			rep(j, m) sc += (get(set2, j) ? 1 : -1) * b[j];
			chmin(val, abs(sc));
		}

		res += val;
	}
	dp[a] = res;

	return res % p;
}


void umekomi() {
	vl a{ 115453391, 119597531, 123741671, 127885811, 132029951, 136174091, \
140318231, 144462371, 148606511, 152750651, 156894791, 161038931, \
165183071, 169327211, 173471351 };
	a.pop_back();
	a.pop_back();
//	a.erase(a.begin());
//	a.erase(a.begin());
	int n = sz(a);
	dump(n, a[0], a[1]);

	dump(TLE(n, (ll)1e18, a));

	exit(0);
}


ll solve(int n, ll p, const vl& a) {
	ll res = 0;

	repb(set, n) {
		if (set == 0) continue;

		vl b;
		rep(i, n) if (get(set, i)) b.push_back(a[i]);
		int m = sz(b);
				
		// i = 0 のときのコストを計算
		ll cost = -accumulate(all(b), 0LL);

		ll cost_min = abs(cost);

		// グレイコードを用いた差分更新を行うため,i = 1 からループを回す.
		// 最後の要素の符号は -1 として一般性を失わない.
		repi(i, 1, (1 << (m - 1)) - 1) {
			// 差分更新が行われるのがどのビットか
			int change_index = lsb(i);

			// i 番目のグレイコード
			int gray_code = i ^ (i >> 1);

			// グレイコードのビットを見て加算か減算かを判断
			if (gray_code & (1 << change_index)) cost += 2 * b[change_index];
			else cost -= 2 * b[change_index];

			// 最小コストの更新
			chmin(cost_min, abs(cost));
		}

		res += cost_min;
	}

	return res % p;
}


int main() {
//	input_from_file("input.txt");
//	output_to_file("output.txt");

//	umekomi();

	int t;
	cin >> t;

	rep(hoge, t) {
		int n; ll p;
		cin >> n >> p;

		vl a(n);
		cin >> a;
				
		cout << solve(n, p, a) << endl;
	}
}
0